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

 Respuesta aceptada

Star Strider
Star Strider el 5 de Mzo. de 2021

0 votos

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 comentarios

Samuel Boulger
Samuel Boulger el 12 de Mzo. de 2021
Thank you!
Star Strider
Star Strider el 12 de Mzo. de 2021
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 5 de Mzo. de 2021

Comentada:

el 12 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by