How to plot multiple lines from a for loop iteration?
15 views (last 30 days)
Show older comments
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 Comment
Voss
on 4 Feb 2022
Edited: Voss
on 4 Feb 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.
Accepted Answer
Voss
on 4 Feb 2022
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
More Answers (2)
Davide Masiello
on 4 Feb 2022
Try to move hold on before plot.
3 Comments
Davide Masiello
on 4 Feb 2022
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
on 4 Feb 2022
Edited: Jan
on 4 Feb 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?
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
