% Math 551 demo 4_17_12 on trapezoidal rule for integrating % y = x^2, y=x^(0.5), and y=x^3 from 0 to 1 using trapz, % traptd2.m and simtd2.m format long h=.1; y=(0:h:1).^(1.0); h*trapz(y) ans = 0.500000000000000 y=(0:h:1).^(2.0); h*trapz(y) ans = 0.335000000000000 h=.01; y=(0:h:1).^(2.0); h*trapz(y) ans = 0.333350000000000 h=.001; y=(0:h:1).^(2.0); h*trapz(y) ans = 0.333333500000000 h=.0001; y=(0:h:1).^(2.0); h*trapz(y) ans = 0.333333335000000 clear h=.1; y=(0:h:1).^(0.5); h*trapz(y) ans = 0.660509341706817 h=.01; y=(0:h:1).^(0.5); h*trapz(y) ans = 0.666462947103148 h=.001; y=(0:h:1).^(0.5); h*trapz(y) ans = 0.666660134393682 h=.0001; y=(0:h:1).^(0.5); h*trapz(y) ans = 0.666666459197108 y=(0:h:1).^(3.0); h*trapz(y) ans = 0.250000002500000 >> f = @(x) x.^2 f = @(x)x.^2 >> traptd2(f,0,1,11) ans = 0.335000000000000 >> simptd2(f,0,1,11) ans = 0.333333333333333 >> f = @(x) x.^(0.5) f = @(x)x.^(0.5) >> simptd2(f,0,1,11) ans = 0.664099589757421 >> f = @(x) x.^3 f = @(x)x.^3 >> traptd2(f,0,1,11) ans = 0.252500000000000 >> simptd2(f,0,1,11) ans = 0.250000000000000 >> f = @(x) x.^4 f = @(x)x.^4 >> traptd2(f,0,1,11) ans = 0.203330000000000 >> simptd2(f,0,1,11) ans = 0.200013333333333 >> f = @(x) x.^(0.5) f = @(x)x.^(0.5) >> simptd2(f,0,1,101) ans = 0.666585482066724 >> simptd2(f,0,1,1001) ans = 0.666664099383543 >> simptd2(f,0,1,10001) ans = 0.666666585482046 diary off