Calculating area under curve from simulink

조회 수: 110 (최근 30일)
Peter Lyngbye
Peter Lyngbye 2022년 12월 16일
답변: Santosh Fatale 2022년 12월 21일
Hello,
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.

답변 (1개)

Santosh Fatale
Santosh Fatale 2022년 12월 21일
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 =
13.7823
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.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by