Simple for Loop calculation help MATLAB!

조회 수: 2 (최근 30일)
Tri Dang
Tri Dang 2021년 6월 16일
댓글: Chunru 2021년 6월 16일
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?

채택된 답변

Chunru
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);
P=1050.00
  댓글 수: 2
Tri Dang
Tri Dang 2021년 6월 16일
thank you so much
for the second part, how can I write a code, lets call it a #term (i) which is a min number of term require for the value of P to exceed 3 times the orignial value of P0 with given r
Chunru
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)
y=23, P=3071.52

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by