# Matlab, find area under experimental plot

• Oct 27th 2009, 11:31 PM
jakesee
[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).

• Oct 28th 2009, 12:24 AM
CaptainBlack
Quote:

Originally Posted by jakesee
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).

CB
• Oct 28th 2009, 01:50 AM
jakesee
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!
• Oct 28th 2009, 06:55 AM
elbarto
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
• Oct 28th 2009, 08:40 AM
jakesee
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.

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! (Headbang) 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```
• Oct 28th 2009, 12:10 PM
CaptainBlack
Quote:

Originally Posted by jakesee
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
• Oct 29th 2009, 03:09 AM
jakesee
Quote:

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.
• Oct 29th 2009, 04:14 AM
elbarto
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
• Oct 29th 2009, 05:21 AM
jakesee
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.
• Oct 29th 2009, 06:09 AM
elbarto
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