Chapter 1 ntroduction to matlaB This book is an introduction to two subjects: MATLAB and numerical computing This first chapter introduces MATLAB by presenting several programs that inves tigate elementary, but interesting, mathematical problems. If you already have some experience programming in another language, we hope that you can see how MATLAB works by simply studying these programs If you want a more comprehensive introduction, an on-line manual from The Math Works is available. Select Help in the toolbar atop the MATLAB command window, then select MATLAB Help and Getting Started. A PDF version is vailable under Printable versions The document is also available from the Math Works Web site 10. Many other manuals produced by The Math Works are available on line and from the Web site A list of over 600 MATLAB-based books by other authors and publishers, in sev- eral languages, is available at [11. Three introductions to MATLAB are of particular interest here: a relatively short primer by Sigmon and Davis [8, a medium-sized mathematically oriented text by Higham and Higham 3, and a large, compreher sive manual by Hanselman and Littlefield 2 You should have a copy of matlaB close at hand so you can run our sample programs as you read about them. All of the programs used in this book have beer collected in a directory (or folder)named (The directory name is the initials of the book title. You can either start MATLAB in this directory or use patht to add the directory to the MATLAB path December 26 2005
Chapter 1 Introduction to MATLAB This book is an introduction to two subjects: Matlab and numerical computing. This first chapter introduces Matlab by presenting several programs that investigate elementary, but interesting, mathematical problems. If you already have some experience programming in another language, we hope that you can see how Matlab works by simply studying these programs. If you want a more comprehensive introduction, an on-line manual from The MathWorks is available. Select Help in the toolbar atop the Matlab command window, then select MATLAB Help and Getting Started. A PDF version is available under Printable versions. The document is also available from The MathWorks Web site [10]. Many other manuals produced by The MathWorks are available on line and from the Web site. A list of over 600 Matlab-based books by other authors and publishers, in several languages, is available at [11]. Three introductions to Matlab are of particular interest here: a relatively short primer by Sigmon and Davis [8], a medium-sized, mathematically oriented text by Higham and Higham [3], and a large, comprehensive manual by Hanselman and Littlefield [2]. You should have a copy of Matlab close at hand so you can run our sample programs as you read about them. All of the programs used in this book have been collected in a directory (or folder) named NCM (The directory name is the initials of the book title.) You can either start Matlab in this directory or use pathtool to add the directory to the Matlab path. December 26, 2005 1
Chapter 1. Introduction to MATLAB 1.1 The golden ratio What is the world's most interesting number? Perhaps you like T, or e, or 17. Some people might vote for o, the golden ratio, computed here by our first MATLAB phi =(1 sqrt(5))/2 Thi hi= 1.6180 Let's see more digits. format long phi 1.61803398874989 This didn't recompute it just displayed 15 significant digits instead of 5 The golden ratio shows up in many places in mathematics: we'll see several in this book. The golden ratio gets its name from the golden rectangle, shown in Figure 1. 1. The golden rectangle has the property that removing a square leaves a haller rectangle with the same shap Figure l.l. The golden rectangle Equating the aspect ratios of the rectangles gives a defining equation for o This equation says that you can compute the reciprocal of o by simply subtracting one. How many numbers have that property? Multiplying the aspect ratio equation by o produces the polynomial equation
2 Chapter 1. Introduction to MATLAB 1.1 The Golden Ratio What is the world’s most interesting number? Perhaps you like π, or e, or 17. Some people might vote for φ, the golden ratio, computed here by our first Matlab statement. phi = (1 + sqrt(5))/2 This produces phi = 1.6180 Let’s see more digits. format long phi phi = 1.61803398874989 This didn’t recompute φ, it just displayed 15 significant digits instead of 5. The golden ratio shows up in many places in mathematics; we’ll see several in this book. The golden ratio gets its name from the golden rectangle, shown in Figure 1.1. The golden rectangle has the property that removing a square leaves a smaller rectangle with the same shape. φ φ − 1 1 1 Figure 1.1. The golden rectangle. Equating the aspect ratios of the rectangles gives a defining equation for φ: 1 φ = φ − 1 1 . This equation says that you can compute the reciprocal of φ by simply subtracting one. How many numbers have that property? Multiplying the aspect ratio equation by φ produces the polynomial equation φ 2 − φ − 1 = 0
1.1. The Golden Ratio The roots of this equation are given by the quadratic formula: 1士 The positive root is the golden ratio. If you have forgotten the quadratic formula, you can ask MaTLab to find the roots of the polynomial. MATLAB represents a polynomial by the vector of its coefficients, in descending order. So the vector p(a) The roots are computed by the roots function. r roots(p) 803398874989 1.61803398874989 ers are the only numbers whose reciprocal can be computed by subtracting You can use the Symbolic Toolbox, which connects MATLAB to Maple, to solve the aspect ratio equation without converting it to a polynomial. The equation is represented by a character string. The solve function finds two solutions r= solve(1/x=x-12) produce 1/2*5^(1/2)+1/2] [1/2-1/2*5(1/2)] e pretty function displays the results in a way that resembles typeset mathe- matics pretty (r) 1/2 [1/25+1/2] 1/2] [1/2-1/
1.1. The Golden Ratio 3 The roots of this equation are given by the quadratic formula: φ = 1 ± √ 5 2 . The positive root is the golden ratio. If you have forgotten the quadratic formula, you can ask Matlab to find the roots of the polynomial. Matlab represents a polynomial by the vector of its coefficients, in descending order. So the vector p = [1 -1 -1] represents the polynomial p(x) = x 2 − x − 1. The roots are computed by the roots function. r = roots(p) produces r = -0.61803398874989 1.61803398874989 These two numbers are the only numbers whose reciprocal can be computed by subtracting one. You can use the Symbolic Toolbox, which connects Matlab to Maple, to solve the aspect ratio equation without converting it to a polynomial. The equation is represented by a character string. The solve function finds two solutions. r = solve(’1/x = x-1’) produces r = [ 1/2*5^(1/2)+1/2] [ 1/2-1/2*5^(1/2)] The pretty function displays the results in a way that resembles typeset mathematics. pretty(r) produces [ 1/2 ] [1/2 5 + 1/2] [ ] [ 1/2] [1/2 - 1/2 5 ]
Chapter 1. Introduction to MATLAB The variable r is a vector with two components, the symbolic forms of the two solutions. You can pick off the first component with phi r(1) which produces ph 1/2*5~(1/2)+1/2 TH ion can be converted to a numerical value in two different ways. It can to any number of digits using variable-precision arithmetic with the vpa(phi, 50) produces 50 digits 1.6180339887498948482045868343656381177203091798058 It can also be converted to double-precision floating point, which is the principal way that MATLAB represents numbers, with the double function. phi double(phi) 1.61803398874989 The aspect ratio equation is simple enough to have closed-form symbolic so- lutions. More complicated equations have to be solved approximately. The inline function is a quick way to convert character strings to objects that can be arguments to the MATLAB functions that operate on other functions f inline(1/x -(x-1)2) defines f(a)=1/-(a-1)and produces Inline function f(x)=1/x-(x-1) (Beginning with MATLAB 7, inline objects will be superceeded by a more powerful construction known as anonymous functions. Inline objects are still allowed in MATLAB 7, but anonymous functions are preferred because they produce more efficient code. The graph of f(a)over the interval 0 <a<4 shown in Figure 1.2 is obtained ezplot(f, 0,4
4 Chapter 1. Introduction to MATLAB The variable r is a vector with two components, the symbolic forms of the two solutions. You can pick off the first component with phi = r(1) which produces phi = 1/2*5^(1/2)+1/2 This expression can be converted to a numerical value in two different ways. It can be evaluated to any number of digits using variable-precision arithmetic with the vpa function. vpa(phi,50) produces 50 digits. 1.6180339887498948482045868343656381177203091798058 It can also be converted to double-precision floating point, which is the principal way that Matlab represents numbers, with the double function. phi = double(phi) produces phi = 1.61803398874989 The aspect ratio equation is simple enough to have closed-form symbolic solutions. More complicated equations have to be solved approximately. The inline function is a quick way to convert character strings to objects that can be arguments to the Matlab functions that operate on other functions. f = inline(’1/x - (x-1)’); defines f(x) = 1/x − (x − 1) and produces f = Inline function: f(x) = 1/x - (x-1) (Beginning with Matlab 7, inline objects will be superceeded by a more powerful construction known as anonymous functions. Inline objects are still allowed in Matlab 7, but anonymous functions are preferred because they produce more efficient code.) The graph of f(x) over the interval 0 ≤ x ≤ 4 shown in Figure 1.2 is obtained with ezplot(f,0,4)
1.1. The Golden Ratio (x1) Figure 1.2. f(o)=0 The name ezplot stands for "easy plot, although some of the English-speaking world would pronounce it"e-zed plot " Even though f(a) becomes infinite as a-0, ezplot automatically picks a reasonable vertical scale The statement phi fzero(f, 1) looks for a zero of f(a)near a =1. It produces an approximation to o that is accurate to almost full precision. The result can be inserted in Figure 1. 2 with hold on plot(phi,0,o') The following MATLAB program produces the picture of the golden rectangle shown in Figure 1. 1. The program is contained in an M-file named goldrect.m, so goldrect 7 GOLDRECT Plot the golden rectangle hi=(1+sqrt(5))/2 to phi phi 0 oJ y [00110] u=[11]; [01] plot(x,y, 'b,,u,v,'b--) text (phi/2, 1.05, '\phi ')
1.1. The Golden Ratio 5 0 0.5 1 1.5 2 2.5 3 3.5 4 −3 −2 −1 0 1 2 3 4 5 6 7 x 1/x − (x−1) Figure 1.2. f(φ) = 0. The name ezplot stands for “easy plot,” although some of the English-speaking world would pronounce it “e-zed plot.” Even though f(x) becomes infinite as x → 0, ezplot automatically picks a reasonable vertical scale. The statement phi = fzero(f,1) looks for a zero of f(x) near x = 1. It produces an approximation to φ that is accurate to almost full precision. The result can be inserted in Figure 1.2 with hold on plot(phi,0,’o’) The following Matlab program produces the picture of the golden rectangle shown in Figure 1.1. The program is contained in an M-file named goldrect.m, so issuing the command goldrect runs the script and creates the picture. % GOLDRECT Plot the golden rectangle phi = (1+sqrt(5))/2; x = [0 phi phi 0 0]; y = [0 0 1 1 0]; u = [1 1]; v = [0 1]; plot(x,y,’b’,u,v,’b--’) text(phi/2,1.05,’\phi’)