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

조회 수: 4 (최근 30일)
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by