Results 1 to 2 of 2

Math Help - Matlab: Integration Approximation

  1. #1
    Newbie
    Joined
    Nov 2009
    Posts
    15

    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!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by bambamm View Post
    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
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: January 3rd 2012, 10:53 AM
  2. Double Integration Matlab
    Posted in the Math Software Forum
    Replies: 0
    Last Post: January 17th 2011, 02:40 AM
  3. Numerical approximation for Integration
    Posted in the Calculus Forum
    Replies: 3
    Last Post: August 20th 2010, 04:56 AM
  4. integration matlab qn
    Posted in the Math Software Forum
    Replies: 4
    Last Post: May 8th 2010, 11:00 PM
  5. Matlab - Least squares approximation of f(x) = e^x
    Posted in the Math Software Forum
    Replies: 5
    Last Post: April 2nd 2010, 01:21 AM

Search Tags


/mathhelpforum @mathhelpforum