integration - Error of midpoint, trapezoidal and simpson rule in matlab -
i'm writing script numerically find integral using these 3 methods. right can compute right value integral, need print error , upper analytical bound of it. have no idea how in matlab. formulas need find maximal value of f'' , f'''', dont know ho that. can me?
thank you
recall upper bound of error either trapezoidal rule, simpson's rule or midpoint rule on f(x)
must bound between interval of x in [a,b]
or else won't able find error. have 2 solutions you, depending on has been given.
solution #1 - closed form solution f(x) given
if have closed form solution of integral, use symbolic toolbox in matlab first define f(x)
, use diff
command differentiate find f'(x)
. if want second derivative, apply diff
command it.
example:
syms x; f = x^4; df = diff(f); d2f = diff(df); f = x^4 df = 4*x^3 d2f = 12*x^2
if want find maximum within interval, assuming step size t
between lower bound a
, b
, can try doing:
a = 1; b = 3; t = 0.01; maxf = max(abs(double(subs(f,a:t:b)))); maxf = 81
what above code finds maximum value between [1,3]
of x^4
in step size of t = 0.01
. can replace fourth line's first parameter f
whatever function want. note: error terms require **absolute* value, why abs
function there.
solution #2 - closed form solution f(x) not given
if don't have function, have set of (x,y)
pairs, can still use diff
function on output y
values implement discrete approximation derivative. such, derivative approximately equal to:
x_i' = x_{i+1} - x_i
this takes (i+1)
point , subtracts i
point. if apply diff
on array of n
points, return array of n-1
points accommodate looking 1 sample ahead find difference. after, can use array , find maximum of derivative. if want find maximum of second derivative, call diff
again on first outpt of diff
. example started:
x = 0:0.01:2*pi; y = sin(x); ymax = max(abs(diff(y))); ymax = 0.0100
what above finds maximum value of discrete approximation derivative y = sin(x)
, denoted diff(y)
.
hopefully 1 of these work you!
aside
i used teach numerical methods when instructor @ university. take @ these slides intro numerical integration using simpson's rule , trapezoidal rule. there slides on how code in matlab (without using built-in functions), slides on how compute error, , how many points need within fixed interval guarantee particular approximation error.
http://www.rnet.ryerson.ca/~rphan/mth510/f2012/lab_10/mth510f2012_lab10.pdf
Comments
Post a Comment