Calculation of area in plots
Calculation of area in plots
(OP)
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
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





RE: Calculation of area in plots
TTFN
RE: Calculation of area in plots
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
RE: Calculation of area in plots
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.