# How to plot a function within a for loop?

조회 수: 4(최근 30일)
Greg 2022년 7월 29일
편집: Stephen23 2022년 7월 31일
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일
편집: 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!