if/elseif for ranges of t

7 views (last 30 days)
Samuel Boulger
Samuel Boulger on 5 Mar 2021
Commented: Star Strider on 12 Mar 2021
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

Accepted Answer

Star Strider
Star Strider on 5 Mar 2021
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.
  2 Comments
Star Strider
Star Strider on 12 Mar 2021
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!