I am trying to create a table showing the repayment schedule for a loan but I can't create the table
2 views (last 30 days)
Show older comments
function table = int_paid (s,n,int)
for year = 1:n
%Calculates the interest paid
%where i=interest rate and n=number of years to repay the loan, p=
%equal payment
p = s * (int/((1-(1/(1+int)^(n)))));
%Calculates the interest paid
%where i=interest rate and n=number of years to repay the loan, p=
%equal payment
interest_paid(year) = p * (1 - ((1/((1+int))^(n-year+1))))
%Calculates the Principle Repaid at specified year
%where i=interest rate and n=number of years to repay the loan, p=
%equal payment
principal_repaid(year) = p * ((1/((1+int))^(n-year+1)));
%Calculates the outstanding balance
%where i=interest rate and n=number of years to repay the loan,
% p=equal payment
outstanding_balance(year) = p * ((1-(1/((1+int))^(n-year)))/int);
end
table= [(0 : year)' interest_paid(year)' principal_repaid(year)' outstanding_balance(year)'];
1 Comment
Walter Roberson
on 19 Oct 2015
You should avoid using "table" as a variable name, as it the name of the MATLAB class constructor for the table class.
Answers (1)
Walter Roberson
on 19 Oct 2015
Your line
table= [(0 : year)' interest_paid(year)' principal_repaid(year)' outstanding_balance(year)'];
is after your "for year" loop. After a "for" loop has ended, the index variable ("year") is left at the last value it had in the for loop, so after the for loop, year will be a scalar with value n. interest_paid(year) will then be interest_paid indexed at a scalar value and so will be a scalar result; likewise with principal_repaid and outstanding_balance. Therefore you are trying to take your vector (0:year)' of length (n+1) and horizontally concatenate three scalars to that; that fails.
As you want the complete variables output you should not be indexing them.
Notice that your interest_paid variable is indexed from year 1 to year n, but you are trying to output (0:year) beside it. Your other variables do not include information from year 0.
0 Comments
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!