MATLAB: Conditional colouring graph problem

colorcoloringcolourcolouringconditionconditionallygraphgraphs

Hey, I have a problem with changing the colours on my graph conditionally; my function is as follows
function [t,vx,vy] = Q3_1400000(rocket,brake)
[t,ax,ay] = getAcceleration(rocket,brake);
vx = 0; vy = 0;
for n = 1:999;
vx(n+1) = vx(n) + ax(n+1);
vy(n+1) = vy(n) + ay(n+1);
k = 1:length(vx);
if vy(k) > vx(k)
c = 'r';
else
c = 'b';
end
end
plot(t,vx(k),c);
hold on
grid on
plot(t,vy(k),c);
xlabel('Time (s)');
ylabel('Velocity (m/s)');
title('\Velocity data for the rocket sled');
legend({'v_x','v_y'})
end
For some reason when the graph plots, the colour will always be the ' else ' colour and won't change at all. Does anybody know the solution to this please ?
Thanks

Best Answer

It looks like you're only testing the condition of whether the very last value in vy is bigger than the very last value in vx because each time it goes through the loop it's overwriting c.