How to shade and calculate area above a reference line in a plot?

11 ビュー (過去 30 日間)
Bhaskar Ravishankar
Bhaskar Ravishankar 2020 年 3 月 13 日
編集済み: Robert U 2020 年 3 月 17 日
Hello,
In the attached plot, I want to shade the area ONLY ABOVE the 0-crossing line without losing the curve below the 0-line.
I also want to calculate the area above the 0-crossing line.
I searched the community for answers and found several solutions to similar questions but it was not specific to this issue.
Could someone please help me? I would very much appreciate a code snippet to help solve this issue.
Thank you.

回答 (1 件)

Robert U
Robert U 2020 年 3 月 13 日
Hi Bhaskar Ravishankar,
You can use area and logical indexing in order to shade the curve:
resX = 0.1;
xlim = [-pi,pi];
x = xlim(1):resX:xlim(2);
y = 1 * sin( x );
fh = figure;
ah = axes(fh);
hold(ah,'on');
ah.XGrid = 'on';
ah.XMinorGrid = 'on';
ah.YGrid = 'on';
ah.YMinorGrid = 'on';
plot(ah,x,y,'-black');
area(ah,x(y>=0),y(y>=0)); % in order to suppress the visual uncertainties you would have to split the area, or make sure to hit the zero crossings.
Integral of the area with non-negative values:
intArea = sum(y(y>=0)*resX);
Kind regards,
Robert
  2 件のコメント
Bhaskar Ravishankar
Bhaskar Ravishankar 2020 年 3 月 16 日
Hi Robert,
I tried this technique with my code and I saw that the -ve portion of my graph disappears when I use area the area command. I'm not able to figure out why. Any suggestions?
I used the 'hold on' command after I plotted the graph before the area command but it gave me completely different results. Not sure what happened there.
The x limits were the same. But it didn't work.
Robert U
Robert U 2020 年 3 月 17 日
編集済み: Robert U 2020 年 3 月 17 日
Probably, you have not enough data points available. Try to remesh your xdata, interpolate the y-data using linear interpolation and then use area command on your interpolated data.
If you want more than generic help, post your code and data.
Kind regards,
Robert

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by