How to plot multiple lines from a for loop iteration?

15 ビュー (過去 30 日間)
Abby
Abby 2022 年 2 月 4 日
コメント済み: Abby 2022 年 2 月 5 日
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
  1 件のコメント
Abby
Abby 2022 年 2 月 5 日
yes this is what i was looking for!! thank you so much

サインインしてコメントする。

その他の回答 (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?
Abby
Abby 2022 年 2 月 4 日
I just updated the original posting, thank you!!

サインインしてコメントする。


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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by