# A Guide to MATLAB for Beginners and Experienced Users - Brian R.H.

**Download**(direct link)

**:**

**50**> 51 52 53 54 55 56 .. 91 >> Next

R=

A*(J+1)~N*J/((J+1)~N-1)

For example, with an initial loan amount A = $150,000 and a loan lifetime of 30 years (360 payments), we get the following table of payment amounts as a function of annual interest rate:Mortgage Payments 147

format bank; disp(' Interest for rate = 1:10,

disp([rate, double(subs(R, rate*percent*peryear]))]) end

Rate Payment')

[A, N, J], [150000, 360,...

Interest Rate Payment

1. .00 482. 46

2. .00 554. 43

3. .00 632. 41

4. 00 716. 12

5. 00 805. 23

6. 00 899. 33

7. 00 997. 95

8. 00 1100. 65

9. 00 1206. 93

10. 00 1316. 36

Note the use of format bank to write the floating point numbers with two digits after the decimal point.

There's another way to understand these calculations that's a little slicker and that uses MATLAB's linear algebra capability. Namely, we can write the fundamental equation

-Pnew = Pold * m — R in matrix from as

Vnew = BVold,

where v is the column vector (p) and B is the square matrix

m — R 0 1

We can check this using matrix multiplication:

syms R P; B = [m -R; 0 1]; v = [P; 1]; B*v

ans = [ m*P-R] [ 1]148 Chapter 9: Applications

which agrees with the formula we had above. (Note the use of syms to reset R and P to undefined symbolic quantities.) Thus the column vector [P; 1] resulting after n payments can be computed by left-multiplying the starting vector [A; 1] by the matrix Bn. Assuming m > 1, that is, a positive rate of interest, the calculation

[eigenvectors, diagonalform] = eig(B)

eigenvectors = [ 1, 1] [ 0, (m-1)/R]

diagonalform = [ m, 0] [ 0, 1]

shows us that the matrix B has eigenvalues m and 1, and corresponding eigenvectors [1; 0] and [1; (m — 1)/R ] = [1; J/R ]. Now we can write the vector [A; 1] as a linear combination of the eigenvectors: [ A;1] = x[1;0] + ;y[1; J/R]. We can solve for the coefficients:

[x, y] = solve('A = x*1 + y*1', '1 = x*0 + y*J/R')

x =

(A*J-R)/J

y =

R/J and so

[ A; 1] = (A — (R/ J )) * [1; 0] + (R/ J) * [1; J/R]

and

Bn • [A; 1] = (A — (R/J)) * mn * [1; 0] + (R/ J) * [1; J/R]. Therefore the principal remaining after n payments is

p = ((A * J—R) * mn + R)/ J = a * mn — R * (mn — 1)/ j.

This is the same result we obtained earlier.

To conclude, let's determine the amount of money A one can afford to borrow as a function of what one can afford to pay as the monthly payment R. We simply solve for A in the equation that P = 0 after N payments.Monte Carlo Simulation 149

solve(A*m"N - R*(m"N - 1)/(m - 1), A)

ans =

R*(m~N-1)/(m~N)/(m-1)

For example, if one is shopping for a house and can afford to pay $1500 per month for a 30-year fixed-rate mortgage, the maximum loan amount as a function of the interest rate is given by

disp(' Interest Rate Loan Amt.') for rate = 1:10, disp([rate, double(subs(ans, [R, N, m], [1500, 360,... 1 + rate*percent*peryear]))]) end

Interest Rate Loan Amt.

1. .00 466360.60

2. .00 405822.77

3. .00 355784.07

4. 00 314191.86

5. 00 279422.43

6. 00 250187.42

7. 00 225461.35

8. 00 204425.24

9. 00 186422.80

10. 00 170926.23

Monte Carlo Simulation

In order to make statistical predictions about the long-term results of a random process, it is often useful to do a simulation based on one's understanding of the underlying probabilities. This procedure is referred to as the Monte Carlo method.

As an example, consider a casino game in which a player bets against the house and the house wins 51% of the time. The question is: How many games have to be played before the house is reasonably sure of coming out ahead? This scenario is common enough that mathematicians long ago figured out very precisely what the statistics are, but here we want to illustrate how to get a good idea of what can happen in practice without having to absorb a lot of mathematics.150 Chapter 9: Applications

First we construct an expression that computes the net revenue to the house for a single game, based on a random number chosen between 0 and 1 by the MATLAB function rand. If the random number is less than or equal to 0.51, the house wins one betting unit, whereas if the number exceeds 0.51, the house loses one unit. (In a high-stakes game, each bet may be worth $1000 or more. Thus it is important for the casino to know how bad a losing streak it may have to weather to turn a profit — so that it doesn't go bankrupt first!) Here is an expression that returns 1 if the output of rand is less than 0.51 and —1 if the output of rand is greater than 0.51 (it will also return 0 if the output of rand is exactly 0.51, but this is extremely unlikely):

revenue = sign(0.51 - rand)

revenue = -1

In the game simulated above, the house lost. To simulate several games at once, say 10 games, we can generate a vector of 10 random numbers with the command rand(1, 10) and then apply the same operation.

revenues = sign(0.51 - rand(1, 10))

revenues =

1-11-1-111-11-1

In this case the house won 5 times and lost 5 times, for a net profit of0 units. For a larger number of games, say 100, we can let MATLAB sum the revenue from the individual bets as follows:

**50**> 51 52 53 54 55 56 .. 91 >> Next