| 
October 28th, 2009, 12:31 AM
| | Newbie | | Join Date: Oct 2009
Posts: 5
Country: Thanks: 1
Thanked 0 Times in 0 Posts
| | [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 06:49 AM.
| 
October 28th, 2009, 01:24 AM
|  | Grand Panjandrum | | Join Date: Nov 2005 Location: South of England
Posts: 11,379
Country: Thanks: 667
Thanked 3,619 Times in 2,916 Posts
| | 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).
Any advice? Thanks a lot. | Google for Simpson's rule
CB
__________________ Truth does not change because it is, or is not, believed by a majority of the people.
Giordano Bruno | 
October 28th, 2009, 02:50 AM
| | Newbie | | Join Date: Oct 2009
Posts: 5
Country: Thanks: 1
Thanked 0 Times in 0 Posts
| | 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! | 
October 28th, 2009, 07:55 AM
| | Member | | Join Date: Mar 2007
Posts: 104
Country: Thanks: 17
Thanked 14 Times in 14 Posts
| | 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 | 
October 28th, 2009, 09:40 AM
| | Newbie | | Join Date: Oct 2009
Posts: 5
Country: Thanks: 1
Thanked 0 Times in 0 Posts
| | 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
| 
October 28th, 2009, 01:10 PM
|  | Grand Panjandrum | | Join Date: Nov 2005 Location: South of England
Posts: 11,379
Country: Thanks: 667
Thanked 3,619 Times in 2,916 Posts
| | 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
__________________ Truth does not change because it is, or is not, believed by a majority of the people.
Giordano Bruno | 
October 29th, 2009, 04:09 AM
| | Newbie | | Join Date: Oct 2009
Posts: 5
Country: Thanks: 1
Thanked 0 Times in 0 Posts
| | 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. | 
October 29th, 2009, 05:14 AM
| | Member | | Join Date: Mar 2007
Posts: 104
Country: Thanks: 17
Thanked 14 Times in 14 Posts
| | 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 | | The following users thank elbarto for this useful post: | |  | 
October 29th, 2009, 06:21 AM
| | Newbie | | Join Date: Oct 2009
Posts: 5
Country: Thanks: 1
Thanked 0 Times in 0 Posts
| | 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 06:49 AM.
| 
October 29th, 2009, 07:09 AM
| | Member | | Join Date: Mar 2007
Posts: 104
Country: Thanks: 17
Thanked 14 Times in 14 Posts
| | 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 | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -7. The time now is 01:35 PM. | | |