How to plot multiple lines from a for loop iteration?

Hi, does anyone know how to make this plot come out with a curve for every alpha iteration [-2 0 2 4 6 8]?
Where the plot and hold on are placed now, the plot is only giving me a curve for alpha = 8 the last iteration.
R = 27.1;
rho = 0.002036;
omega = 27.01;
T = 16000;
A = pi.*R.^2;
C_t = T ./ (rho.*A.*(omega.*R).^2);
lambda_h = sqrt(C_t./2);
lambda_o = [lambda_h];
lambda = 0.1;
ratios = [];
mu_vec = [];
alpha_vec = [];
figure
for alpha = -2:2:8
while abs((lambda-lambda_o)/lambda) > 0.001
for mu = 0:0.01:0.5
lambda_o = lambda_o(end);
lambda = mu.*tand(alpha) + (C_t./(2*sqrt(mu.^2 + lambda_o.^2)));
lambda_o = [lambda_o lambda];
mu_vec = [mu_vec mu];
ratio = lambda./lambda_h;
ratios = [ratios ratio];
plot(mu_vec, ratios)
hold on
end
end
end

1 comentario

Voss
Voss el 4 de Feb. de 2022
Editada: Voss el 4 de Feb. de 2022
I think you better rethink your while condition there (and probably the definition of lambda_o):
while abs((lambda-lambda_o)/lambda) > 0.001
When the while loop has executed at leat once, then lambda_o is a 1-by-2 vector, the second element of which is lambda (a scalar). Therefore the expression (lambda-lambda_o)/lambda is a 1-by-2 vector with second element equal to 0. A non-scalar conditional expression (e.g., a while condition with a vector) will evaluate to true if and only if all elements are true. Since the second element of your while expression is zero after the first time through the while loop (and 0 is not greater than 0.001), the while loop will never execute a second time.
Not to mention that, after the inital test, the while condition is only tested for mu = 0.5, which is to say, you probably should rethink the relationship between the while loop and the for mu loop as well.

Iniciar sesión para comentar.

 Respuesta aceptada

Maybe this?
R = 27.1;
rho = 0.002036;
omega = 27.01;
T = 16000;
A = pi.*R.^2;
C_t = T ./ (rho.*A.*(omega.*R).^2);
lambda_h = sqrt(C_t./2);
figure
for alpha = -2:2:8
lambda_o = [lambda_h];
lambda = 0.1;
ratios = [];
mu_vec = [];
% alpha_vec = [];
while abs((lambda-lambda_o)/lambda) > 0.001
for mu = 0:0.01:0.5
lambda_o = lambda_o(end);
lambda = mu.*tand(alpha) + (C_t./(2*sqrt(mu.^2 + lambda_o.^2)));
lambda_o = [lambda_o lambda];
mu_vec = [mu_vec mu];
ratio = lambda./lambda_h;
ratios = [ratios ratio];
end
end
plot(mu_vec, ratios)
hold on
end

1 comentario

Abby
Abby el 5 de Feb. de 2022
yes this is what i was looking for!! thank you so much

Iniciar sesión para comentar.

Más respuestas (2)

Davide Masiello
Davide Masiello el 4 de Feb. de 2022
Try to move hold on before plot.

3 comentarios

Abby
Abby el 4 de Feb. de 2022
I've done that and it didnt work :(
Could you share a more complete version of the code with the values of lambda, mu, lambda_o, mu_vec etc. so I can just copy-paste and run it on my pc?
Abby
Abby el 4 de Feb. de 2022
I just updated the original posting, thank you!!

Iniciar sesión para comentar.

Jan
Jan el 4 de Feb. de 2022
Editada: Jan el 4 de Feb. de 2022
You do create a lot of plots, but all have the same value so they conceal eachother.
A small change to demonstrate this:
R = 27.1;
rho = 0.002036;
omega = 27.01;
T = 16000;
A = pi.*R.^2;
C_t = T ./ (rho.*A.*(omega.*R).^2);
lambda_h = sqrt(C_t./2);
lambda_o = [lambda_h];
lambda = 0.1;
ratios = [];
mu_vec = [];
alpha_vec = [];
figure;
axes('NextPlot', 'add'); % Same as: hold on
counter = 0;
step = 0.1;
for alpha = -2:2:8
while abs((lambda-lambda_o)/lambda) > 0.001
for mu = 0:0.01:0.5
lambda_o = lambda_o(end);
lambda = mu.*tand(alpha) + (C_t./(2*sqrt(mu.^2 + lambda_o.^2)));
lambda_o = [lambda_o lambda];
mu_vec = [mu_vec mu];
ratio = lambda./lambda_h;
ratios = [ratios ratio];
% Add a small vertical shift to show the stack the set
% of lines:
plot(mu_vec, ratios + counter * step);
counter = counter + 1;
end
end
end
This line looks strange:
while abs((lambda-lambda_o)/lambda) > 0.001
If the innerloop produces the matching value, which is smaller equal 0.001, the nody of the outer loop is not entered anymore. Is this wanted?

Categorías

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

Etiquetas

Preguntada:

el 4 de Feb. de 2022

Comentada:

el 5 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by