I am trying to create an array that displays the values 1-25 and puts the correct calculation next to it
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
James Tursa
el 13 de Nov. de 2015
Editada: James Tursa
el 13 de Nov. de 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 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!