Info
此问题已关闭。 请重新打开它进行编辑或回答。
Plotting loop value according to years
1 次查看(过去 30 天)
显示 更早的评论
I have problem plotting loop values. I tried to plot trendline of sum (b) of each round according to years. So that the updatet value is plottet with respect to that year. What I'm doing wrong?
b=5000;
i=0.03;
ii=0.05;
y=0;
years=20;
while y<years
y=y+1;
if b>=8000;
b=b*(1+ii);
else
b=b*(1+i);
end
end
bal=b
plot(y,bal,'b-')
xlabel('Years')
grid on
0 个评论
回答(1 个)
David K.
2019-9-25
The problem is that the value b is not being saved within the loop, so your plot function is trying to plot a single value which does not really work. I would change it as such:
b=5000;
i=0.03;
ii=0.05;
y=0;
years=20;
bal = zeros(1,years); % pre allocate b (it's good practice not entirely needed)
bal(y+1) = b; % save the first value of b
while y<years
y=y+1;
if b>=8000;
b=b*(1+ii);
else
b=b*(1+i);
end
bal(y+1) = b; % save the value of b created
end
y = 0:years; % y also needs to be a vector and not a single value
plot(y,bal,'b-')
xlabel('Years')
grid on
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!