Calculating area under curve from simulink
35 views (last 30 days)
Show older comments
Peter Lyngbye on 16 Dec 2022
Answered: Santosh Fatale on 21 Dec 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.
Santosh Fatale on 21 Dec 2022
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:
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))
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.
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!