#==================================================== ============== # ---------------------------------- ------------------------------ # Step 0 : Assign values to each variable # P: principal # m: total months of repayment # i: annual interest rate # s: monthly interest rate P=25000 i=0.05 m=120 s=i/12 # step 1 : Calculate month vector t=1:m # step 2 : Calculate monthly repayment amount monthly.payment = (1+s)^m*s*P/((1+s)^m-1) # step 3 : Calculate the principal amount vector that should be repaid each month principal.paid.money.t = (1+s)^(t-1)*s*P/((1+s)^m-1) # step 4 : Calculate the monthly outstanding principal measure vector principal.remaining= P*(1-((1+s)^t-1)/((1+s)^m-1)) # step 5 : Use the monthly repayment amount to deduct the monthly principal amount vector to be repaid, and calculate the interest repaid each month interest.paid.month.t = monthly.payment - principal.paid.money.t # step 6 : Accumulate the interest paid each month to calculate the total interest total.interest.paid=sum(interest.paid.month.t) # step 7 : Display the calculation result in the console monthly.payment total.interest.paid t principal.paid.money.t interest.paid.month.t principal.remaining # step 8 : Plot with the month vector as the horizontal axis and the outstanding principal amount vector as the vertical axis plot(t,principal.remaining,type="l")
From "A Beginner's Guide to the R Language"