- 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.
- 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.
- Indent what is inside the for loop, to make the code easier to understand.

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

조회 수: 7(최근 30일)

표시 이전 댓글

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.

##### 댓글 수: 0

### 채택된 답변

William Rose
2022년 8월 21일

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

William Rose
2022년 8월 22일

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.

### 추가 답변(0개)

### 참고 항목

### 범주

### Community Treasure Hunt

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

Start Hunting!