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.
- loanAmount
- loanTerm
- 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 onplot(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 parametersmonthlyRate = annualRate/12;numPeriods = 12*loanTerm;% Calculate the amortization schedule[Principle,Interest,Balance,Payment] = amortize(monthlyRate,numPeriods,loanAmount);% Visualize the paymentsplot(Balance,'LineWidth',1.5)hold onplot(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 parametersloanAmount = 440000;annualRate = 0.04;loanTerm = 30;monthlyRate = annualRate/12; % 0.04 is the annual ratenumPeriods = 12*loanTerm; % loanTerm is 30 years% Calculate the amortization schedule[Principle,Interest,Balance,Payment] = amortize(monthlyRate,numPeriods,loanAmount);% Visualize the paymentsplot(Balance,'LineWidth',1.5)hold onplot(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.
Best Answer