Simple for Loop calculation help MATLAB!
2 次查看(过去 30 天)
显示 更早的评论
Matlab
I want to calculate anual compound of an investment using for loop, lets call its P is a final value.
I have 3 variables P0 the investment amount, r is rate, and y=years.
Can you help me outline the code using for loop to calculate the annually compound P if 3 variables are fixed?
0 个评论
采纳的回答
Chunru
2021-6-16
Using loop (for integer number of years):
P0 = 1000;
r = 0.05;
y = 3;
P = P0; % initialization
for i=1:y
P = P0*(1+r);
end
fprintf('P=%.2f\n', P);
2 个评论
Chunru
2021-6-16
This is brute force method using loop. There is close-form formula for such problem.
P0 = 1000;
r = 0.05;
y = 0;
P = P0;
while true
y = y + 1;
P = P*(1+r);
if P>=3*P0
break;
end
end
fprintf('y=%d, P=%.2f\n', y, P)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!