I am having trouble saving a value and calculating new values from it.
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Voss
2022년 8월 19일
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!