Search code examples
matlabfill

fill function in Matlab 2017 does not work


I was trying to fill the area between a horizontal line and a curve. I am following the instructions in this. The 2 curves are

enter image description here

The code I am using to generate the graph is

alpha = [0:0.01:1];
mu = 1;
V = [0:0.01:10];
Lambda1 = 2;
y2 = (1./(mu-alpha*Lambda1)+2*alpha*Lambda1./((mu-alpha*Lambda1).^2)).*(alpha<=0.3)+max(V).*(alpha>0.3);
f0 = max(V).*ones(1,length(alpha));
A = [alpha,fliplr(alpha)];
Y1 = [f0,fliplr(y2)];     
figure
h1 = fill(A,Y1,'k'),axis([0 1 0 11]),
set(h1, 'FaceAlpha', 0.1)

The idea is that alpha is the running variable, i.e., the horizontal axis. The blue curve is a piecewise function, i.e., before alpha=0.3, it is a qudratic function, and after alpha=0.3, it stays at 10. Basically, I want to have the region when 0<=alpha<=0.3 be shaded.

I thought my above code would work, since I thought I understand how the fill function works. The x,y in fill(x,y,'r') are 2 vectors representing the coordinates of the boundary of a share in 2D space.

I don't know why in my case it does not work...


Solution

  • Your code is correct. But your function has a singularity for alpha=0.5: (mu-0.5*Lambda1)=0.

    So your y2(51)=NaN and Matlab -rightfully- screws up.