Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Trapezoidal Rule and Error?

Subject: Trapezoidal Rule and Error?

From: Ryan

Date: 12 Nov, 2010 05:49:04

Message: 1 of 3

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!

Subject: Trapezoidal Rule and Error?

From: Roger Stafford

Date: 12 Nov, 2010 07:29:05

Message: 2 of 3

"Ryan " <riley127099@hotmail.com> wrote in message <ibikgg$2ba$1@fred.mathworks.com>...
> ......
> for h=hs
> int=(h/2)*(fa+fb); %Trapezoid Formula
> int0=(10^(-10)/2)*(fa+fb)
> e=abs(int-int0);
> es=[es,e];
> end
> ......
- - - - - - - - - -
  If the code you show is what you actually used, it is incorrect. The quantities you call fa and fb should be changing in value at each step in the for-loop. As they stand, they remain fixed at exp(-a^2) and exp(-b^2) at every step. You need to have fa and fb be the varying values of exp(-x^2) at opposite ends of each interval of length h as you progress in small steps along from a to b. Then you need to take the sum of all these terms to get the approximate trapezoidal approximation for the entire interval from a to b. You then repeat this for many values of h to see how the error varies with changing h. You can use the value you calculate for extremely small h as the correct value to compare with (or else known values of the normal cumulative distribution function.)

  To an approximation, the errors made in trapezoidal integration are given by the area between a chord line between two points on a curve and a circle connecting the points with the radius of the curve's curvature there. This is equal to r^2/2*(phi-sin(phi)) where r is the radius and phi the projected angle. For small phi this is approximately r^2/12*phi^3. If you halve the interval h, you approximately halve each projected phi and this gives one-eighth the error for each step, and there are twice as many steps. Therefore the total error ought to be about one-fourth as great if the h-interval is halved. In other words the error should vary as the square of the interval length as that length approaches zero.

Roger Stafford

Subject: Trapezoidal Rule and Error?

From: SteeleLatisha22

Date: 31 Dec, 2010 22:07:02

Message: 3 of 3

If you want to buy a house, you would have to receive the <a href="http://bestfinance-blog.com/topics/credit-loans">credit loans</a>. Moreover, my brother commonly uses a collateral loan, which seems to be the most useful.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us