I am having trouble saving a value and calculating new values from it.

1 view (last 30 days)
my code is:
n = 180;
a = zeros(1,n);
a(1) = 20000;
R = 0.06;
r = R/12;
z = amortization(a(1),R,n);
for i = 2:n
b(i) = a(i-1)*r;
%b is interest payed in first month
c = z-b(i);
%c is amount applied towards loan principle
a(i) = a(i-1)-c;
%a is new amount left to be paid
d(i) = b(i)+b(i-1);
%d is last month's interest plus this months
e = b(1) + d(i);
disp(e)
x=1:180;
end
100 199.6561 198.9667 198.2738 197.5775 196.8777 196.1743 195.4675 194.7571 194.0432 193.3257 192.6046 191.8799 191.1516 190.4196 189.6840 188.9447 188.2017 187.4550 186.7046 185.9504 185.1924 184.4307 183.6651 182.8958 182.1225 181.3454 180.5644 179.7795 178.9907 178.1980 177.4012 176.6005 175.7958 174.9871 174.1743 173.3575 172.5365 171.7115 170.8823 170.0490 169.2116 168.3699 167.5241 166.6740 165.8196 164.9610 164.0981 163.2309 162.3593 161.4834 160.6031 159.7184 158.8293 157.9357 157.0377 156.1352 155.2281 154.3165 153.4004 152.4797 151.5544 150.6244 149.6899 148.7506 147.8066 146.8579 145.9045 144.9463 143.9834 143.0156 142.0429 141.0654 140.0830 139.0957 138.1035 137.1063 136.1041 135.0969 134.0847 133.0674 132.0450 131.0175 129.9849 128.9471 127.9041 126.8560 125.8025 124.7438 123.6798 122.6105 121.5359 120.4558 119.3704 118.2795 117.1832 116.0814 114.9741 113.8613 112.7429 111.6189 110.4892 109.3540 108.2130 107.0664 105.9140 104.7558 103.5919 102.4222 101.2466 100.0651 98.8777 97.6844 96.4851 95.2798 94.0685 92.8511 91.6276 90.3981 89.1623 87.9204 86.6723 85.4180 84.1573 82.8904 81.6172 80.3375 79.0515 77.7590 76.4601 75.1547 73.8428 72.5243 71.1992 69.8675 68.5291 67.1840 65.8322 64.4737 63.1083 61.7362 60.3571 58.9712 57.5783 56.1785 54.7717 53.3578 51.9369 50.5089 49.0737 47.6314 46.1818 44.7250 43.2609 41.7895 40.3108 38.8246 37.3310 35.8299 34.3214 32.8053 31.2816 29.7503 28.2113 26.6647 25.1103 23.5481 21.9781 20.4003 18.8146 17.2210 15.6193 14.0097 12.3921 10.7663 9.1324 7.4904 5.8401 4.1816
plot(x, a,'b')
hold on
plot(x,e,'r')
function P = amortization(a, R, n)
r = R/12;
P = a*(r*((1+r)^n))/(((1+r)^n) - 1);
end
I am trying to plot the total accumulated interest paid (sum of b). so the correct plot would be (1,100), (2,199.66), etc. Any help would be appreciated. I am new to matlab.

Accepted Answer

Voss
Voss on 19 Aug 2022
n = 180;
a = zeros(1,n);
b = zeros(1,n);
e = zeros(1,n);
a(1) = 20000;
R = 0.06;
r = R/12;
z = amortization(a(1),R,n);
for i = 2:n
b(i) = a(i-1)*r;
%b is interest payed in first month
c = z-b(i);
%c is amount applied towards loan principle
a(i) = a(i-1)-c;
%a is new amount left to be paid
d(i) = b(i)+b(i-1);
%d is last month's interest plus this months
e(i) = e(i-1) + b(i);
end
x=1:180;
plot(x,a,'b')
hold on
plot(x,e,'r')
function P = amortization(a, R, n)
r = R/12;
P = a*(r*((1+r)^n))/(((1+r)^n) - 1);
end

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices 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!