Results 1 to 10 of 10

Math Help - Matlab, find area under experimental plot

  1. #1
    Newbie
    Joined
    Oct 2009
    Posts
    5

    [Solved] Matlab, find area under experimental plot

    Hi all,

    My first time here, greetings to everyone.

    Now I have an issue I need help from. I think this is a common problem, but I can't find the solution in forum search.

    I have a plot y against t with t from 0 to 30 interval 0.5 and now I want to find area under plot where, for example, t = 0.184 to 5.623. How can I do this? (i am quite noob to matlab).

    I read about trapz and quads, but seems like they need the function to be defined. since mine is experimental data, i don't have the function. the purpose of calculating the area is to approximate/find the function f(t).

    Any advice? Thanks a lot.
    Last edited by jakesee; October 29th 2009 at 05:49 AM.
    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 jakesee View Post
    Hi all,

    My first time here, greetings to everyone.

    Now I have an issue I need help from. I think this is a common problem, but I can't find the solution in forum search.

    I have a plot y against t with t from 0 to 30 interval 0.5 and now I want to find area under plot where, for example, t = 0.184 to 5.623. How can I do this? (i am quite noob to matlab).

    I read about trapz and quads, but seems like they need the function to be defined. since mine is experimental data, i don't have the function. the purpose of calculating the area is to approximate/find the function f(t).

    Any advice? Thanks a lot.
    Google for Simpson's rule

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Oct 2009
    Posts
    5
    yea thanks, but sorry, it didn't really help. I need a matlab solution.

    i did see some examples using simpson() but, it didn't work on my matlab. maybe i don't have the toolbox or something? then again, judging from the examples for simpsons, seems like the function is required to be known.

    any other functions i can use in matlab?

    PS: I am trying to do an engineering problem, so pardon me if my maths is not that up to the forum's expectations. thanks!
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    I haven't tried it yet but this looks promising.

    MATLAB Central - File detail - Inverse (integrated) gradient

    intgrad1 specifically is the function I think you are chasing.

    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Oct 2009
    Posts
    5
    Thanks.

    But seriously, I am a matlab noob. Reading the matlab documentation is like looking up a tamil dictionary. Searching for the definition of one tamil word only returns even more tamil words.. all gibberish to me. I'm sorry for being bothersome.

    Now this is the documentation I got from downloading the intgrad1 file. I have no idea what it is talking about.

    But at the end there is an example
    Code:
    % Example usage: 
    %  x = 0:.001:1;
    %  f = exp(x) + exp(-x);
    %  dfdx = exp(x) - exp(-x);
    %  tic,fhat = intgrad1(dfdx,.001,2,2);toc
    here it shows that f(x) = known = exp(x) + exp(-x)

    Is there a way to specify f(x) in terms for the data points?

    If plot() can do it I don't see why there isn't a way to do something like

    Code:
     
    % psuedo code
    f = plot(x, y);
    A = integrate(f);
    This has to be a common task! Would appreciate if can get further help. Thanks in advance!

    Code:
     
    % intgrad: generates a vector, integrating derivative information.
    % usage: fhat = intgrad1(dfdx)
    % usage: fhat = intgrad1(dfdx,dx)
    % usage: fhat = intgrad1(dfdx,dx,f1)
    % usage: fhat = intgrad1(dfdx,dx,f1,method)
    %
    % arguments: (input)
    %  dfdx - vector of length nx, as gradient would have produced.
    %
    %    dx - (OPTIONAL) scalar or vector - denotes the spacing in x
    %         if dx is a scalar, then spacing in x (the column index
    %         of fx and fy) will be assumed to be constant = dx.
    %         if dx is a vector, it denotes the actual coordinates
    %         of the points in x (i.e., the column dimension of fx
    %         and fy.) length(dx) == nx
    %
    %         DEFAULT: dx = 1
    %
    %    f1 - (OPTIONAL) scalar - defines the first eleemnt of fhat
    %         after integration. This is just the constant of integration.
    %
    %         DEFAULT: f1 = 0
    %
    %  method - (OPTIONAL) scalar - either 0, 1, 2, or 3. Defines
    %         the integration scheme used.
    %
    %         method = 0 --> cumtrapz
    %
    %         method = 1 --> solves central finite difference
    %                        approximation using linear algebra
    %                        A second order fda. At least 3 points
    %                        are necessary.
    %
    %         method = 2 --> integrated spline model
    %                        This will almost always be the most
    %                        accurate among the alternative methods.
    %
    %         method = 3 --> integrated pchip model
    %
    %         method = 4 --> higher order finite difference approximation
    %                        A 4th order fda. At least 5 points are
    %                        necessary.
    %
    %         DEFAULT: method = 2
    %
    %         Note: method = 0 (cumtrapz) will generally be the fastest,
    %         and method = 2 (spline integral) will be the most accurate
    %         of the four methods.
    %         Methods 1, 3, and 4 were put in there mainly for fun on my
    %         part, lthough for equally spaced points, the 4th order fda
    %         should also be quite accurate.
    %
    %         Data series with noise in them may be best integrated using
    %         a lower order method to avoid noise amplification.
    %
    % arguments: (output)
    %   fhat - vector of length nx, containing the integrated function
    %
    % Example usage: 
    %  x = 0:.001:1;
    %  f = exp(x) + exp(-x);
    %  dfdx = exp(x) - exp(-x);
    %  tic,fhat = intgrad1(dfdx,.001,2,2);toc
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by jakesee View Post
    yea thanks, but sorry, it didn't really help. I need a matlab solution.

    i did see some examples using simpson() but, it didn't work on my matlab. maybe i don't have the toolbox or something? then again, judging from the examples for simpsons, seems like the function is required to be known.

    any other functions i can use in matlab?

    PS: I am trying to do an engineering problem, so pardon me if my maths is not that up to the forum's expectations. thanks!
    Have you considered writing some code yourself??

    CB
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Oct 2009
    Posts
    5
    Have you considered writing some code yourself??
    I wish I could, but unfortunately, I don't think I know how to do it. My assumption was that there must be some built in function to do just what I need because I think this is really a common ... calculation, no?

    Furthermore, I was told to "... use matlab for calculating the area ..." but without further guidance, I couldn't do it.

    something like this would be perfect (psuedocode)
    f = plot(t, y)
    A = integrate(f, t1, t2) or
    A = simspons(f, t1, t2) or
    A = trapz(f, t1, t2)

    Don't understand why it has to be so complicated just to calculate area *sigh*

    But at this rate I think I am wrong afterall, and my only option is to draw the graph in excel then start counting squares on the graph paper.
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    Doe this simple implementation of the trapezoidal rule work for you.
    Code:
    function I = jrbTrap(x,y)
    Y = (y(2:end)+y(1:end-1))/2;
    X = x(2:end)-x(1:end-1);
    I = sum(X.*Y);
    Code:
    EDU>> x = sort([0 rand(1,9)*10 10]);
    EDU>> y = x.^2;
    EDU>> I = jrbTrap(x,y)
    
    I =
    
      337.3206
    
    EDU>>
    The answer should of course be 333.333.


    Elbarto
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Newbie
    Joined
    Oct 2009
    Posts
    5
    Thanks elbarto!

    That's what I need! Now I can understand your code!

    But I think the approximation is not good enough, plus I need area within an interval instead of the whole plot.

    I try to tweak the function abit. Will come back after trying. Thanks!

    EDIT: Thanks elbarto, my problem is solved. Your little code snippet helped me understand how matlab actually works and how to write code to run the calculations.
    Last edited by jakesee; October 29th 2009 at 05:49 AM.
    Follow Math Help Forum on Facebook and Google+

  10. #10
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    Glad I could help you out. There are a few different ways you could implement this function but the one I provided is more like the "MATLAB" away of doing things as you can avoid using loops by taking advantage of vectorized code. Good luck.

    Elbarto
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 1
    Last Post: July 30th 2011, 09:47 PM
  2. 3D plot of z = (Matlab)
    Posted in the Math Software Forum
    Replies: 4
    Last Post: July 14th 2011, 09:22 PM
  3. Help with surface plotting experimental data (Matlab)
    Posted in the Math Software Forum
    Replies: 1
    Last Post: August 3rd 2009, 06:02 AM
  4. [Matlab] 2D Sequence Plot in 3D
    Posted in the Math Software Forum
    Replies: 2
    Last Post: April 23rd 2009, 05:28 AM
  5. MatLab Plot
    Posted in the Math Software Forum
    Replies: 1
    Last Post: August 2nd 2008, 11:59 AM

Search Tags


/mathhelpforum @mathhelpforum