I am trying to create an array that displays the values 1-25 and puts the correct calculation next to it

4 ビュー (過去 30 日間)
ws
ws 2015 年 11 月 13 日
編集済み: James Tursa 2015 年 11 月 13 日
bal = input('What is your initial balance '); %%%Takes the user input for intial balance
rate = input('What is your annual interest rate '); %%%Takes user input for rate
A=zeros(25,1); %%%%Creating a 1 by 25 array
for i = 1:25 %%%%loop for numbers 1-25
p = ((i/100)/12)*bal; %%%Formula
r = (rate/100)/12; %%%Formula
a = 1 - ((r*bal)/p); %%%Formula
b= -log(a); %%%Formula
c = 1 + r; %%%Formula
d = log(c); %%%%Formula
n = ceil(b/d); %%%%final calculation
A(i)=n; %%%Store values of final calculation
end
fprintf('%d\n %d',i,A); %%%Print out 1:25 and final calculations

回答 (1 件)

James Tursa
James Tursa 2015 年 11 月 13 日
編集済み: James Tursa 2015 年 11 月 13 日
You could move the fprintf inside the loop, just before the "end" statement. E.g.,
fprintf('%2d %d\n',i,A(i)); %%%Print out i and final calculation for this i
That being said, I think you should double check your code. E.g., this line looks like you are using i as a simple index:
for i = 1:25 %%%%loop for numbers 1-25
But this line looks like you are using i as an interest rate:
p = ((i/100)/12)*bal; %%%Formula
It looks rather strange that the interest rate is increasing at each iteration, and I doubt that is what you are intending to calculate.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by