I want to be able to calculate and plot the total interest if the extra payment each month is changing

8 visualizzazioni (ultimi 30 giorni)
my code is:
which calls on my file:
function P = amortization(a, R, n)
r = R/12;
P = a*(r*((1+r)^n))/(((1+r)^n) - 1);
end
I want to be able to plot p vs e so basically the first data point would be (10, (total interest paid if extra payment a month is $10). The next point would be (20, (total interest paid if extra payment a month was 20), and so on.

Risposta accettata

William Rose
William Rose il 21 Ago 2022
  1. Please add lots of comments to your code. Especially when you are asking for help. This will allow other to understand your code and give useful advice. For example, what variables are for what purpose, etc.
  2. Tell us what happens when you run your code. Do you get an error? Do you get output that is not what you expect? Tell us the error, or why the result is unexpected.
  3. Indent what is inside the for loop, to make the code easier to understand.
Exactly what are you trying to do? Your code pays 0 extra in month 1, $10 extra in month 2, $20 extra in month 3, and so on, up to $500 extra in month 51. Is that your intent?
I saved your code as a file and ran it. I got this result:
Unable to perform assignment because the left and right sides have different number of elements
Error in loanRepayment (line 21)
a(i) = a(i-1)-c
This occurs because a(i) is a scalar and c is a vector. I think you intended c to be a scalar: the amont of principal paid at the current time step. c is a vector in your script because it is defined by c = z-b(i); where z is a vector. z is a vector because it is defined by z=amortization(a(1),R,n)+p; , The right hand side of the preceding equation is a vector, because p is a vector. Is this what you intended?
p, as used in the code, is a vector whose elemnts are the extra amount paid each period. I think amortization(a(1),R,n) is the standard payment. Then z would be the vector of the total payent in each period.
I suspect you meant to write, or should have written
c=z(i)-b(i);
for line 20, to get the amount of pricipal paid at time i. We can fix that, but before we do, there i another problem: z has only 51 elements, because p has 51 elements. But there are 180 pyments. Therefore, in month 52, the code will have an error, because it will try to read z(52), which does not exist.
You said you want to plot p versus e. Define p and e in words, please, to help others understand what you are trying to do. In the code as you have presented it, e is 1-by-180 and p is 1-by-51, so p and e cannot be plotted versus one another.
e(i) is the running sum of interest paid up through period i.
I think you want to determine the total interest paid, at the completion of loan repayment, when you pay a constant extra amount each month. This is not what your code does. I think you want to see how the total interest paid varies with the amount of the constant extra monthly payment.
If I am right, then I suggest the following:
Use an inner loop and an outer loop. The outer loop goes from i=1 to 51. The amount of the extra monthly payment changes on each outer loop pass, from 0 to 500 in steps of 10. The inner loop goes from j=1 until the loan is repaid, which is 180 months, if there is no extra payment. You may want to use a while loop for the inner loop, since you do not know in advance how many months it will take to repay. (Actually there's a formula and a function for that...) Each time the inner loop completes, save the total interst paid in f(i), where f is 1-by-51, and reset e to all zeros for the next pass thorugh the inner loop. When you are all done, you can plot p versus f. They will both have 51 elements.
I assume you do not have the Financial Toolbox, or you do not want to use it. It has functions for this.
I hope that gives you enough guidance to adjust your script. Good luck.
  3 Commenti
William Rose
William Rose il 21 Ago 2022
Modificato: William Rose il 21 Ago 2022
The inner loop, (while loop), should have a stopping rule, rather than listing a number of times to execute. Read the manual about while loops. In this case, the stopping criterion is that we stop when the loan is paid back. We use the while loop instead of a for loop, because we don;t know how many months it will take to pay off the loan, when we pay more than the minimum. So we don't want to make 180 payments, if the loan is paid off after 120 months. When we pay extra, then, if we pay for too many months, the loan balance will become negative, which will result in negative interest, which will lead to non-sensical results.
Also: Inside the while loop, you need to think about when to use index j and when to use index i.
Also: Think carefully about the timing. In other words, a(j) is the balance at the start of month i. Is b(j) the interst paid at start of month j, or at end of month j? If it is at the start of the month, then is b(1)=0, because you don't pay on the date the loan begins? Whatever you decide, make sure it makes sense in all months including the first and the last. In the code you first posted, there were only 179 payments, so in that initial version of the code, the loan did not get fully paid off. For the latest code you have psosted, the loan probably does get paid off, but the while loop logic needs work (as noted above), so once you fix the while loop, make sure the loan gets paid off and that there are not extra payments.
If you set b()=0 (the vector of monthly interest paid) at the start of each pass through the outer loop, then, once you have completed the inner loop, b() will contain all the monthly interest payments. THerefore you can sum it up to get the total interest paid.
William Rose
William Rose il 22 Ago 2022
here is some pseudo-code:
extraPayment=0:10:500; %initialize array
totIntPaid=zeros(size(extraPayment)); %allocate array
for i=1:length(extraPayment)
%compute the total monthly payment for this run
b=zeros(1,180); %initialize the vector b
j=1; %initialize the month counter
while a(j)>0 %while the loan balance exceeds zero
%each month, compute b(j)=interest paid this month,
% a(j+1)=loan balance for next month, etc.
j=j+1 %increment the month counter
end
totIntPaid(i)=sum(b);
end
Maybe the general outline above will be useful. You will need to fill in the details above, plot final results, etc. Note the stopping condition for the while loop: stop when loan balance is not positive. a(j) is the amount due at the beginning of period j. b(j) is the interest paid at the end of period j.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by