How to plot a function within a for loop?

15 次查看(过去 30 天)
I am trying to graph the resulting output of my function in a for loop, but all that comes up is a blank graph.
Any help is appreciated

采纳的回答

Voss
Voss 2022-7-29
You're plotting one point at a time there, which won't appear unless you use a data marker. And even then you'll only see the last one without turning hold on
% F = input('Value for F: ')
F = 3;
figure()
hold on % hold on to see all the points
for i = .05:.05:.95
term = F*((1/(1-i)^1)^.8+11*(1/i)^.4);
fprintf('Cost is %4.1f for Xa %1.2f\n', term,i)
plot(term,'o') % data marker
end
Cost is 112.5 for Xa 0.05 Cost is 86.2 for Xa 0.10 Cost is 73.9 for Xa 0.15 Cost is 66.4 for Xa 0.20 Cost is 61.2 for Xa 0.25 Cost is 57.4 for Xa 0.30 Cost is 54.5 for Xa 0.35 Cost is 52.1 for Xa 0.40 Cost is 50.3 for Xa 0.45 Cost is 48.8 for Xa 0.50 Cost is 47.6 for Xa 0.55 Cost is 46.7 for Xa 0.60 Cost is 46.2 for Xa 0.65 Cost is 45.9 for Xa 0.70 Cost is 46.1 for Xa 0.75 Cost is 47.0 for Xa 0.80 Cost is 48.9 for Xa 0.85 Cost is 53.3 for Xa 0.90 Cost is 66.6 for Xa 0.95
There you go - there's a plot of all the term values.
I imagine it would be better to plot the term values as a function of i, which might look like this:
figure()
hold on
for i = .05:.05:.95
term = F*((1/(1-i)^1)^.8+11*(1/i)^.4);
fprintf('Cost is %4.1f for Xa %1.2f\n', term,i)
plot(i,term,'o')
end
Cost is 112.5 for Xa 0.05 Cost is 86.2 for Xa 0.10 Cost is 73.9 for Xa 0.15 Cost is 66.4 for Xa 0.20 Cost is 61.2 for Xa 0.25 Cost is 57.4 for Xa 0.30 Cost is 54.5 for Xa 0.35 Cost is 52.1 for Xa 0.40 Cost is 50.3 for Xa 0.45 Cost is 48.8 for Xa 0.50 Cost is 47.6 for Xa 0.55 Cost is 46.7 for Xa 0.60 Cost is 46.2 for Xa 0.65 Cost is 45.9 for Xa 0.70 Cost is 46.1 for Xa 0.75 Cost is 47.0 for Xa 0.80 Cost is 48.9 for Xa 0.85 Cost is 53.3 for Xa 0.90 Cost is 66.6 for Xa 0.95
And it might be better still to plot them in a single line, so make the variable term a vector and plot it after the loop:
figure();
i_vector = 0.05:0.05:0.95;
for i = 1:numel(i_vector)
term(i) = F*((1/(1-i_vector(i)))^0.8+11*(1/i_vector(i))^.4);
fprintf('Cost is %4.1f for Xa %1.2f\n', term(i), i_vector(i))
end
Cost is 112.5 for Xa 0.05 Cost is 86.2 for Xa 0.10 Cost is 73.9 for Xa 0.15 Cost is 66.4 for Xa 0.20 Cost is 61.2 for Xa 0.25 Cost is 57.4 for Xa 0.30 Cost is 54.5 for Xa 0.35 Cost is 52.1 for Xa 0.40 Cost is 50.3 for Xa 0.45 Cost is 48.8 for Xa 0.50 Cost is 47.6 for Xa 0.55 Cost is 46.7 for Xa 0.60 Cost is 46.2 for Xa 0.65 Cost is 45.9 for Xa 0.70 Cost is 46.1 for Xa 0.75 Cost is 47.0 for Xa 0.80 Cost is 48.9 for Xa 0.85 Cost is 53.3 for Xa 0.90 Cost is 66.6 for Xa 0.95
plot(i_vector,term)

更多回答(1 个)

David Hill
David Hill 2022-7-29
No for-loop needed.
i = .05:.05:.95;
F = 10;
term = F*((1./(1-i).^1).^.8+11*(1./i).^.4);
plot(term)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by