School work question, Having troubles adding onto a matrix
이전 댓글 표시
This is my problem:
%Imagine that you are a proud new parent. You decide to start a college saving plan for your child hoping to have enough money in 18 years to pay for cost of education. Suppose that your folks give you a 1000 dollars to start with and you have a 6% interest per year of 0.5% interest per month. Because of interest payments and your contributions, each month you balance will increase in accordance with the formula:
% New balance = old balance + interest + your contribution
% Write a for loop formula to find the saving account balance in 18 years. Plot the values on a time line with horizontal being time and dollar on the vertical
I'm having a problem getting the matrix a(old_balance) to add on each new value. Any advice for fixing this bug. Please and Thankyou.
old_balance = 1000;
interest = .005;
a = zeros(217);
for time = 0:1/12:18
old_balance = old_balance + 100 + (old_balance * interest);
a(old_balance) = old_balance
end
채택된 답변
추가 답변 (2개)
the cyclist
2015년 4월 28일
You effectively want
a(time)
instead of
a(old_balance)
in the last line of your loop. But notice that your time variable is not an integer, so you cannot index with that directly.
You will need to figure out a way to count that time interval and fill in the array accordingly. (It's not too difficult.)
Star Strider
2015년 4월 28일
You have the correct idea, but there are two problems: first, MATLAB uses positive integers for array subscripts so fractional increments such as 1/12 will not work; and second you need to subscript ‘old_balance’. )You don’t need ‘a’, since it just replicates ‘old_balance’.)
I made those tweaks, but otherwise did not alter your code:
old_balance(1) = 1000;
interest = .005;
time = 0:1/12:18;
for k1 = 1:length(time)
old_balance(k1+1) = old_balance(k1) + 100 + (old_balance(k1) * interest);
end
I will leave it to you to determine if it produces the result you want.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!