>> diary 9_13_00 % remind myself how to call left_hand.m >> 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) % first approximate the integral of sin(.1x) over [0,1] >> [area,h,err_lhr] = left_hand(0,1,101); >> [area,h,err_trap] = trap(0,1,101); >> [area,h,err_simp] = simp(0,1,101); >> err_lhr err_lhr = 4.9917e-004 >> err_trap err_trap = 4.1632e-009 >> err_simp err_simp = 3.1225e-016 % now approximate the integral of sin(x) over [0,1] >> [area,h,err_lhr] = left_hand(0,1,101); >> [area,h,err_trap] = trap(0,1,101); >> [area,h,err_simp] = simp(0,1,101); >> err_lhr err_lhr = 0.0042 % here I change the format that the numbers are displayed to % scientific notation, so I can see 5 significant figures at all times. >> format short e >> err_lhr err_lhr = 4.2112e-003 >> err_trap err_trap = 3.8308e-006 >> err_simp err_simp = -2.5539e-011 % now approximate the integral of sin(5*x) over [0,1] >> [area,h,err_lhr] = left_hand(0,1,101); >> [area,h,err_trap] = trap(0,1,101); >> [area,h,err_simp] = simp(0,1,101); >> err_lhr err_lhr = -4.7648e-003 >> err_trap err_trap = 2.9849e-005 >> err_simp err_simp = -4.9760e-009 % from the above, we see that if the function is fixed, then % Simpsons rule does better than the trapezoidal rule which does % better than the left-hand rule. We also see that if you fix a % method and increase the structure of the function (sin(.1x) vs % sin(x) vs sin(5x) then the rule does worse and worse. % now we're going to use a different routine that will find the % needed number of meshpoints to meet the demanded error. We're % approximating the integral of x^6 over [0,1]. >> help left_hand_filter this takes the interval [a,b], and starts by integrating on an interval with n mesh-points. It then takes finer and finer meshes until the error is less than the given tolerance (tol). It then returns the area, the error, the mesh-size, and the number of flops it took to perform the integral using the left-hand rule: [area,h,error,t_run] = left_hand_filter(a,b,n,tol) % the routine prints out the error at each try, you can see that the % error decreases until it's less than 10^(-2). The routine then % stops and I get the area, the interval length, the final error, % and the number of flops. >> [area,h,error,t_run ] = left_hand_filter(0,1,3,10^(-2)) error = 1.3504e-001 error = 9.4395e-002 error = 5.4728e-002 error = 2.9299e-002 error = 1.5137e-002 error = 7.6904e-003 area = 1.3517e-001 h = 1.5625e-002 error = 7.6904e-003 t_run = 334 % now use trapezoidal rule. I've edited the errors out of the diary % since the point of all this was to see the flop-count. >> [area,h,error,t_run ] = trap_filter(0,1,3,10^(-2)) area = 1.5063e-001 h = 1.2500e-001 error = -7.7719e-003 t_run = 94 % now use simpsons rule. >> [area,h,error,t_run ] = simp_filter(0,1,3,10^(-2)) area = 1.4535e-001 h = 2.5000e-001 error = -2.4879e-003 t_run = 51 % so we see that to meet the tolerance of 10^(-2), the lefthand % rule, trapezoidal rule, and simpsons rule take 334, 94, 51 flops % respectively. % now go for the tolerance of 10^(-3) >> [area,h,error,t_run ] = left_hand_filter(0,1,3,10^(-3)) area = 1.4188e-001 h = 1.9531e-003 error = 9.7466e-004 t_run = 2574 >> [area,h,error,t_run ] = trap_filter(0,1,3,10^(-3)) area = 1.4335e-001 h = 3.1250e-002 error = -4.8812e-004 t_run = 334 >> [area,h,error,t_run ] = simp_filter(0,1,3,10^(-3)) area = 1.4302e-001 h = 1.2500e-001 error = -1.6094e-004 t_run = 83 % for 10^(-3) the flop counts are 2574, 334, and 83 respectively. % now go for 10^(-4) >> [area,h,error,t_run ] = left_hand_filter(0,1,3,10^(-4)) area = 1.4280e-001 h = 1.2207e-004 error = 6.1028e-005 t_run = 40974 >> [area,h,error,t_run ] = trap_filter(0,1,3,10^(-4)) area = 1.4289e-001 h = 7.8125e-003 error = -3.0517e-005 t_run = 1294 >> [area,h,error,t_run ] = simp_filter(0,1,3,10^(-4)) area = 1.4287e-001 h = 6.2500e-002 error = -1.0144e-005 t_run = 147 % for 10^(-4) the flop counts are 40974, 1294, and 147 % now go for 10^(-5). We're giving up on the left-hand rule % because it's too slow >> [area,h,error,t_run ] = trap_filter(0,1,3,10^(-5)) area = 1.4286e-001 h = 3.9063e-003 error = -7.6294e-006 t_run = 2574 >> [area,h,error,t_run ] = simp_filter(0,1,3,10^(-5)) area = 1.4286e-001 h = 3.1250e-002 error = -6.3534e-007 t_run = 275 % the flop counts are 2574 (trap) and 275 (simpsons) % how long does it take simpsons rule to reach 10^(-10)? % 4115 flops while trapezoidal rule takes 1310734 flops (this took % nearly two hours on the pc!) >> [area,h,error,t_run ] = simp_filter(0,1,3,10^(-10)) area = 1.4286e-001 h = 1.9531e-003 error = -9.7012e-012 t_run = 4115 >> [area,h,error,t_run ] = trap_filter(0,1,3,10^(-10)) area = 1.4286e-001 h = 7.6294e-006 error = -2.9104e-011 t_run = 1310734 >> diary off