MATLAB: If/elseif for ranges of t

elseifif statementMATLAB

Hi, I am trying to add formula so that phi and v2 change according to the value of t. Can anyone see why the values aren't changing as t varies? This section of code is situated within a for loop for t = 1:91. v1 is a value previously assigned to 0.0066.
if (t < 7.5)
v2 = 0.000182;
phi = 0.01;
elseif (t >= 7.5) && (t < 14.5)
v2 = 0.00124;
phi = 0.01;
elseif (t >= 14.5) && (t < 28.5)
v2 = 0.000737;
phi = 0.008;
elseif (t >= 28.5) && (t < 35.5)
v2 = 0.00228;
phi = 0.006;
elseif (t >= 35.5) && (t < 42.5)
v2 = 0.00385;
phi = 0.004;
else
v2 = v1;
phi = 0.002;
end

Best Answer

I would do something like this:
v2fcn = @(t) 0.000182*(t < 7.5) + 0.00124*((t >= 7.5) & (t < 14.5)) + 0.000737*((t >= 14.5) & (t < 28.5)) + 0.00228*((t >= 28.5) & (t < 35.5)) + 0.00385*((t >= 35.5) & (t < 42.5)) + 0.0066*(t >= 42.5);
phifcn = @(t) 0.01*(t < 7.5) + 0.01*((t >= 7.5) & (t < 14.5)) + 0.008*((t >= 14.5) & (t < 28.5)) + 0.006*((t >= 28.5) & (t < 35.5)) + 0.004*((t >= 35.5) & (t < 42.5)) + 0.002*(t >= 42.5);
f = @(t) [v2fcn(t); phifcn(t)];
t = 1:91;
figure
plot(t, f(t))
legend('v_2','\phi')
xlabel('t')
ylabel('Value')
ylim([0 0.0101])
No if block needed. You can combine them both in the matrix rather than defining them separately first.