I am having trouble saving a value and calculating new values from it.
1 view (last 30 days)
Show older comments
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
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.
0 Comments
Accepted Answer
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
0 Comments
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots 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!