> diary 9_11_00 % first move to the directory where the files are saved. > pwd ans = C:\MATLAB\bin > cd ../.. > pwd ans = C:\ > cd _math320_00 % check the files are there: > ls . f_int.m simp.m trap_filter.m .. left_hand.m simp_filter.m f.m left_hand_filter.m trap.m % look at "left_hand.m" to see what the command is to use it > help left_hand this has the integration for the left-hand rule, n can be an even or odd number. [area,h,error] = left_hand(a,b,n) % use Left Hand Rule to approximate int_0^1 cos(x) using 2, 4, 8, 16 % mesh-points > [a_lhr1,h1,e_lhr1] = left_hand(0,1,2); > [a_lhr2,h2,e_lhr2] = left_hand(0,1,4); > [a_lhr3,h3,e_lhr3] = left_hand(0,1,8); > [a_lhr4,h4,e_lhr4] = left_hand(0,1,16); % look at the areas. They're decreasing, hopefully to some value... > a_lhr1 a_lhr1 = 1 > a_lhr2 a_lhr2 = 0.9103 > a_lhr3 a_lhr3 = 0.8729 > a_lhr4 a_lhr4 = 0.8565 % look at the errors. they appear to be decreasing by a factor of 2. % this is a good sign. > e_lhr1 e_lhr1 = -0.1585 > e_lhr2 e_lhr2 = -0.0688 > e_lhr3 e_lhr3 = -0.0314 > e_lhr4 e_lhr4 = -0.0150 % check whether they really are decreasing by a factor of 2... > e_lhr1/e_lhr2 ans = 2.3039 > e_lhr2/e_lhr3 ans = 2.1911 > e_lhr3/e_lhr4 ans = 2.0920 % what is the error if we have 10000 meshpoints? > [a_lhr,h,e_lhr] = left_hand(0,1,10000); > e_lhr e_lhr = -2.2986e-005 % now repeat the excercise with the trapezoidal rule > [a_trap1,h1,e_trap1] = trap(0,1,2); > [a_trap1,h1,e_trap2] = trap(0,1,4); > [a_trap1,h1,e_trap3] = trap(0,1,8); > [a_trap1,h1,e_trap4] = trap(0,1,16); % first, recall the errors with the left-hand rule > e_lhr1, e_lhr2, e_lhr3, e_lhr4 e_lhr1 = -0.1585 e_lhr2 = -0.0688 e_lhr3 = -0.0314 e_lhr4 = -0.0150 % the errors are definitely smaller than with the left-hand rule % also, they appear to be decreasing more quickly. > e_trap1, e_trap2, e_trap3, e_trap4 e_trap1 = 0.0713 e_trap2 = 0.0078 e_trap3 = 0.0014 e_trap4 = 3.1168e-004 % look at the ratios of the errors to see the rate they're decreasing % it's faster than 4. > e_trap1/e_trap2 ans = 9.1367 > e_trap2/e_trap3 ans = 5.4527 > e_trap3/e_trap4 ans = 4.5931 % what's the error with 10000 mesh-points? It's 10^(-10). With the % left-hand rule, it's 10^(-5). We're much happier with the % trapezoidal rule. > [a_trap,h,e_trap] = trap(0,1,10000); > e_trap e_trap = 7.0137e-010 > diary off