clear exact = @(x) (x - pi*sinh(2*x)/sinh(2*pi))/4; approx3 = @(x) 2/5*sin(x) - 1/8*sin(2*x) + 2/39*sin(3*x); approx5 = @(x) approx3(x) -1/40*sin(4*x) + 2/145*sin(5*x); a = 0; b = pi; np = 100; x = linspace(a, b, np); figure(1) plot(x, exact(x), '-') hold on plot(x, approx3(x), '-') legend('exact', 'galerkin-3', 'location', 'best') grid on xlabel('x') title('Exact soulution of an ODE vs 3-term Galerkin approximations') figure(2) plot(x, exact(x), '-') hold on plot(x, approx5(x), '-') legend('exact', 'galerkin-5', 'location', 'best') grid on xlabel('x') title('Exact soulution of an ODE vs 5-term Galerkin approximations') nn = 1000; xx = linspace(a, b, nn); error3 = norm(exact(xx) - approx3(xx), inf) error5 = norm(exact(xx) - approx5(xx), inf)