How to plot multiple lines from a for loop iteration?

21 次查看(过去 30 天)
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 个评论
Voss
Voss 2022-2-4
编辑:Voss 2022-2-4
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.

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-2-4
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

更多回答(2 个)

Davide Masiello
Davide Masiello 2022-2-4
Try to move hold on before plot.
  3 个评论
Davide Masiello
Davide Masiello 2022-2-4
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?

请先登录,再进行评论。


Jan
Jan 2022-2-4
编辑:Jan 2022-2-4
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?

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by