MATLAB: How to put the plot starting in zero

plot

Hey guys, I'm new around here so I'm sorry if I'm asking something obvious… I'm trying to make a plot but it doesn't show me the initial values, between 0 and 2. I have already read many tutorials but I'm not getting there.. hope you can help me, here is the code:
P1=2;
P2=10;
P3=50;
C1=1.5;
for p=P1:P2
C2=1.5+((p-P1)*0.5);
end
for p=P2:P3
C3=5.5+((p-P2)*0.3);
end
x=[??;linspace(2,10,1);linspace(10,50,1)]; (here is the problem..)
y=[C1;C2;C3];
plot(x,y,'g')
Thanks in advance 🙂 cheers!

Best Answer

There were several fundamental issues with your code. I fixed them.
P1=2;
P2=10;
P3=50;
C1=1.5;
C2=1.5+(((P1:P2)-P1)*0.5);
C3=5.5+(((P2:P3)-P2)*0.3);
x=[1 linspace(2,10,length(P1:P2)) linspace(10,50,length(P2:P3))];
y=[C1 C2 C3];
plot(x,y,'g')
Compare my code with yours, side-by-side, and try to identify all the differences.
In particular, notice that I did not need to use for loops to calculate C2 and C3.