I am trying to create an array that displays the values 1-25 and puts the correct calculation next to it
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (1)
James Tursa
il 13 Nov 2015
Modificato: James Tursa
il 13 Nov 2015
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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!