ECOR 2606 - Lecture 5 (functions and plotting)

# Assumed sizes mustbecompatible dotrequired sizes

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0 10 20 ]; &gt;&gt; ‐0.017 * t ans = 0 ‐0.1700 ‐0.3400 &gt;&gt; exp(ans) ans = 1.0000 0.8437 0.7118 &gt;&gt; 56.75*ans ans = 56.7500 47.8780 40.3930 &gt;&gt; 1+ans ans = 57.7500 48.8780 41.3930 &gt;&gt; 250/ans % a scalar divided by a vector is a no‐no (see table) ??? Error using ==&gt; mrdivide Matrix dimensions must agree. &gt;&gt; 250./ans ans = 4.3290 5.1148 6.0397 &gt;&gt; B = @(t) 250./(1 + 56.75*exp(‐0,17*t)); 3 1/18/2010 Problem (text 5.14): You buy a \$25,000 piece of equipment for nothing down at \$5,500 per year for 6 years. What interest rate are you paying? The formula below relates the payment amount (A) to the present worth of the item (P), the number of years (n), and the interest rate (i, expressed as a fraction, 1% = 0.01) A P i (1 i ) n (1 i ) n 1 If we knew P, n, and i we could easily calculate A, but this isn’t the problem. We know A, P, and n and want to calculate i. Ideally we’d manipulate the equation to obtain an expression for i in terms of A, P, and n (remember that analytic solutions are best when we can obtain them). i = somefunction (A, P, n) Since we can’t do this easily we will convert the problem into root finding form P i (1 i ) n A0 (1 i ) n 1 and use numerical methods (e.g. fzero) to find the value of i that satisfies the equation. Step 1: Define the basic function. &gt;&gt; P = 25000; n = 6; &gt;&gt; calcA = @(i) P * (i .* (i + 1).^n) ./ ((i + 1).^n ‐ 1); Step 2: Plot the function to get some idea of its behaviour and the approximate location of the solution. &gt;&gt; fplot (calcA, [0.01 0.20]) % plot for interest rates from 1% to 20% Step 3: Define the function for root finding (the f in f(x) = 0). &gt;&gt; A = 5500; &gt;&gt; f = @(i) calcA(i) ‐ A; Step 4: Use fzero to locate the desired root. In this case the plot tells us that the answer is somewhere between 6% and 10%. &gt;&gt; i = fzero(f, [0.06 0.10]) i = 0.0856 &gt;&gt; calcA(i) % check that answer is correct (always a good idea) ans = 5.5000e+003 4 1/18/2010 The basic idea can be used to produce a useful function m‐file that calculate the interest rate for any given P, A, and n. function [ i ] = P5_14( P, A, n ) %P5_14 Calculate interest rate. % NOTE: Assumes that interest rate is &gt;= 0.005. % Inputs: P = present value of item % A = annual payment % n ‐ number of years % Outputs: i = interest rate (as a fraction) f = @(i) (P * (i .* (i + 1).^n) ./ ((i + 1).^n ‐ 1)) ‐ A; maxI = A/P; i = fzero(f, [ 0.005 maxI ] ); end The search for the root cannot be made to start at zero because f is undefined at i = 0 (as the formula for calculating A does not work for i = 0). This difficulty can be overcome by producing a calcA function that properly deals with the special case of i = 0. This function could be implemented in a separate m‐file but it is also possible to place it in the same m‐file as the basic function as shown below. This makes it a subfunction (interested students should read text section 3.1.3). function [ i ] = P5_14v2( P, A, n ) %P5_14v2 Calculates interest rate. % Inputs and outputs as before… f = @(i) calcA (P, n, i) ‐ A; maxI = A/P; i = fzero(f, [ 0 maxI ] ); end function [A] = calcA (P, n, i) if i == 0...
View Full Document

## This note was uploaded on 09/13/2013 for the course ECOR 2606 taught by Professor Goheen during the Fall '10 term at Carleton CA.

Ask a homework question - tutors are online