%Lab 4
%Written by Håkon Hoel, 2011-11-20

%Randvardesproblem
close all, clear all
a=1; b=-1; f = @(x)(3*pi)^2*cos(3*pi*x); % villkor
n = [5 10 20 40]; % delintervall
u_0 = a; u_end=b; % randvillkor
colors = 'crgbk'; % vector for coloring the plotted solutions

for i=1:length(n)
    
    h= 1/(n(i)+1); x = (1:n(i))'*h;
    
    A = %Fill in this line 
    
    b = h^2*f(x); b(1)= b(1)+u_0; b(end)=b(end)+u_end;
    u = A\b; %numerical solution
    
    
    plot([0;x;1], [u_0;u;u_end], colors(i))
    hold on
    
end
    
u_sol = @(x) cos(3*pi*x); x = (0:0.01:1)';
plot(x,u_sol(x),colors(5)) %plotting the analytical solution

title('Solutions of the boundary value problem')
xlabel('x-axis')
legend('n=5','n=10','n=20','n=40','u_{analytical}')