numerical methods - Calculating Errors of the Trapezoidal Rule in MATLAB -



numerical methods - Calculating Errors of the Trapezoidal Rule in MATLAB -

i'm trying calculate how errors depend on step, h, trapezoidal rule. errors should smaller smaller value of h, me doesn't happen. code:

iref reference value calculated , verified simpson's method , matlab function quad, respectively

for h = 0.01:0.1:1 x = a:h:b; v = y(x); itrap = (sum(v)-v(1)/2-v(end)/2)*h; error = abs(itrap-iref) end

i think there's wrong way i'm using h, because trapezoidal rule works known integrals. happy if help me this, because can't understand why errors "jumping around" way do.

i wonder if maybe part of problem not intervals - each step size h - have same a , b because of way x constructed. seek next additional fprintf statement:

for h = 0.01:0.1:1 x = a:h:b; fprintf('a=%f b=%f\n',x(1),x(end)); v = y(x); itrap = (sum(v)-v(1)/2-v(end)/2)*h; error = abs(itrap-iref); end

depending upon a , b (i chose a=0 , b=5) a values identical (as expected) b varied 4.55 5.0.

i think want maintain interval [a,b] same each step size take in order improve comparing between each iteration. rather iterating on step size, instead iterate on n, number of as spaced sub-intervals within [a,b].

rather

for h = 0.01:0.1:1 x = a:h:b;

you more like

% iterate on each value of n, chosen step size % similar had before n = [501 46 24 17 13 10 9 8 7 6] % create as spaced vector of n numbers between , b x = linspace(a,b,n); % step delta h = x(2)-x(1); v = y(x); itrap = (sum(v)-v(1)/2-v(end)/2)*h; error = abs(itrap-iref); fprintf('a=%f b=%f len=%d h=%f error=%f\n',x(1),x(end),length(x),h,error); end

when evaluate above code, notice a , b consistent each iteration, h chose before, , error increment step size increases.

try above , see happens!

matlab numerical-methods

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -