_ I'm trying to get this function to work. But i keep getting an error. I am trying to determine the amount in the savings account for next 18 years, which is represented by x(k) in an array. Problem is that only the first value for the first month shows up in the array._ * _ * _
clc,clear
% Variable declaration
% x will be used to store the value of the new balance
% a will be used to store the value of the old balance
% i will be used to store the value of the interest rate
% c will be used to store the value of the user's contribution
% Variable initialization
a = input('Enter a value for the initial balance: ');
i = input('Enter a value for the interest rate: ');
c = input('Enter a value that you will contribute: ');
% Calculation
month = 1:12:216;
x=zeros(length(month));
for k=1:length(month);
x(k) = a + i(k) + c
end

댓글 수: 10

Sara
Sara 2014년 7월 29일
is "i" an array? it doesn't look so in the input, but then you use i(k). In addition, x=zeros(length(month)); should be x=zeros(length(month),1); probably. Can you provide the inputs you are using?
Dylan Flores
Dylan Flores 2014년 7월 29일
편집: Dylan Flores 2014년 7월 29일
a = 1000 i = 0.05 c = 100
also sorry, made an edit to the question above. Interest rate should be fixed since its per month.
Sara
Sara 2014년 7월 29일
All the number in the expression of x are constant so you get just one answer. Which eqn are you trying to implement?
Dylan Flores
Dylan Flores 2014년 7월 29일
Its an equation given by the question. Its mentioned above. Any ideas on how I can get around this problem? Because the question demands that i use the for loop to find the amount in the savings account each month for the next 18 years
Sara
Sara 2014년 7월 29일
Then I think you got the wrong eqn. How does this sound:
x(1) = a;
for k = 2:numel(month)
x(k) = (x(k-1)+c)*(1+i); % this assuming you make the contrib every month and that the interest rate is applied monthly
end
Dylan Flores
Dylan Flores 2014년 7월 29일
Ah but that equation was given by the question
Sara
Sara 2014년 7월 29일
What question? Can you post the text of the question? Your eqn makes no sense, interest rate multiply capital not sum it.
Dylan Flores
Dylan Flores 2014년 7월 29일
Yeah i thought it was weird as well how this formula was handled but...
Sara
Sara 2014년 7월 29일
Old balance is x(k-1) not a and I think interest means x(k-1)*i. The loop becomes:
x(1) = a;
for k = 2:numel(month)
x(k) = x(k-1)+ x(k-1)*i + c; %or x(k-1)*(i+1) + c;
end
If the interest is %, divide i by 100.
Dylan Flores
Dylan Flores 2014년 7월 29일
Thanks! It works a lot better now!

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

답변 (1개)

Ben11
Ben11 2014년 7월 29일
편집: Ben11 2014년 7월 30일

0 개 추천

Maybe try this:
month = 1:12:216;
x=zeros(1,length(month)); % otherwise you have a 18*18 array
x(1) = a;
for k=2:length(month);
x(k) = x(k-1) + (i/100)*x(k-1) + c; % add the amount present during the previous month. Oh and I divided your interest rate by 100.
end

댓글 수: 2

Dylan Flores
Dylan Flores 2014년 7월 29일
Hey Thanks for helping out!
Ben11
Ben11 2014년 7월 30일
편집: Ben11 2014년 7월 30일
You're welcome! It looks like Sara and I arrived at pretty much a similar solution; although I incorrectly used a instead of x(k-1) :)

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

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2014년 7월 29일

편집:

2014년 7월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by