# How to plot a function within a for loop?

4 ビュー (過去 30 日間)
Greg 2022 年 7 月 29 日

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
##### 1 件のコメント表示非表示 なし
Stephen23 2022 年 7 月 31 日

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

### 採用された回答

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 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) サインインしてコメントする。

R2022a

### Community Treasure Hunt

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

Start Hunting!