ITEC 2600 Introduction to Analytical Programming ...people.math.yorku.ca/~zyang/2600m/Lecture...

14
ITEC 2600 Introduction to Analytical Programming Instructor: Prof. Z. Yang Office: DB3049

Transcript of ITEC 2600 Introduction to Analytical Programming ...people.math.yorku.ca/~zyang/2600m/Lecture...

ITEC 2600

Introduction to Analytical Programming

Instructor: Prof. Z. Yang

Office: DB3049

Lecture Ten

Mortgage Payment

Mortgage

• Mortgage generally involves two parties, borrower

and lender.

• Borrower borrows money to finance ‘big’ purchase,

such as a house.

• Lender obtains payback of loan principal and agreed

interests.

• The loan amount is usually big so that the borrower

has to make multiple payments with agreed terms

(e.g., monthly, etc.)

• Lender’s right is secured by ownership (title) of the

property (purchased).

3

An Example

• A borrower borrows money (a large amount)

from a bank (lender) to buy a house

• The borrower agrees to pay a fixed amount of

money to the bank every month.

• The monthly payments include two parts:

1. Interests owed

2. Portion of the loan principle

• The borrower agrees to pay back the entire loan

in X years (e.g., 25 years)

4

Notation in the Example

The following terms and notation will be used in

formula derivation and mortgage payment

calculation:

• O – Original loan principle

• B – Loan principal (remaining balance)

• P – Monthly payment

• n – Total number of payments

• r – Interest rate (per month)

5

Loan Principal

• After the first payment,

𝐵1 = 𝐵0 − (𝑃 − 𝐵0 ∗ 𝑟)

• After the second payment,

𝐵2 = 𝐵1 − (𝑃 − 𝐵1 ∗ 𝑟)

• In general after the ith payment, we have,

𝐵𝑖 = 𝐵𝑖−1 − (𝑃 − 𝐵𝑖−1 ∗ 𝑟)

𝐵𝑖 = 𝐵𝑖−1 ∗ (1 + 𝑟) − 𝑃

6

New Principal Balance

𝑩 = 𝑩 ∗ 𝟏 + 𝒓 − 𝑷

• New balance is the remaining balance.

• Using mathematical induction, the

remaining balance is below:

𝐵 = 𝑂 ∗ (1 + 𝑟)𝑛−𝑃 ∗(1 + 𝑟)𝑛−1

(1 + 𝑟) − 1

7

New month balance Previous month balance

previous month payment

Script for Payment

Calculation

Simple mortgage payment calculation

O - original loan amount

B - loan balance after n payments

n - total number of payments

P - monthly payment

r - monthly interest rate = annual_rate/12

Hint: let B=0 to obtain P. P becomes zero after total

number of payments and P is constant;

8

Script for Payment

Calculation (cont’d)

O=150,000; % original loan amount $150K

y=30; % Amortization period is 30 years.

annual_rate=0.10; % annual interest rate

n=y*12; % total number of payments

r=annual_rate/12;

u=r/((1+r)^n-1);

% how to calulate P

disp(P)

9

Script for Payment

Calculation (cont’d)

disp('Rate Monthly Payment ($)');

O=150000; % original loan amount $150K

y=30; % Amortization period is 30 years.

annual_rate=0;

for i=1:9; % annual interest rate

annual_rate=i/100;

n=y*12; % total number of payments

r=annual_rate/12;

u=r/((1+r)^n-1);

P=(O*(1+r)^n)*u;

disp([num2str(annual_rate*100),'%

',num2str(round(P,2))]);

end10

Function Script

1. Create a function script to calculate

monthly mortgage payment:

2. The inputs of the function are:

original loan amount;

amortization period;

annual interest rate.

3. The output of the function is monthly

payment amount.

11

Function Script for Payment

function P = m_payment(Orig, years,

annual_rate)

N = years*12; % total number of

payments

r = annual_rate/12;

u = r/((1+r)^N-1);

P = (Orig*(1+r)^N)*u;

12

Function Script (Cont’d)

Write a script to do the following:

1. Input original loan amount,

amortization period, and annual

interest rate.

2. Call the function to calculate the

monthly payment.

3. Display the accurate and adequate

payment information.

13

More Example

The prices, in dollar, of a certain stock over a 10-day

period are given below:

price = [19, 18, 22, 21, 25, 19, 17, 21, 27, 29]

You had 1000 shares at the beginning of Day One. You

bought 100 shares on the days when the price was below

$20 and sold 100 shares on the days when the price was

above $25. Use MATLAB to compute

a. The amount of money that you spent on buying shares

during the 10-day period;

b. The amount of money you received from the sale of

the shares during the 10-day period.

14