ECOR 2606 - Lecture 5 (functions and plotting)

N numberofyears outputsi interestrateasafraction fip

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: A = P / n; else A = P * (i .* (i + 1).^n) ./ ((i + 1).^n ‐ 1); end end 5 1/18/2010 Problem: Find all of the points that satisfy both y = x2‐17x+60 and y = 50sin(x/2) (i.e. find all of the intersections of the curves defined by these equations). Step 1: Plot the two curves. &gt;&gt; f1 = @(x) 50 * sin(0.5 * x); &gt;&gt; f2 = @(x) x.^2 ‐ 17 * x + 60; &gt;&gt; x = linspace (0, 20, 100); &gt;&gt; y1 = f1(x); y2 = f2(x); &gt;&gt; plot (x, y1, 'r', x, y2, 'b') &gt;&gt; grid on The plot function will accept more than one pair of x and y vectors. This allows multiple plots to be placed on the same graph. Each pair of vectors may be followed by a string cntaining plotting options. Some of the permitted characters are: colour: ‘r’ = red, ‘b’ = blue, ‘k’ = black, ‘g’ = green line style: ‘‐’ = solid, ‘:’ = dotted, ‘‐‐’ = dashed, ‘‐.’ = dash dot data point markers: ‘x’ = crosses, ‘o’ = circles Options may be combined (e.g. ‘r:x’ gives a dotted red plot with a crosses) 6 1/18/2010 Note: The hold command is another way of placing multiple plots on the same graph. &gt;&gt; plot (x, y1, 'r‘) &gt;&gt; hold on &gt;&gt; plot (x, y2, 'b') &gt;&gt; hold off % be careful ‐ hold for a figure stays in effect until it is turned off &gt;&gt; grid on Step 2: Define the function for root finding (and perhaps plot it) &gt;&gt; f = @(x) f1(x) ‐ f2(x); &gt;&gt; figure (2) &gt;&gt; fplot (f, [0 20]) &gt;&gt; grid on; Step 3: Find the roots: &gt;&gt; x1 = fzero(f, [0 2]); &gt;&gt; x2 = fzero(f, [6 8]); &gt;&gt; x3 = fzero(f, [12 14]); &gt;&gt; x4 = fzero(f, [16 18]); 7...
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