%% Symbolic MWR solution followed by numerical graph %% of u" + u + x = 0, u(0)=0=u(1) by Galerkin method %syms x real ; % activate x %% u_e = sin(x)/sin(1) - x ; % exact solution %% u_a = [H(x)]{D} Galerkin solution %H = [(x - x^2) (x^2 - x^3)] ; % assumed solution %% R_E = R_o + [h(x)]{D} residual error %R_o = x ; part 1 %h = [(-2 + x - x^2) (2 - 6*x + x^2 - x^3)] ;% part 2 %s = H' * h ; % prepare to integrate %s = simplify (s) ; % simplify %c = H' * R_o ; % prepare to integrate %% C + S*D = 0, S*D = -C the matrices %S = int (s, 0, 1) ; % integrate square %% S = [ -3/10, -3/20; -3/20, -13/105] answer %C = int (c, 0, 1) ; % integrate column %% C = [ 1/12; 1/20] answer %D = -S \ C ; % invert, solve %% D = [ 71/369; 7/41 ] answer % Comment out the symbolic part, plot the result D = [ 71/369; 7/41 ] ; % answer % plot the two results for k = 1:21 ; R(k) = (k - 1)/20. ; r = R(k) ; u_e(k) = sin(r)/sin(1) - r ; u_a(k) = [(r - r^2) (r^2 - r^3)]*D ; end ; % for k ; clf ; plot(R, u_e, 'r--', 'LineWidth', 2) ; hold on grid on plot(R, u_a, 'k-') ; title ('Global Galerkin MWR u"+u+x=0') xlabel('x'); ylabel('u(x) exact dashed') print -dpng Global_MWR_G fprintf ('Created file Global_MWR_G.png \n')