Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculation of area in plots

Status
Not open for further replies.

CrisJack

Mechanical
Joined
Jul 28, 2005
Messages
2
Location
AU
Hi,
I'm using MATLAB 6.1 and want to calculate the area between the plot of a vector (with given values for the components) and the x-axis. I tried the quad command, but that's for functions only. Also the polyarea command doesn't seem to calculate the area correctly.
Can anyone tell me the, if there is a different command available?
Cheers,
Chris
 
What's wrong with a straight numerical integration?

TTFN



 
Hi,
the numerical integration command (quad) needs a function or an string as input (but I only have the values of the vector (e.g. 5 60 45 3 8 etc.)). I programmed Simpsons rule:
%int=1/3*h*[fo+4*(f1+f3+...+f2n-1)+2*(f2+f4+...+f2n-2)+f2n]
and the results seem quite reasonable (about 5% error for several test functions).
I'm quite sure that there's also a MATLAB command for vectors (and not only for functions) available, I just don't know where to find it...
cheers,
Chris
 
% One solution
area=sum(y(1:end-1)+y(2:end).*diff(x))/2;

% Another solution
area=trapz(x,y);

One function you may not be aware of is the always usefull "lookfor".

>> lookfor integration
CUMTRAPZ Cumulative trapezoidal numerical integration.
TRAPZ Trapezoidal numerical integration.
lotkademo.m: %% Numerical Integration of Differential Equations
ADAMS Simulink 1.x ADAMS integration algorithm.
EULER Simulink 1.x EULER integration algorithm.
GEAR Simulink 1.x GEAR integration algorithm.
LINSIM Simulink 1.x LINSIM integration algorithm.
RK23 Simulink 1.x rk23 integration algorithm.
RK45 Simulink 1.x RK45 integration algorithm.
SFUNMEM A one integration-step memory block S-function.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top