阴影并计算特定区域

时间:2016-07-17 11:16:42

标签: matlab plot matlab-figure area curve

我尝试以某种方式更改代码,以便只有第一个区域以灰色阴影显示。如何设置水平线,使其仅显示在我想要阴影的区域下?

此外,我想计算一个地区的面积。我如何实现这一目标?我知道它是trapz,但我不确定如何设置边界。谢谢!

x = 0:.01:4*pi;  %// x data
y = sin(x);      %// y data
level = 0.5;     %// level
plot(x, y)
hold on
area(x, max(y, level), level, 'EdgeColor', 'none', 'FaceColor', [.7 .7 .7])

曲线: -
Curve

2 个答案:

答案 0 :(得分:3)

您也可以尝试这个简单的选项:

x = 0:.01:4*pi;  %// x data
y = sin(x);      %// y data
level = 0.5;     %// level
lineStart = find(y>=level,1);
lineEnd = find(y(lineStart:end)<=level,1)+lineStart;
plot(x,y)
hold all
area(x(lineStart:lineEnd),y(lineStart:lineEnd),...
     level,'EdgeColor', 'none', 'FaceColor', [.7 .7 .7],'ShowBaseLine','off')
line([x(lineStart),x(lineEnd)],[level level ])
hold off

没有定义感兴趣的区域a-priory: Filling area

不要忘记hold off ...

要区域化: A = trapz(x(lineStart:lineEnd),y(lineStart:lineEnd))

答案 1 :(得分:2)

您可以将area绘图中x轴的范围限制为感兴趣的范围,例如:从0到4,然后计算此范围内函数的结果值。对于基线:您可以在area命令中隐藏它并使用line命令手动添加它。

x = 0:.01:4*pi;  %// x data
y = sin(x);      %// y data
level = 0.5;     %// level
plot(x, y)
hold on

x_interest = 0:.01:4;
y_interest = sin(x_interest);
area(x_interest, max(y_interest, level), level, ...
    'EdgeColor', 'none', 'FaceColor', [.7 .7 .7], ...
    'ShowBaseLine', 'off');
line( [ min(x_interest) max(x_interest) ], [ level level ] )

enter image description here