# Matlab: Integration Approximation

• Nov 11th 2010, 09:51 PM
bambamm
Matlab: Integration Approximation
I need to devise an integration method for computing the values of the function

F(x) =int(exp(-x^2)), a and b are 0 and x, respectively.
For this fixed value of x, I must demonstrate how the error in the integral changes with h using a plot.
------------------------------------------

Here is my code:

function face = trapez(b)
%Trapezoidal rule for solving the integral of exp(-x^2) between a and b.
n=linspace(10,1,100);
a=0;
hs=(b-a)./n;
fa=exp(-0^2);
fb=exp(-b^2);
es=[]
for h=hs
int=(h/2)*(fa+fb); %Trapezoid Formula
int0=(10^(-10)/2)*(fa+fb)
e=abs(int-int0);
es=[es,e];
end
face=es;

In this code I set the "real value" of the integral of the function as having a very small h and then I plot the function. Then I made a plot of the function vs. increasing h.

n=linspace(10,1,100);
a=Trapez(3);
hs=3./n
loglog(hs,a)

The problem is the graph came out having a linear error curve, which just doesn't seem right. Should the error fluctuate like one's heart rate? Is there a problem with my code? Thanks in advance!
• Nov 11th 2010, 10:10 PM
CaptainBlack
Quote:

Originally Posted by bambamm
I need to devise an integration method for computing the values of the function

F(x) =int(exp(-x^2)), a and b are 0 and x, respectively.
For this fixed value of x, I must demonstrate how the error in the integral changes with h using a plot.
------------------------------------------

Here is my code:

function face = trapez(b)
%Trapezoidal rule for solving the integral of exp(-x^2) between a and b.
n=linspace(10,1,100);
a=0;
hs=(b-a)./n;
fa=exp(-0^2);
fb=exp(-b^2);
es=[]
for h=hs
int=(h/2)*(fa+fb); %Trapezoid Formula
int0=(10^(-10)/2)*(fa+fb)
e=abs(int-int0);
es=[es,e];
end
face=es;

In this code I set the "real value" of the integral of the function as having a very small h and then I plot the function. Then I made a plot of the function vs. increasing h.

n=linspace(10,1,100);
a=Trapez(3);
hs=3./n
loglog(hs,a)

The problem is the graph came out having a linear error curve, which just doesn't seem right. Should the error fluctuate like one's heart rate? Is there a problem with my code? Thanks in advance!

1. Comment your code so that we can see what you think it is doing.

2. You have not used the trapezoid rule.

CB