0
votes

I was wondering if anyone knew how I could colour the area under the curve for the below code. I have tried to use area(Mu), however this returns a shaded area slightly different to the plot.

n=1;
a=[0:0.001:4];
A=(a.*n);
B=(n-n.*a);
C=(1-n);
Mu=(-B+sqrt((B.^2)-(4.*A.*C)))./(2.*A);
plot(a,Mu)
xlim([0 4])
ylim([0 .8])
1
It's straight-forward... Just replace plot with area which gives you the line area(a,Mu). This can be found in the documentation like this: «area(X,Y) For vectors X and Y, area(X,Y) is the same as plot(X,Y) except that the area between 0 and Y is filled.»Matt
@Matt removing the plot call is not the correct generic answer if the user wants to do something "special" with the boundary. The OP has tried area, so it's not like she's unaware that the function exists...excaza
@excaza Of course, if the OP needs the plot, she needs to call area after hold on or first plot the area and then the line. She tried area(Mu) but not area(a,Mu)... That's the reason why used the word replace to emphasize that the arguments are exactly the same. The citation corresponds to that as well.Matt
@Matt telling them to remove functions from their code unrelated to the asked question, and therefore harming their function, is not as helpful as explaining to them why what they already tried didn't work as they expected.excaza
@excaza I do not agree that it harms the function because I assume the OP knows how to draw the line again if needed. The only thing I wanted to express with my first comment is basically to use the same arguments for area as for plot. I agree that I could have explained why it caused the wrong result. Namely because the x-axis does not match. BTW: I agree as well to this comment.Matt

1 Answers

4
votes

You're almost there. area(Mu) uses a default value such that it, in this case, actually executes area([1:length(Mu)],Mu). This gives you the undesired result, whilst area(a,Mu) is the same as plot(a,Mu) except that the area between 0 and Mu is filled.

Use area(a,Mu). Then plot your line on top of the area, or just change the edge of the area. See example below:

n=1;
a=[0:0.001:4];
A=(a.*n);
B=(n-n.*a);
C=(1-n);
Mu=(-B+sqrt((B.^2)-(4.*A.*C)))./(2.*A);
area(a,Mu)
hold on
xlim([0 4])
ylim([0 .8])
plot(a,Mu)