Calculating area under curve from simulink

83 visualizaciones (últimos 30 días)
Peter Lyngbye
Peter Lyngbye el 16 de Dic. de 2022
Respondida: Santosh Fatale el 21 de Dic. de 2022
I have a curve simulink that i have used the "to workspace" block on.
Now i got the curve in workspace but how can i find the area under the curve between two X(time) points?
And is there a way to tell how many "points" u integrate, meaning how many points there actually are used to get the area.

Respuestas (1)

Santosh Fatale
Santosh Fatale el 21 de Dic. de 2022
Hi Peter,
You can calculate the area under the curve using the "trapz" function. To find the area over a finite interval and the non-unit spacing between data points, use following syntax:
Area = trapz(X,Y)
Here input X is the point spacing (x-coordinates) for data points present in input Y.
For the variable created from Simulink, the point spacing is equivalent to sample time used in Simulink. You could get sample times for the curve from variable "tout". If variable is created as time series variable, then the first column represents data point spacing with respect to time.
Following code demonstrates this:
>> openExample('simulink/OutputSimulationDataWithBlocksExample')
Create a breakpoint at Line 30 and execute the code till breakpoint. You could see "out" variable in workspace. Execute following command in command window.
>> varCurve = out.simoutToWorkspace;
>> X = varCurve.Time;
>> Y = varCurve.Data;
>> Area = trapz(X(1:11), Y(1:11))
Area =
Note that for accurate area, you need higher samples in interested interval.
To fix the interval for integration, choose suitable input X and corresponding data points Y.
Refer the documentation of “trapz” function and "timeseries" for more information about numerical integration.


Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by