ECOR 2606 - Lecture 5 (functions and plotting)

N numberofyears outputsi interestrateasafraction fip

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

View Full Document Right Arrow Icon
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. >> f1 = @(x) 50 * sin(0.5 * x); >> f2 = @(x) x.^2 ‐ 17 * x + 60; >> x = linspace (0, 20, 100); >> y1 = f1(x); y2 = f2(x); >> plot (x, y1, 'r', x, y2, 'b') >> 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. >> plot (x, y1, 'r‘) >> hold on >> plot (x, y2, 'b') >> hold off % be careful ‐ hold for a figure stays in effect until it is turned off >> grid on Step 2: Define the function for root finding (and perhaps plot it) >> f = @(x) f1(x) ‐ f2(x); >> figure (2) >> fplot (f, [0 20]) >> grid on; Step 3: Find the roots: >> x1 = fzero(f, [0 2]); >> x2 = fzero(f, [6 8]); >> x3 = fzero(f, [12 14]); >> 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