How to plot multiple lines from a for loop iteration?

조회 수: 17 (최근 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 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