3

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?

enter image description here

EBH
  • 10,350
  • 3
  • 34
  • 59
MatlabNewb
  • 757
  • 1
  • 6
  • 11
  • 2
    Possible duplicate of [Area under surface between two curves](http://stackoverflow.com/questions/38409981/area-under-surface-between-two-curves) – Matt Jul 17 '16 at 13:33
  • Yes I modified the plot in order to give a better understanding of my problem. If I modify the old question, will people see it? – MatlabNewb Jul 17 '16 at 13:54
  • Yes, people can see it because it will bump the question to the top of the *active* list on the homepage. See [this](http://meta.stackoverflow.com/questions/313528/does-the-question-will-be-bumped-to-top-of-queue-when-it-get-edited) Meta post and [this](http://stackoverflow.com/help/privileges/edit) page in the [help] where is written: "*Editing a post also bumps the question to the top of the homepage. [...]*". Note that asking the same question twice is discouraged and mostly not well-received by the community. – Matt Jul 17 '16 at 14:25
  • 2
    Thanks for the hint @Matt ! Sorry for that. Will keep that in mind for future questions. – MatlabNewb Jul 17 '16 at 14:33
  • If the blue line is asymptotic as x->0 then you will get area = infinity, unless you bound it also from the top. – EBH Jul 17 '16 at 18:26
  • Hi @EBH it is bounded but you cannot see it. I actually got the intersection point of the blue curve with the y-axis in my workspace, so that is fine :-) Any hints? – MatlabNewb Jul 17 '16 at 18:29

1 Answers1

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: Integrating between y1 and y2

Tell me is that solves the problem ;)

Community
  • 1
  • 1
EBH
  • 10,350
  • 3
  • 34
  • 59
  • Thank you very much! I saw that I failed to set up the conditions of the intersection properly! Now it is very obvious, especially thanks to your code! Much appreciated mate! :) – MatlabNewb Jul 17 '16 at 20:06
  • 1
    The units of your `y` axis, since you integrate over `x`. – EBH Jul 17 '16 at 21:09
  • One last question. I tried to get the same results for the function but just turned. https://s31.postimg.org/mc3ym1omz/Zwischenablage01.jpg But I got an error or better to say StartLine and EndLine are both 1 in the Workspace. Do you know what mistake I might have made? Your help is much appreciated! – MatlabNewb Jul 18 '16 at 08:57
  • The code `lineEnd = find(y1<=y2,1)` assumes that `y1` starts above `y2`, and so it's finding the first intersect of them. If `y2` already starts above `y1` then you get `lineEnd = 1` as a result, and the length to integrate on is `0` - this is why you get the error. – EBH Jul 18 '16 at 09:33
  • Thanks for making that clear. I tried to modify the lines so that `lineStart = find(x<=100,1);` (because 100 is the max x-value in my case) and `lineEnd = find(y1>=y2,1);` and I think that is the mistake. But logically I just tell Matlab to find where y1 is bigger than y2. But unfortunately it does not work. Any ideas? Thank you! – MatlabNewb Jul 18 '16 at 09:57
  • @MatlabNewb this is becoming to messy, and I find it hard to follow your final goal. I suggest that you'll edit your question or write another one to meet your needs in a more general way (i.e. you have more than one function), and then it is possible to write a script that preform what you need. Specifically to what you wrote, I think you swapped `lineStart` and `lineEnd`. – EBH Jul 18 '16 at 11:02