# Error problem

• May 2nd 2010, 01:02 AM
chronicals
Error problem
I want to compute x within 0.1% relative error with Simpson method, these are my m-files. Which command i should add for this?

function
simps(a, b, n)

%simps(a, b, n) approximates the integral of a function f(x) in the
%interval [a;b] by the composite simpson rule
%n is the number of subintervals

h = (b-a)/n;

sum_even = 0;

for i = 1:n/2-1
x(i) = a + 2*i*h;
sum_even = sum_even + f(x(i));
end

sum_odd = 0;

for i = 1:n/2
x(i) = a + (2*i-1)*h;
sum_odd = sum_odd + f(x(i));
end

integral = h*(f(a)+ 2*sum_even + 4*sum_odd +f(b))/3

function y = f(x)
y=1/x;

• May 2nd 2010, 03:07 AM
CaptainBlack
Quote:

Originally Posted by chronicals
I want to compute x within 0.1% relative error with Simpson method, these are my m-files. Which command i should add for this?

function
simps(a, b, n)

%simps(a, b, n) approximates the integral of a function f(x) in the
%interval [a;b] by the composite simpson rule
%n is the number of subintervals

h = (b-a)/n;

sum_even = 0;

for i = 1:n/2-1
x(i) = a + 2*i*h;
sum_even = sum_even + f(x(i));
end

sum_odd = 0;

for i = 1:n/2
x(i) = a + (2*i-1)*h;
sum_odd = sum_odd + f(x(i));
end

integral = h*(f(a)+ 2*sum_even + 4*sum_odd +f(b))/3

function y = f(x)
y=1/x;

You know the error bound for the composite Simpson's rule:

$\displaystyle |\varepsilon|<\frac{M(b-a)^5}{180n^4}$

where $\displaystyle M$ is a bound on the absolute value of the fourth derivative of $\displaystyle f(x)$ on $\displaystyle [a,b]$

Now use the information you have and this bound on the error to obtain a bound on the relative error as a function on $\displaystyle n$ ...

An alternative method is to use your function to estimate the integral for a number of values of $\displaystyle n$ and use these to determine when the relative error is small enough.

CB