clear all; close all; clc; % solving the bvp example with direct solve % solves eq. 5.5.3 from the notes N=100; alpha=1; beta=1; t0=0; tN=10; t=linspace(t0,tN,N+1); dt=t(2)-t(1); p=exp(-(t.^2)); q=cos(t); r=sin(3*t)+cos(t); for i=1:N-1 A(i,i)=2+(dt^2)*q(i+1); end % we use q(i+1) instead of q(i) because the vector in the notes % starts with q0 for i=1:N-2 A(i,i+1)=-1+(dt/2)*p(i+1); %upper diagonal A(i+1,i)=-1-(dt/2)*p(i+2); %lower diagonal end for i=2:N-2 b(i)=-dt^2*r(i+1); end b(1)=-dt^2*r(2)+(1+dt*p(2)/2)*alpha; b(N-1)=-dt^2*r(N)+(1-dt*p(N)/2)*beta; b=b.'; % spy the matrix spy(A) y=A\b; Y(2:100)=y; Y(1)=alpha; Y(N+1)=beta; % solving an eigenvalue problem clear all; close all; clc; n=200; x=linspace(-1,1,n); dx=x(2)-x(1); n0=5; m=n-2; e=ones(m,1); A=spdiags([e (-2+dx^2*n0)*e e],-1:1, m, m); [V,D]=eigs(A); Y1(2:n-1)=V(:,1); Y1(1)=0; Y1(n)=0; Y2(2:n-1)=V(:,2); Y2(1)=0; Y2(n)=0; Y3(2:n-1)=V(:,3); Y3(1)=0; Y3(n)=0; Y4(2:n-1)=V(:,4); Y4(1)=0; Y4(n)=0;