Area between line and curve (no function)

淺唱寂寞╮ 提交于 2019-12-02 01:14:21

问题


I want to calculate the area beween the orange and the blue line. I managed to shade the area. But I do not know how to apply the trapz function in order to get the area. In this post: Area under surface between two curves I got some solutions but I do not have a specific equation for the curves just the plots per se.

The code for the orange line is:

x_1 = [0,M1_1];
y_1 = [c1,c1];
v = plot(x_1,y_1,'LineWidth',2)

The blue curve is a plot of arrays with the length of (10000x1)-abscissa and (1x10000)-ordinate.

If I use

%c0_1: Intersection blue curve with y-axis
%c1_1: Intersection orange curve with y-axis
A = trapz(ab1(0:c1_1),ab_y1(c1_1:c0_1))

I get the following error:

Warning: Integer operands are required for colon operator when used as index Warning: Integer operands are required for colon operator when used as index Error using trapz (line 58) LENGTH(X) must equal the length of Y in dim 2.

How can I apply my trapz function easily on my problem?


回答1:


Here is an answer, although I'm not sure what is the difference between the situation here and here, and therefore I'm not sure it truelly answers your question...

Anyway, you don't need to know y1 function explicitly, just to have its' series of data.

x = 0:0.1:12;       % x data
y1 = 3*exp(-0.5*x); % y data
y2 = 0.5;
lineStart = find(x>=0,1);
lineEnd = find(y1<=y2,1);
f = plot(x,y1,'b',x,ones(1,length(x))*y2,'r','LineWidth',2);
ylim([0 4])
hold on
area(x(lineStart:lineEnd),y1(lineStart:lineEnd), y2,...
    'EdgeColor', 'none', 'FaceColor', [0.5 0.5 1],'ShowBaseLine','off')
hold off
A = trapz(x(lineStart:lineEnd),y1(lineStart:lineEnd));

I added also the illustration of the integrated area:

Tell me is that solves the problem ;)



来源:https://stackoverflow.com/questions/38421441/area-between-line-and-curve-no-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!