Amortization Schedule Function - wrong result?

3 views (last 30 days)
Hi Guys, I am struggling with a Matlab task regarding Amortization Schedule Function and would be very thankfur for help!
The Exercise states:
" Writing a function is especially useful when you want to change certain variable values and see the results.
The script shown* calculates the amortization schedule of a fixed-rate mortgage.
To find the mortgage terms that suit your needs, you might want to try different loan amounts, rates and time periods.
TASK: Convert the script mortgageCalculator.m into a function. The function should accept three parameters.
  1. loanAmount
  2. loanTerm
  3. annualRate
It should output the monthly Payment.
In addition to adding the function declaration to the code file, you will need to make some changes in the code as well.
Hint: Add a function declaration with three inputs and one output on the first line of the file. Since you are providing the inputs, you should use those inputs in the code appropriately. The hard coded interest rate and loan term values should be replace by the function inputs. Do you still need to set a value to loanAmount?
*In the beginning the task gives you the following script which should be converted according to the task:
% This script calculates amortization schedule of a fixed-rate mortgage
% Define the parameters
loanAmount = 440000;
monthlyRate = 0.04/12; % 0.04 is the annual rate
numPeriods = 12*30; % loanTerm is 30 years
% Calculate the amortization schedule
[Principle,Interest,Balance,Payment] = amortize(monthlyRate,numPeriods,loanAmount);
% Visualize the payments
plot(Balance,'LineWidth',1.5)
hold on
plot(cumsum(Principle),'LineWidth',1.5)
plot(cumsum(Interest),'LineWidth',1.5)
legend('Remaining Balance','Accumulated Principle Paid','Accumulated Interest Paid')
title('Amortization Schedule')
hold off
The Solution gives you the following script:
(This obviously does not work on its own since the definitions of annualRate, loanTerm and loanAmount are missing)
function Payment = mortgageCalculator(loanAmount,loanTerm,annualRate)
% This function calculates amortization schedule of a fixed-rate mortgage
% Define the parameters
monthlyRate = annualRate/12;
numPeriods = 12*loanTerm;
% Calculate the amortization schedule
[Principle,Interest,Balance,Payment] = amortize(monthlyRate,numPeriods,loanAmount);
% Visualize the payments
plot(Balance,'LineWidth',1.5)
hold on
plot(cumsum(Principle),'LineWidth',1.5)
plot(cumsum(Interest),'LineWidth',1.5)
legend('Remaining Balance','Accumulated Principle Paid','Accumulated Interest Paid')
title('Amortization Schedule')
hold off
I created this script:
function Payment = mortgageCalculator(loanAmount,loanTerm,annualRate)
% This script calculates amortization schedule of a fixed-rate mortgage
% Define the parameters
loanAmount = 440000;
annualRate = 0.04;
loanTerm = 30;
monthlyRate = annualRate/12; % 0.04 is the annual rate
numPeriods = 12*loanTerm; % loanTerm is 30 years
% Calculate the amortization schedule
[Principle,Interest,Balance,Payment] = amortize(monthlyRate,numPeriods,loanAmount);
% Visualize the payments
plot(Balance,'LineWidth',1.5)
hold on
plot(cumsum(Principle),'LineWidth',1.5)
plot(cumsum(Interest),'LineWidth',1.5)
legend('Remaining Balance','Accumulated Principle Paid','Accumulated Interest Paid')
title('Amortization Schedule')
hold off
The result for the monthly Payment is then 2.1006e+03 and I really cannot find any reason why this should be false.
Still, I receive an "Incorrect!" Result and its said that the function does not return the correct reslut.
Do you have any idea what I did wrong or if I misunderstood or just missed anything in the Task?
I am stuck on this and would be super happy for some help.

Accepted Answer

Paul
Paul on 27 Nov 2020
Your solution will always yield the same result regardless of the what the user inputs to your function for loanAmount, loanTerm, and annualRate because the function assigns values to those inputs inside the function (the three lines under
%Define the parameters
That's one (the only?) difference between your function and the solution. Once you get rid of those three lines you'll be able to determine the payment for any amount, term, and rate. For example:
amount = 500000;
term = 20;
rate = .03;
payment = mortageCalculate(amount,term,rate)
This is discussed in the statement of the Exercise.
  1 Comment
Florian Ewald
Florian Ewald on 28 Nov 2020
Thank you so much Paul, that solved my problem! I created the following seperate script (calculate.m) and the solution was accepted!
loanAmount = 440000;
loanTerm = 30;
annualRate = 0.04;
Payment = mortageCalculator(loanAmount, loanTerm, annualRate);

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!