MATH1052 Multivariate Calculus and Ordinary Differential Equations WORKBOOK Semester 2, 2009 These lecture notes belong to: I can be contacted via: If you find them, please return them to me! c Department of Mathematics, The University of Queensland, Brisbane QLD 4072, Australia. 2 How to use this workbook This workbook is designed for use during lectures. It contains copies of all of the slides that will be shown in lectures, giving you time to listen and think in class, rather than spending the entire time writing. There are exercises, definitions and examples in the workbook for you to fill in. These will be covered in lectures, and you should write down all the information given. The lecturer will make it clear when they are covering something that you need to complete. The completed workbook will act as a study guide to assist you in working through assignments and preparing for the mid-semester and final exams. For this reason, it is very important to attend lectures. We will use these notes very heavily, so it is important that you get your own copy. • This workbook should be taken to lectures, tutorials and lab sessions. • Do not try to re-use a copy from previous years or from your friends: the notes change from year to year. In addition it is important for you to write things in your own words. • Each year, some people accidentally lose their notes, which causes big problems for them. Please write your name and contact details on the front page just in case. • These notes have been prepared very carefully, but there will inevitably be some errors in them. We are continually trying to improve the notes. If you have any suggestions on how to do so, please tell us. 3 Overview MATH1052 is a second course in calculus. It will enable you to use and visualise functions of two or more variables. Most importantly it extends your knowledge of calculus so you can differentiate multivariable functions and find their maxima and minima. These ideas are basic to the complex optimisation problems that occur when mathematics is used in economics, finance, and engineering. MATH1052 also introduces ordinary differential equations (ODE’s); one of the basic tools in mathematical modelling. ODE’s are used to describe how things change with time. In engineering and the physical sciences ODE’s are used to work out the motion of particles and satellites, the rates of chemical reactions, or to model electric fields. In biology they are used to model populations (to determine safe harvesting levels, for example) and to model epidemics. The textbook for the course is Calculus (6th Edition) by James Stewart (Brooks/Cole). This is the only text you need for the course. 4 CONTENTS Contents 1 Functions of Several Variables 1.1 1.2 1.3 1.4 1.5 1.6 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.1.1 Visualising Functions . . . . . . . . . . . . . . . . . . . . . . . 14 1.1.2 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Conic Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.2.1 Parabolas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.2.2 Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.2.3 Ellipses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.2.4 Hyperbolas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.2.5 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Scalar Equation for a Plane . . . . . . . . . . . . . . . . . . . . . . . 24 1.3.1 Horizontal Planes . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.3.2 Vertical Planes . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.3.3 Planes in General . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.3.4 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Vector Equation for a Plane . . . . . . . . . . . . . . . . . . . . . . . 29 1.4.1 Vector Revision. . . . . . . . . . . . . . . . . . . . . . . . . . . 29 1.4.2 Vector Equation to a Plane . . . . . . . . . . . . . . . . . . . 32 1.4.3 Distance from a Point to a Plane . . . . . . . . . . . . . . . . 34 1.4.4 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Lines in Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.5.1 Parallel, Skew and Orthogonal Lines. . . . . . . . . . . . . . . 40 1.5.2 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Contour Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 1.6.1 1.7 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Cross-sections of a Surface . . . . . . . . . . . . . . . . . . . . . . . . 52 1.7.1 1.8 13 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 A Catalog of Quadratic Surfaces in 3-D . . . . . . . . . . . . . . . . . 57 1.8.1 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 CONTENTS 5 2 Partial Derivatives and Tangent Planes 2.1 2.2 2.3 2.4 2.5 63 Limits and Continuity . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.1.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.1.2 Multivariable Limits . . . . . . . . . . . . . . . . . . . . . . . 64 2.1.3 Multivariable Continuity . . . . . . . . . . . . . . . . . . . . . 66 2.1.4 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 2.1.5 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Partial Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 2.2.1 Slope in the x Direction . . . . . . . . . . . . . . . . . . . . . 68 2.2.2 Slope in the y Direction . . . . . . . . . . . . . . . . . . . . . 69 2.2.3 Partial Derivatives for f (x, y, z) . . . . . . . . . . . . . . . . . 71 2.2.4 Higher Derivatives . . . . . . . . . . . . . . . . . . . . . . . . 72 2.2.5 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Tangent Plane . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 2.3.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 73 2.3.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 2.3.3 Equation for a Tangent Plane . . . . . . . . . . . . . . . . . . 75 2.3.4 Smoothness . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 2.3.5 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Linear Approximations . . . . . . . . . . . . . . . . . . . . . . . . . . 78 2.4.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 78 2.4.2 Linear Approximations for f (x, y) . . . . . . . . . . . . . . . . 79 2.4.3 Estimating Small Changes . . . . . . . . . . . . . . . . . . . . 81 2.4.4 Estimating Error . . . . . . . . . . . . . . . . . . . . . . . . . 82 2.4.5 Differentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 2.4.6 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Gradients and Directional Derivatives . . . . . . . . . . . . . . . . . . 84 2.5.1 Directional Derivatives . . . . . . . . . . . . . . . . . . . . . . 85 6 CONTENTS 2.6 2.5.2 The Gradient Vector ∇f . . . . . . . . . . . . . . . . . . . . . 86 2.5.3 Properties of the Gradient Vector . . . . . . . . . . . . . . . . 88 2.5.4 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 The Chain Rule and Applications . . . . . . . . . . . . . . . . . . . . 93 2.6.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 93 2.6.2 Chain Rule for f (x, y) . . . . . . . . . . . . . . . . . . . . . . 94 2.6.3 Larger Chain Rules . . . . . . . . . . . . . . . . . . . . . . . . 95 2.6.4 Implicit Differentiation . . . . . . . . . . . . . . . . . . . . . . 97 2.6.5 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 3 Max and Min Problems on Surfaces 3.1 3.2 3.3 Quadratic Approximation . . . . . . . . . . . . . . . . . . . . . . . . 99 3.1.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 99 3.1.2 Quadratic Approximations for f (x, y) . . . . . . . . . . . . . . 101 3.1.3 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Critical Points and Optimization . . . . . . . . . . . . . . . . . . . . 107 3.2.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 107 3.2.2 Critical Points for f (x, y) 3.2.3 Global Maxima and Minima . . . . . . . . . . . . . . . . . . . 119 3.2.4 The Matlab comand fminsearch . . . . . . . . . . . . . . . . . 120 3.2.5 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 . . . . . . . . . . . . . . . . . . . . 109 Constrained Optimisation and Lagrange Multipliers . . . . . . . . . . 121 3.3.1 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 4 Ordinary Differential Equations 4.1 99 125 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 4.1.1 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 4.1.2 Examples of ODEs . . . . . . . . . . . . . . . . . . . . . . . . 125 4.1.3 How do these ODEs arise? . . . . . . . . . . . . . . . . . . . . 126 CONTENTS 4.2 4.3 7 4.1.4 Solution to a DE . . . . . . . . . . . . . . . . . . . . . . . . . 126 4.1.5 Verify that y = Ae 2 x satisfies y 0 = xy. . . . . . . . . . . . . . 127 4.1.6 Initial Value Problem: Fluid Flow . . . . . . . . . . . . . . . . 128 4.1.7 The Order of an ODE . . . . . . . . . . . . . . . . . . . . . . 128 4.1.8 Extra reading: A Car at Constant Velocity . . . . . . . . . . . 129 4.1.9 Extra reading: Ice Thickness . . . . . . . . . . . . . . . . . . . 130 1 2 Motion of Projectiles . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 4.2.1 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 4.2.2 Gravity in One Dimension . . . . . . . . . . . . . . . . . . . . 131 4.2.3 In Two Dimensions . . . . . . . . . . . . . . . . . . . . . . . . 132 4.2.4 Example: Hitting a Target . . . . . . . . . . . . . . . . . . . . 133 4.2.5 Realistic Models . . . . . . . . . . . . . . . . . . . . . . . . . . 135 4.2.6 A Model with Air Resistance . . . . . . . . . . . . . . . . . . 135 Slope Fields and Equilibrium Solutions . . . . . . . . . . . . . . . . . 137 4.3.1 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 4.3.2 Example: the slope field of y 0 = 2y . . . . . . . . . . . . . . . 138 4.3.3 Equilibrium solutions . . . . . . . . . . . . . . . . . . . . . . . 138 4.3.4 Example: y 0 = −3(y − 1) . . . . . . . . . . . . . . . . . . . . . 139 4.3.5 Example: y 0 = 2t + 1 . . . . . . . . . . . . . . . . . . . . . . . 140 4.3.6 Example: y 0 = y(1 − y) . . . . . . . . . . . . . . . . . . . . . . 141 4.3.7 Stability of Equilibrium Solutions . . . . . . . . . . . . . . . . 142 4.3.8 Example: Stable or Unstable? . . . . . . . . . . . . . . . . . . 143 4.3.9 Existence and Uniqueness of Solutions . . . . . . . . . . . . . 143 4.3.10 Example: Long-term Behaviour . . . . . . . . . . . . . . . . . 144 4.3.11 Extra reading: consider the stability of ẏ = y 2 . . . . . . . . . 144 4.4 Euler’s Method for Solving ODE’s Numerically . . . . . . . . . . . . . 145 4.4.1 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 4.4.2 Revision: Equation of a Straight Line . . . . . . . . . . . . . . 145 8 CONTENTS 4.5 4.4.3 Example: y 0 = 2t, 4.4.4 Example. Euler’s Method using Matlab . . . . . . . . . . . . . 148 4.4.5 Demonstrating errors in Euler’s method . . . . . . . . . . . . 149 4.7 . . . . . . . 146 Separable ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 4.5.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 4.5.2 Solving separable ODEs . . . . . . . . . . . . . . . . . . . . . 151 4.5.3 Understanding implicit solutions: solve 4.5.4 Solve the IVP dy x = , y(0) = 3. . . . . . . . . . . . . . . . . 152 dx y 4.5.5 Solve the IVP dy sin x = , y(0) = 1. . . . . . . . . . . . . . . . 152 dx y 4.5.6 Solve dy x = . . . . . . . . . 151 dx y 4.5.8 dy = y. . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 dx dy Logistic equation: solve the IVP = y(1 − y), y(0) = y0 . . 153 dt Extra reading: Ice Thickness 2 . . . . . . . . . . . . . . . . . . 155 4.5.9 Extra reading: 4.5.7 4.6 y(0) = 0 by Euler’s Method dy −t = . . . . . . . . . . . . . . . . . . . . . 155 dt y Applications, Heat Equation, Population Growth . . . . . . . . . . . 156 4.6.1 Newton’s law of cooling . . . . . . . . . . . . . . . . . . . . . 156 4.6.2 Example: Police Report . . . . . . . . . . . . . . . . . . . . . 157 4.6.3 Exponential Population Growth . . . . . . . . . . . . . . . . . 159 4.6.4 Example: Rabbits . . . . . . . . . . . . . . . . . . . . . . . . . 160 4.6.5 Nonlinear Model of Population Growth . . . . . . . . . . . . . 161 4.6.6 Example: Halibut . . . . . . . . . . . . . . . . . . . . . . . . . 162 4.6.7 Extra reading: solving ODE’s Numerically . . . . . . . . . . . 164 Solving Linear First Order Equations . . . . . . . . . . . . . . . . . . 165 4.7.1 4.7.2 4.7.3 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 µ ¶ 2t dy + 2 Example: y = t2 + 1 . . . . . . . . . . . . . . . 166 dt t +1 Example: dy = y + t. . . . . . . . . . . . . . . . . . . . . . . . 168 dt CONTENTS 4.7.4 4.7.5 4.8 9 dy + xy = 1 with y(1) = 2. . . . . . . . . . . . 169 dx Extra reading: Electric Circuits . . . . . . . . . . . . . . . . . 170 Example: x2 Linear Homogeneous Second Order Differential Equations with Constant Coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 4.8.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 4.8.2 The Superposition Principle . . . . . . . . . . . . . . . . . . . 173 4.8.3 Constant Coefficients . . . . . . . . . . . . . . . . . . . . . . . 174 4.8.4 Case 1: Two real different roots r1 and r2 . . . . . . . . . . . . 175 4.8.5 Case 2: Equal roots . . . . . . . . . . . . . . . . . . . . . . . . 176 4.8.6 Case 3: Complex Roots 4.8.7 Example: ÿ + 9y = 0. . . . . . . . . . . . . . . . . . . . . . . . 178 4.8.8 Example: ÿ − 4ẏ + 5y = 0, 4.8.9 The Three Cases . . . . . . . . . . . . . . . . . . . . . . . . . 179 . . . . . . . . . . . . . . . . . . . . . 177 y(0) = 1 , ẏ(0) = 0. . . . . . . . 178 4.8.10 Extra reading: numerical solutions . . . . . . . . . . . . . . . 179 4.9 Linear Nonhomogeneous Second Order Differential Equations . . . . . 180 4.9.1 4.9.3 Some Observations . . . . . . . . . . . . . . . . . . . . . . . . 180 dy Example: + 3y = x . . . . . . . . . . . . . . . . . . . . . . 182 dx The Method of Undetermined Coefficients . . . . . . . . . . . 182 4.9.4 Example: y 00 − y = x2 + e2x . . . . . . . . . . . . . . . . . . . 183 4.9.5 Example: y 00 + y = 3 + sin x . . . . . . . . . . . . . . . . . . . 184 4.9.6 Extra reading: y 00 − y = −2x2 + 5 + 2ex 4.9.2 . . . . . . . . . . . . 185 4.10 Simple Harmonic Motion and Damped Oscillations . . . . . . . . . . 186 4.10.1 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 4.10.2 Hooke’s law and Newton’s second law. . . . . . . . . . . . . . 186 4.10.3 Mass on a Spring. . . . . . . . . . . . . . . . . . . . . . . . . . 186 4.10.4 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 4.10.5 Damped Oscillations . . . . . . . . . . . . . . . . . . . . . . . 189 4.10.6 Overdamping: a2 − 4mk > 0. . . . . . . . . . . . . . . . . . . 189 10 CONTENTS 4.10.7 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 4.10.8 Critical damping: a2 − 4mk = 0. . . . . . . . . . . . . . . . . 191 4.10.9 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 4.10.10 Underdamping: a2 − 4mk < 0. . . . . . . . . . . . . . . . . . . 192 4.10.11 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 4.10.12 Summary of Motion of a Spring: . . . . . . . . . . . . . . . . . 193 4.10.13 Extra reading: the pendulum . . . . . . . . . . . . . . . . . . 194 5 Parametrisation of Curves and Line Integrals 5.1 5.2 195 Parametrisation of Curves . . . . . . . . . . . . . . . . . . . . . . . . 195 5.1.1 Example: x = t2 − 2t, y = t + 1 . . . . . . . . . . . . . . . . 196 5.1.2 Example: x = t2 − 2t, y = t + 1, 0 ≤ t ≤ 4 5.1.3 The Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 5.1.4 Example: A Circle with Specified Starting Point . . . . . . . . 198 5.1.5 The Ellipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 5.1.6 The Spiral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 5.1.7 The Helix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 5.1.8 Piecewise linear paths . . . . . . . . . . . . . . . . . . . . . . 201 5.1.9 Extra reading: Matlab Plotting Parametrised Equations . . . 201 . . . . . . . . . 196 Position Vectors, Velocity and Acceleration . . . . . . . . . . . . . . . 202 5.2.1 Position Vector . . . . . . . . . . . . . . . . . . . . . . . . . . 202 5.2.2 Parametrising straight lines . . . . . . . . . . . . . . . . . . . 202 5.2.3 Example: Parametrise a piecewise linear path . . . . . . . . . 203 5.2.4 Example: position vector . . . . . . . . . . . . . . . . . . . . . 204 5.2.5 Velocity Vector . . . . . . . . . . . . . . . . . . . . . . . . . . 205 5.2.6 Acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 5.2.7 Example: Given constant v(t), find r(t) . . . . . . . . . . . . 206 5.2.8 Example: Find v(t) for a helical path . . . . . . . . . . . . . . 206 CONTENTS 5.2.9 11 Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 5.2.10 Extra reading: stone on a string . . . . . . . . . . . . . . . . . 208 5.3 5.4 5.5 Line Integrals and Work Done . . . . . . . . . . . . . . . . . . . . . . 209 5.3.1 Length of a Curve 5.3.2 Example: Length of a Helix . . . . . . . . . . . . . . . . . . . 211 5.3.3 Work Done by a Constant Force . . . . . . . . . . . . . . . . . 212 5.3.4 Work Done over a Curve . . . . . . . . . . . . . . . . . . . . . 213 5.3.5 Example: Work on a Circular Path . . . . . . . . . . . . . . . 216 5.3.6 Extra reading: length of a spiral . . . . . . . . . . . . . . . . . 217 5.3.7 Extra reading: length of a parabola . . . . . . . . . . . . . . . 217 Gradient Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 5.4.1 Main Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 5.4.2 Conservative Fields . . . . . . . . . . . . . . . . . . . . . . . . 218 5.4.3 Example: Work done by an Electric Field . . . . . . . . . . . 220 5.4.4 Example: Work done by a force . . . . . . . . . . . . . . . . . 221 5.4.5 Example: Show that F = (y + x)i + xj is a gradient field. 5.4.6 Example: Not a Gradient Field . . . . . . . . . . . . . . . . . 222 . . 221 Work Done or Path Integrals for Nonconservative Fields . . . . . . . 223 5.5.1 Example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 5.5.2 Example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 6 MATLAB Guide 6.1 . . . . . . . . . . . . . . . . . . . . . . . . 209 226 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 6.1.1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . 226 6.1.2 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 6.2 The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 6.3 M-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 6.3.1 If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 12 CONTENTS 6.3.2 6.4 6.5 For and While Loops . . . . . . . . . . . . . . . . . . . . . . . 231 Visualising Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 6.4.1 Ezplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 6.4.2 Ezcontour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 6.4.3 Ezsurf and Ezmesh . . . . . . . . . . . . . . . . . . . . . . . . 235 6.4.4 Functions of 3 Variables . . . . . . . . . . . . . . . . . . . . . 235 Visualising Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 6.5.1 Plot and Plot3 . . . . . . . . . . . . . . . . . . . . . . . . . . 236 6.5.2 Contour, Surf and Mesh . . . . . . . . . . . . . . . . . . . . . 236 6.6 Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 6.7 Optimisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 6.8 6.9 6.7.1 Using Fminsearch . . . . . . . . . . . . . . . . . . . . . . . . 239 6.7.2 Lines of Best Fit . . . . . . . . . . . . . . . . . . . . . . . . . 240 Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 6.8.1 Slope Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 6.8.2 Euler’s Method . . . . . . . . . . . . . . . . . . . . . . . . . . 244 6.8.3 Runge-Kutta Method . . . . . . . . . . . . . . . . . . . . . . . 245 6.8.4 Systems of Differential Equations . . . . . . . . . . . . . . . . 246 Parametric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 6.10 Common commands and operations . . . . . . . . . . . . . . . . . . . 249 6.10.1 Common Numerical Operations & Constants . . . . . . . . . . 249 6.10.2 Vector & Matrix Operations . . . . . . . . . . . . . . . . . . . 249 6.10.3 Special Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Guitar string 13 1 Functions of Several Variables 1.1 Introduction From MATH1051 or earlier mathematics, you will be familiar with working with functions of one variable in the form y = f (x). The tools of calculus were useful because you could: • sketch and visualise the relationship between x and y • find minima and maxima of y • fit a functional relationship between x and y • analyse the sensitivity of y on x by calculating dy dx • find approximations of f (x) through the Taylor series • find solutions to f (x) = 0 In this first module of MATH1052, we shall undertake these same basic ideas, but in the framework of functions of several variables (multivariate functions). Most of the theory for this section of the course is covered in Stewart 6th Ed., Chapter 15. Many familiar formulae are functions of more than one variable. For example, the volume V of a box, V (w, h, d) = whd is a function of width, height and depth. The profile of a guitar string is a function of the distance along the string and time f (x, t) = A sin(x) cos(t). Pluck 1 0.5 0 −0.5 −1 1 Time 0.5 Guitar0 0 0.2 0.4 0.6 0.8 1 14 1. FUNCTIONS OF SEVERAL VARIABLES 1.1.1 Visualising Functions Consider the volume of a cylinder as a function of two variables. V (r, `) = πr2 ` The first method of visualising it may be to keep ` fixed and plot V . Using Matlab, the ezplot function is the simplest graphing aid: ezplot(’pi*r^2*1’,[0,5]) hold on; ezplot(’pi*r^2*2’,[0,5]) hold on; ezplot(’pi*r^2*0.5’,[0,5]) Alternatively, we could keep r fixed and plot V : ezplot(’pi*1^2*l’,[0,5]) hold on; ezplot(’pi*0.5^2*l’,[0,5]) ezplot(’pi*2^2*l’,[0,5]) 1.1. INTRODUCTION 15 Better still is visualising the surface by sketching a 3-D graph. Let z = f (x, y) and then picture z as a surface. In Matlab, the ezsurf function will prove to be our quickest tool: ezsurf(’pi*r^2*l’,[0,2,0,2]) Volume of a Cylinder 30 25 20 15 10 5 2 0 0 1.5 0.5 length 1 1 0.5 1.5 2 radius 0 Note how you can change the point of view of the plot with the ‘rotate’ button. Just like the surface-pressure example, we can plot contour lines for the volume function. In Matlab, use the ezcontour function: ezcontour(’pi*r^2*l’,[0,2,0,2]); Using these tools, you should be able to replicate the plots in Stewart, Section 15.1. We shall start the course by looking at quadratic functions of two variables (where f (x, y) is quadratic in x and y). These have contours which are circles, ellipses, parabolas or hyperbolas. 1.1.2 Main Points • What is a function of several variables and why are they useful? • Methods to visualise a function of two variables. • Matlab commands ezplot, ezsurf and ezcontour. 16 1. FUNCTIONS OF SEVERAL VARIABLES 1.2 Conic Sections The goal of this section is to be confident in graphing, parabolas, circles, ellipses and hyperbolas (without use of a graphics calculator). This is easily done by completing the square. See also Stewart, Section 11.5. 1.2.1 Parabolas Consider the following graphs y = x2 y = x2 + 3 y = (x + 2)2 y = 2x2 y = 2(x + 2)2 + 3 Putting a quadratic function y = f (x) = ax2 + bx + c into the form (y − k) = a(x − h)2 , or equivalently, y = a(x − h)2 + k is called completing the square. What is the advantage of this? What is (h,k)? 1.2. CONIC SECTIONS 17 Completing the square makes use of the identity x2 ± 2xh + h2 = (x ± h)2 . Example Sketch the parabola y = x2 + 6x + 8. Example Sketch the parabola y = −3x2 − 6x + 1. 18 1. FUNCTIONS OF SEVERAL VARIABLES Exercise: Prove the quadratic formula. In other words, given ax2 + bx + c = 0, by completing the square, show that √ −b ± b2 − 4ac x= . 2a Parabolas, Circles, Ellipses and Hyperbolas are all examples of CONIC SECTIONS, so they all come from intersecting a double cone such as z 2 = x2 + y 2 , with a plane. See the diagrams in Stewart p. 690. Slice horizontally and you get circles. Tip the plane a bit and you get ellipses. Keep tipping and you will get a parabola. Continue tipping and the intersection becomes a pair of hyperbolas. 1.2. CONIC SECTIONS 1.2.2 19 Circles • A circle centred at the origin (0, 0) with radius r has the equation x2 + y 2 = r 2 . • A circle with radius r and centre (h, k) (x − h)2 + (y − k)2 = r2 . Example Sketch the circle x2 + 10x + y 2 − 4y + 20 = 0. 20 1.2.3 1. FUNCTIONS OF SEVERAL VARIABLES Ellipses An ellipse centred at the origin, in standard form, has the equation x2 y 2 + 2 = 1. a2 b An ellipse may also be defined as the set of points in a plane the sum of whose distances from two fixed focal points F1 and F2 is a constant. If a ≥ b, then the foci are at (±c, 0) where c2 = a2 − b2 . These lie on the major axis (x axis here). The ellipse intersects the x axis at the vertices (±a, 0), and the y axis at the vertices (0, ±b). What is the equation for an ellipse centred at the point (h, k)? 1.2. CONIC SECTIONS Exercise: Sketch the ellipse 21 x2 − x + 9y 2 = 0. We could check this in Matlab with the command ezplot(’x^2-x+9*y^2=0’,[-1,2],[-1,1]) 22 1.2.4 1. FUNCTIONS OF SEVERAL VARIABLES Hyperbolas A hyperbola centred at the origin, with asymptotes y = ± ab x, has the equation x2 y 2 − 2 =1 a2 b or − x2 y 2 + 2 = 1. a2 b A hyperbola may also be defined as the set of points in a plane, the difference of whose distances from two fixed focal points F1 and F2 is a constant. For these hyperbolae, the foci are at (±c, 0) and at (0, ±c) respectively, where c2 = a2 + b2 . What is the equation for a hyperbola centred at the point (h, k)? 1.2. CONIC SECTIONS Exercise: Sketch the hyperbola 23 x2 − 4y 2 = 9. Another kind of hyperbola is the very familiar y = 1/x. 1.2.5 Main Points • By completing the square, you should be able to graph by hand any equation of the form ax2 + by 2 + cx + dy + e = 0 identifying it as a line(s), circle, parabola, ellipse or hyperbola, and point out the main features: turning points, asymptotes and intercepts on axes. • You should also be able to obtain the equation from a sketch with features given. 24 1.3 1. FUNCTIONS OF SEVERAL VARIABLES Scalar Equation for a Plane The equation to a plane can be expressed using scalars or vectors. In this section, we will sketch planes in 3-space and write the scalar equation for a plane. In 3-space (R3 ), we usually take z pointing upwards and the (x, y) plane to be horizontal. The x and y co-ordinates give the position on the ground and z gives the height. This material is covered in Stewart, Section 13.5. 1.3.1 Horizontal Planes The x and y axes lie in the horizontal plane z = 0. All other horizontal planes are parallel to z = 0 and are given by z = c. 1.3.2 Vertical Planes A vertical plane has the form ax + by + d = 0; it is a function of x and y only, and z does not appear. Example: Sketch x + y − 1 = 0. First sketch the line in the (x, y) plane; then the plane contains this line and is parallel to the z axis. 1.3. SCALAR EQUATION FOR A PLANE 1.3.3 25 Planes in General In general, a plane may be written as a linear function of x, y and z i.e. f (x, y, z) = ax + by + cz + d = 0. If the plane is not vertical, i.e. c 6= 0, then this can be rearranged so that z is a function of x and y: z = F (x, y) = mx + ny + z0 . To plot the plane in Matlab, simply plot F (x, y) using ezsurf. The easiest way to sketch the plane by hand is to use the trangle method: The plane intercepts each axis at one point. These three points make up a triangle which defines the plane. z The Plane z=4−x−2y Triangle The plane z = 4 − x − 2y intersects the z axis at z = 4, the x axis at x = 4, and the y axis at y = 2. 4 2 y 0 x −2 −4 −6 0 −8 0 1 2 1 2 3 3 4 4 26 1. FUNCTIONS OF SEVERAL VARIABLES Example: Sketch the plane 2x + 3y − z + 6 = 0. • First find the intercepts on the axes. • Now draw the triangle between these 3 points and extend it to get the plane! The triangle defines the plane. In Matlab, the graphing command is simply: ezsurf(’2*x+3*y+6’,[-2,2,-5,5]) The Plane z=6+2x+3y 15 10 5 0 −5 −10 −3 −2 −1 0 1 1 0 −1 −2 −3 1.3. SCALAR EQUATION FOR A PLANE 27 Example: Find the the plane passing through (0, 0, 5) , (1, 3, 2) , (0, 1, 1). plane z=9x−4y+5 50 40 30 20 10 0 −10 −20 −30 −40 5 2 1 0 0 −1 y −5 −2 x The Matlab command to produce this surface would be: ezsurf(’9*x-4*y+5’,[-2,2,-5,5]) The following picture shows the intersection of the planes 2x + 3y − z + 6 = 0 and z = 9x − 4y + 5. Note that the intersection is a line. 28 1. FUNCTIONS OF SEVERAL VARIABLES planes 2x+3y−z+6=0 and z=9x−4y+5 50 40 30 20 10 0 −10 −20 −30 −40 5 2 1 0 0 −1 y −5 −2 x To produce two or more plots on the same figure, in Matlab use the hold command: hold on; 1.3.4 Main Points • You should be able to draw horizonal, vertical and general planes by hand. • You should be able to plot non-vertical planes in Matlab. • You should be able to find the equation to a plane through 3 given points. • You should know the scalar equation for a plane. 1.4. VECTOR EQUATION FOR A PLANE 1.4 29 Vector Equation for a Plane See Stewart Chapter 13.1 - 13.5 1.4.1 Vector Revision. In 2D Pythagoras gives the distance between (x0 , y0 ) and (x1 , y1 ) as d= p (x0 − x1 )2 + (y0 − y1 )2 . In 3D, the distance between (x0 , y0 , z0 ) and (x1 , y1 , z1 ) is d= Example: p (x0 − x1 )2 + (y0 − y1 )2 + (z0 − z1 )2 . Find the distance between the points (1, 2, 3) and (0, 0, 0). The norm of a vector is its length. In Matlab, you can directly call the norm of a vector: v=[1 2 3] v = 1 2 norm(v) ans = 3.7417 3 30 1. FUNCTIONS OF SEVERAL VARIABLES Recall the dot product of two vectors a and b is given by a · b = (a1 , a2 , a3 ) · (b1 , b2 , b3 ) = a1 b1 + a2 b2 + a3 b3 . It is also true that a · b = |a||b| cos θ where θ is the angle between a and b, and the norm of a is q √ |a| = a · a = a21 + a22 + a23 . If a · b = 0, then a and b are orthogonal. The vector projection of b onto a is the component of b in the direction of a proja b = |b| cos θ × a a·b = a. |a| |a|2 1.4. VECTOR EQUATION FOR A PLANE 31 Recall that the cross product a × b is a vector orthogonal to both a and b given by a × b = (a1 , a2 , a3 ) × (b1 , b2 , b3 ) = (a2 b3 − a3 b2 , a3 b1 − a1 b3 , a1 b2 − a2 b1 ). This can be calculated easily using the determinant. It is also true that |a × b| = |a||b| sin θ. The cross product points in the direction given by the right-hand-rule. 32 1.4.2 1. FUNCTIONS OF SEVERAL VARIABLES Vector Equation to a Plane Suppose we know that a certain point P0 = (x0 , y0 , z0 ) lies on a plane, and that a vector n is orthogonal to the plane. z n y P P0 . x . −→ Let P = (x, y, z) be an arbitrary point on the plane. Then the vector P0 P = (x − x0 , y − y0 , z − z0 ) lies in the plane and must be orthogonal to the vector n. Thus n · (x − x0 , y − y0 , z − z0 ) = 0 is the equation for this plane. This can also be succinctly written as n · (r − r0 ) = 0 where r is the position vector of P and r0 is the position vector of P0 . This is the vector equation for a plane. The vector and scalar equations are in fact the same, as shown next. 1.4. VECTOR EQUATION FOR A PLANE 33 We start with the vector equation, where n = (a, b, c). n · (r − r0 ) = 0 (a, b, c) · (x − x0 , y − y0 , z − z0 ) = 0 a(x − x0 ) + b(y − y0 ) + c(z − z0 ) = 0 ax + by + cz + d = 0 This is the scalar equation of the plane (where d = −ax0 − by0 − cz0 ). Example: Find the vector and scalar equations for the plane passing through the point (2, 4, −1) with normal vector n = 2i + 3j + 4k. Example: Find the angle between the planes x +4y − 3z = 1 and −3x +6y +7z = 0 (see Stewart Sec 13.5). 34 1.4.3 1. FUNCTIONS OF SEVERAL VARIABLES Distance from a Point to a Plane A common problem that arises in applications is to find the distance from a given point to a given plane. By distance, we mean the minimum distance. The problem is to find a formula for the distance D from P1 = (x1 , y1 , z1 ) to the plane ax + by + cz + d = 0. z P1 y D n b P0 . x . −→ Suppose we are given a point P0 = (x0 , y0 , z0 ) in the plane. The vector b =P0 P1 = (x1 − x0 , y1 − y0 , z1 − z0 ) . The distance D is the the length of the projection of b onto the normal vector n. Thus, D= |a(x1 − x0 ) + b(y1 − y0 ) + c(z1 − z0 )| |n · b| = |n| |n| |(ax1 + by1 + cz1 ) − (ax0 + by0 + cz0 )| √ = a 2 + b2 + c2 Now, P0 lies in the plane, so ax0 + by0 + cz0 + d = 0. Simplifying the formula gives D= |ax1 + by1 + cz1 + d| √ . a2 + b2 + c2 1.4. VECTOR EQUATION FOR A PLANE 35 Example: (a) Find the distance from the point (1, 2, 3) to the plane z = 4. The distance intended here is the shortest distance — or the distance perpendicular to the plane. (b) Find the distance between the point (1, 2, 3) and the plane x + y = 1. 1.4.4 Main Points • You should be able to calculate distances in 3D, and the norm of a vector. • You should know dot product, vector projection and the cross product • You should be able to derive the scalar equation from the vector equation for a plane. • You should be able to find the equation for a plane given sufficient information. • You should be able to find the perpendicular distance from a point to a plane, or between two planes. 36 1.5 1. FUNCTIONS OF SEVERAL VARIABLES Lines in Space There are three ways to mathematically represent a line in space: a) The vector representation b) The parametric representation c) Symmetric equations, obtained by eliminating parameters in (b). Depending on the application, one or the other methods might be best. (a) Vector representation Let P0 = (x0 , y0 , z0 ) be a point on the line L and v be a vector parallel to L. For an arbitrary point P = (x, y, z) lying on L we have −→ P0 P = (x − x0 , y − y0 , z − z0 ). z P0 P y r r0 v x . . −→ Now P0 P = a, say, where a = tv for some scalar t. Then r = r0 + a = r0 + tv. Example: Find the vector representation for the line passing through (5, 1, 3) and parallel to the vector (1, 4, −2). 1.5. LINES IN SPACE 37 (b) Parametric representation The parametric representation of a line is a scalar representation of the vector equation r = r0 + tv. Writing each vector in component form, r = (x, y, z) r0 = (x0 , y0 , z0 ) v = (a, b, c) gives (x, y, z) = (x0 , y0 , z0 ) + t(a, b, c). Matching components results in three scalar equations: x = x0 + at, y = y0 + bt, z = z0 + ct. Example: Find the parametric equations for the line passing through (5, 1, 3) and parallel to i + 4j − 2k. 38 1. FUNCTIONS OF SEVERAL VARIABLES (c) Symmetric equations The parameter t can be eliminated from the parametric equations, resulting in symmetric equations. From x = 5 + t, y = 1 + 4t, z = 3 − 2t, we obtain t = x−5= y−1 z−3 = . 4 −2 . Example: Find the symmetric equations of the line that passes through A = (2, 4, −3) and B = (3, −1, 1). 1.5. LINES IN SPACE 39 To plot a line in three-dimensional space, use the parametric form. The Matlab command is ezplot3. It takes three functions of a parameter, and the range of the parameter. Example: Plot the line x = 100 + 200t, y = 200 + 300t, z = 300 − 10t for t between −100 and 100. The Matlab command would be: ezplot3(’100+200*t’,’200+300*t’,’300-10*t’,[-100,100]) You can grab and rotate the resulting axes. 40 1.5.1 1. FUNCTIONS OF SEVERAL VARIABLES Parallel, Skew and Orthogonal Lines. The vector and parametric representations of a straight line are not unique. You could choose a different starting point r0 , or the vector v could be in the same direction, but have a different magnitude. In general, if v = ai + bj + ck, the numbers a, b, c are called direction numbers. Two lines are parallel if when they’re written as r = r10 + vt, and r = r20 + ut then u and v are linear multiples of each other. Two straight lines in three dimensional space rarely intersect. Non parallel, nonintersecting lines are called skew lines. Example: Show that the lines with parametric representation below are skew lines: L1 x = 1 + t, L2 x = 2s, y = −2 + 3t, y = 3 + s, z = 4 − t, z = −3 + 4s. 1.5. LINES IN SPACE 41 Two lines are orthogonal if the vectors of the direction numbers are orthogonal. An easy test for orthogonality is to take the dot product (if it is zero, then they are orthogonal). Example: Show that the two lines in symmetric form below are orthogonal. x−1 z+5 == y − 3 = , 2 4 2−x 5−y z−5 = = . 1 2 1 42 1. FUNCTIONS OF SEVERAL VARIABLES Further Theory Unlike lines, planes can not be skew in 3 dimensions. The only possibilities are that the two planes meet or that they are parallel. If two planes are parallel their normal vectors must also be parallel. The intersection of two planes in 3D is a line. Example: Find the parametric equations for the intersection of the planes: z = x + y, 1.5.2 2x − 5y − z = 1. Main Points • You should know the various expressions for lines. • Given one representation for a line, you should be able to find the other representations. • You should be able to find the equation for a line given sufficient information. • Know how to find where planes/lines meet in any combination. 1.6. CONTOUR DIAGRAMS 1.6 43 Contour Diagrams Geographical maps have curves of constant height above sea level, or curves of constant air pressure (isobars), or curves of constant temperature (isothermals). Drawing contours is an effective method of representing a 3D surface in two dimensions. We now look at functions of two variables, z = f (x, y) and their contours z = constant. See Stewart section 15.1. Consider the 3D surface z = f (x, y) = x2 + y 2 sliced by horizontal planes z = 0, z = 1, z = 2 . . . Plane z=0 Contour Description z=1 z=2 z=3 z=4 Note that as the radius increases, the contours are more closely spaced. 44 1. FUNCTIONS OF SEVERAL VARIABLES Example: Draw a contour diagram for z = f (x, y) = p x2 + y 2 . In Matlab, the command is ezcontour(’sqrt(x^2+y^2)’,[-10,10,-10,10]); If the planes z = constant are equally spaced in z, say z = 0, c, 2c, 3c ..., it is not hard to visualise the surface from its contour diagram. Spread out contours mean the surface is quite flat and closely spaced ones imply a steep climb. Potential well z=x2+y2 Cone z = sqrt(x 2+y2) 4 4 3.5 3 3 2 Plane z = 2 2.5 2 1.5 1 Plane z=2 1 Plane z=1 0 4 −2 0.5 0 0 −2 Plane z = 1 2 −1.5 −1 −0.5 0 0.5 1 1.5 2 2 4 2 0 0 −2 −2 −4 −4 1.6. CONTOUR DIAGRAMS 45 Note that the contours of have circular symmetry. for f (x, y), then the graph depends only on the radial graph will look the same. Example the last two functions were all circles. Such surfaces When x and y only appear as (x2 + y 2 ) in the equation has circular symmetry about the z axis. The height z p 2 2 distance r = x + y . So from any ’side-on’ view the 2 −y 2 z = e−x z=exp(−(x2+y2)) 1 0.8 0.6 0.4 0.2 0 2 1 2 1 0 0 −1 −1 −2 y −2 x exp(−x2−y2) 3 2 y 1 0 −1 −2 −3 −3 −2 −1 0 x 1 2 3 46 1. FUNCTIONS OF SEVERAL VARIABLES Example: Draw a contour diagram for z = x2 + 4y 2 − 2x + 1. Plane z=1 z=2 z=3 Contour Description 1.6. CONTOUR DIAGRAMS 47 Example: A saddle z = x2 − y 2 has hyperbolic contours. z=0 z=1 z=2 z = −1 z = −2 x2−y2 2 1.5 1 y 0.5 0 −0.5 −1 −1.5 −2 −2 −1.5 −1 −0.5 0 x 0.5 1 1.5 2 48 1. FUNCTIONS OF SEVERAL VARIABLES Exercise: Sketch the contour diagram for f (x, y) = 9x2 − 4y 2 + 2 with contours f = −2, 2, 6 and 10. Exercise: Sketch the contour diagram for z = f (x, y) = x2 . 1.6. CONTOUR DIAGRAMS 49 Exercise: Sketch the contours for z = f (x, y) = x − y 2 . Example: Draw the contour diagram of the planes z=x and z = x + y. Note: Contour diagrams of planes consist of parallel lines the same perpendicular distance apart. 50 1. FUNCTIONS OF SEVERAL VARIABLES You can construct the plane itself from its contour diagram provided the contours are labelled. Let the plane be z = mx + ny + c. Any point in the plane (x0 , y0 , z0 ) gives c: c = z0 − mx0 − ny0 . To find m consider moving along the plane in the positive x direction between two contours. Calculate the change in z, ∆z, between the two contours, and the change in x, ∆x, as you move from one contour line to the next. (Remember move in the x direction only, i.e. in a plane y = constant). ∆z . ∆x Similarly take a plane x = constant and move in the positive y direction to calculate ∆y. Then m = Then n = ∆z . ∆y Example: Find the plane given by the following contour diagram: Plane Contour z = 0 2x + y = 3 z = 1 2x + y = 2 1.6. CONTOUR DIAGRAMS 51 A plane z = mx + ny + c does not have just one slope. It has slope m in the x direction and slope n in the y direction. To visualise this, imagine starting at (0, 0, c) and walking in the (x, z) plane along the line z = mx + c (with slope m). Or walk in the (y, z) plane along the line z = ny + c (with slope n). Example: Find the plane with slope 6 in the x direction and 4 in the y direction which passes through the point (1, 5, 4). 1.6.1 Main Points • You should be able to plot contour diagrams in Matlab using the ezcontour function (and its variants). • You should be able to recognise circular symmetry in an equation. • You should be able to match contour diagrams with functions. • You should be able to plot simple contours by hand. 52 1.7 1. FUNCTIONS OF SEVERAL VARIABLES Cross-sections of a Surface A cross-section is the intersection of a surface with a vertical plane such as y = const (See also Stewart Section 13.6). Example The height z of a vibrating guitar string can be expressed as a function of horizontal distance x, and time t z = f (x, t) = A sin x cos t where 0 < x < π. The snapshots where t = constant are cross-sections of this ’surface’. If t=0 t = π4 t = π2 t = 3π 4 z z z z = A sin x = √A2 sin x =0 √ sin x = −A 2 These are all sine curves, but with different amplitudes. We can also consider the cross sections in x. For instance x = π2 (at the top of the sine wave), then z = A cos t which equals the amplitude of the sine wave. 1.7. CROSS-SECTIONS OF A SURFACE 53 Guitar string sin(π x) cos(2π t) 1 0.5 x 0 t −0.5 −1 1 1 0.8 0.5 0.6 0.4 0 0.2 0 Matlab can be used to make a movie of the 3-D surface by plotting cross sections at different t values in sequence. The sequence of plots can be stored in a vector and played as a movie using the following code: x=(0:0.25:1); for j=1:100 t=j/25; z=sin(pi*x)*cos(2*pi*t); plot(x,z);axis([0,1,-1,1]); M(j)=getframe; end Note: ezplot cannot be used to do this because Matlab gets confused about which of t, x is a variable and which is a number. 54 1. FUNCTIONS OF SEVERAL VARIABLES Example: Use Cross Sections to sketch the surface V = 13 πr2 h (volume of a cone). Here is the matlab version of the Volume of a cone V = 13 πr2 h. (In the following picture, x = r and y = h.) V=(1/3)(pi) r2 h 20 15 10 Volume of a Cone 5 10 0 8 −5 6 −10 4 −15 2 −20 4 0 0 2 2 1 0 0 −2 y −1 −4 −2 0.5 height 2 1 1.5 1 1.5 0.5 2 x 0 radius 1.7. CROSS-SECTIONS OF A SURFACE 55 Example: The cross sections of a saddle z = x2 − y 2 are parabolas. For y = y0 they point up: z = x2 − (y0 )2 ; and for x = x0 they point down: z = −y 2 + x20 . The surface is tricky to draw. (Think of a saddle for horse riding.) Here is the matlab version of the saddle z = x2 − y 2 . Saddle: z=x2−y2 4 3 2 1 0 −1 −2 −3 −4 2 1 2 1 0 0 −1 y −1 −2 −2 x 56 1. FUNCTIONS OF SEVERAL VARIABLES Example: Use cross sections to sketch z = f (x) = x2 1.7.1 Main Points • You should be able to construct cross sections of multivariate functions. • Cross sections are 2D graphs. • Animation of cross sections is another way to visualise multivariate functions. 1.8. A CATALOG OF QUADRATIC SURFACES IN 3-D 1.8 57 A Catalog of Quadratic Surfaces in 3-D Quadric surfaces are all the surfaces of the form ax2 + by2 + cz2 + dx + ey + fz + g = 0 where a, b, c, d, e, f, g are given real constants. Given an equation of this form, we can identify it as one of the following types. See Stewart p. 844. Note that the following list shows only the simplest equation for each type. The Ellipsoid (and Sphere) Simplest equation: x2 + y2 + z2 = 1 The x-cross sections (x =const) are The y-cross sections (y =const) are The contours (z =const) are 58 1. FUNCTIONS OF SEVERAL VARIABLES The Elliptic Paraboloid z = x2 + y 2 Simplest equation: The x-cross sections (x =const) are The y-cross sections (y =const) are The contours (z =const) are z=x2+y2, (potential well) 8 7 6 5 4 3 2 1 0 2 1 2 1 0 0 −1 −1 −2 y −2 x The Hyperbolic Paraboloid (Saddle) z = x2 − y 2 Simplest equation: The x-cross sections (x =const) are The y-cross sections (y =const) are The contours (z =const) are 2 2 Saddle: z=x −y 4 3 2 1 0 −1 −2 −3 −4 2 1 2 1 0 0 −1 y −1 −2 −2 x 1.8. A CATALOG OF QUADRATIC SURFACES IN 3-D 59 The General Cone z2 = x2 + y 2 Simplest equation: The x-cross sections (x =const) are The y-cross sections (y =const) are The contours (z =const) are General Cone 3 2 1 0 −1 −2 −3 −3 2 −2 −1 0 0 1 2 −2 3 2 2 2 z =x + y /4 Hyperboloids of One Sheet z2 = x2 + y 2 − 1 Simplest equation: The x-cross sections (x =const) are The y-cross sections (y =const) are The contours (z =const) are Hyperboloid (one sheet) 3 2 1 0 −1 −2 −3 −3 2 −2 −1 0 0 1 2 3 2−2 2 2 z =x + y /4−1 60 1. FUNCTIONS OF SEVERAL VARIABLES Hyperboloids of Two Sheets z2 = x2 + y 2 + 1 Simplest equation: The x-cross sections (x =const) are The y-cross sections (y =const) are The contours (z =const) are Hyperboloid (two sheets) 3 2 1 0 −1 −2 −3 −3 2 −2 −1 0 0 1 2 3 2−2 2 2 z =x + y /4+1 Plane(s) Simplest equation(?): z2 = x2 , x + y + z = 1, or The x-cross sections (x =const) are The y-cross sections (y =const) are The contours (z =const) are z The Plane z=4−x−2y Triangle 4 2 y 0 x −2 −4 −6 0 −8 0 1 2 1 2 3 3 4 4 z2 = 1 1.8. A CATALOG OF QUADRATIC SURFACES IN 3-D 61 Elliptic, Parabolic and Hyperbolic Cylinders Simple examples: x2 + y 2 = 1 (no z terms) z = x2 (no y terms) z2 − y2 = 1 (no x terms) 2 z=x in 3D 4 3.5 3 2.5 2 1.5 1 0.5 0 2 1 2 1 0 0 −1 y −1 −2 −2 Example. Sketch the surface x2 − 2y 2 = 1. x 62 1. FUNCTIONS OF SEVERAL VARIABLES Exercise Classify the following surfaces • (x − 1)2 + (y + 3)2 − z 2 = 0 • x2 − z 2 = y • z2 + y = 5 • x2 − y 2 + z 2 + 2x = 0 1.8.1 Main Points • You should now be fluent in sketching surfaces and contours for the conics and symmetric functions of two variables (that is, any function of the form ax2 + by 2 + cz 2 + dx + ey + f z + g = 0). • You should also be able to match surfaces with their cross-sections and contours. • You should be able to plot surfaces, contours and cross-sections in Matlab. 63 2 Partial Derivatives and Tangent Planes We hope to soon talk about derviatives of a function of more than one variable. To do this, we first check how the familiar concepts of a limit and continuity extend to functions of more than one variable. This material is covered in Section 15.2 of Stewart. 2.1 2.1.1 Limits and Continuity Review for f (x) Recall that for a function f (x) of one variable, we say that the limit lim f (x) exists x→a if and only if, the limit from the left is equal to the limit from the right, i.e., lim f (x) = lim+ f (x). x→a− x→a Further, if the limit exists and equals the actual value of f at x = a, i.e. if lim f (x) = lim+ f (x) = f (a) x→a− x→a then we say that f (x) is continuous at this point. 64 2.1.2 2. PARTIAL DERIVATIVES AND TANGENT PLANES Multivariable Limits When f is a function of more than one variable, the situation is more interesting. There are more than two ways to approach our point of interest. Consider the function x2 . f (x, y) = 2 x + y2 To see the function in Matlab, type ezsurf(’(x^2/(x^2+y^2)’) x2/(x2+y2) 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 −5 0.2 0.1 0 0 6 4 2 0 5 −2 −4 x Consider the limit as (x, y) → (0, 0). (i) Approaching the origin (0, 0) along y = 0: −6 y 2.1. LIMITS AND CONTINUITY 65 (ii) Approaching the origin along x = 0: Does lim (x,y)→(0,0) f (x, y) exist? In general, for the limit lim (x,y)→(a,b) f (x, y) to exist, it is necessary that every path approaching (a, b) gives the same value for the limit. So, we have the following test to demonstrate when a limit does not exist. Test to Show No Limit If f (x, y) → L1 as (x, y) → (a, b) along the path C1 , and f (x, y) → L2 6= L1 as (x, y) → (a, b) along another path C2 , then the limit lim (x,y)→(a,b) f (x, y) does not exist. Note: Since there are infinitely many possible paths to approach (a, b), can we ever prove that a limit really exists? We omit these proofs (epsilon-delta proofs) from the course (or see Stewart Sec 15.2, Example 4). 66 2.1.3 2. PARTIAL DERIVATIVES AND TANGENT PLANES Multivariable Continuity Definition A function f (x, y) is continuous at a point (a, b) if lim (x,y)→(a,b) f (x, y) = f (a, b) i.e. if the limit at the point exists and equals the value at the point. If a function is continuous at every point in a domain, we say simply that it is continuous. Most of the functions we will use are continuous. For example, polynomials in x and y are continuous (everywhere). Returning to our example, where is f (x, y) = x2 − y 2 continuous? x2 + y 2 Now define a function g by g(x, y) = f (x, y) where (x, y) 6= (0, 0) and g(0, 0) = 0. Is g continuous everywhere? (can we plug the hole in f ?) 2.1. LIMITS AND CONTINUITY 2.1.4 67 Exercise Is the function f (x, y) = xy x2 + y 2 continuous? (See Stewart 15.2 Example 2) 2.1.5 Main Points • You should be able to show when a limit does not exist • You should understand continuity of multivariate functions. 68 2.2 2. PARTIAL DERIVATIVES AND TANGENT PLANES Partial Derivatives This material is covered in Stewart, Sections 15.3. 2.2.1 Slope in the x Direction Consider the surface z = f (x, y) = 1 − x2 − y 2 and the point P = (1, −1, −1). Draw the y = constant cross-section through P and find the slope in the x direction at this point. 2 1−x −y 2 2 0 −2 −4 −6 −2 −1 2 0 1 1 x 0 2 −1 −2 y The slope in the x direction, with y held fixed, is called the partial derivative of f (x, y) with respect to x at the point (a, b) ¯ f (a + h, b) − f (a, b) ∂f ¯¯ ∆z = fx (a, b) = lim = lim ¯ ∆x→0 ∆x h→0 ∂x (a,b) h 2.2. PARTIAL DERIVATIVES 2.2.2 69 Slope in the y Direction Draw the x = constant cross section and find the slope at P = (1, −1, −1) in the y direction. i.e. where x = 1. 2 1−x −y 2 2 0 −2 −4 −6 −2 −1 2 0 1 1 x 0 2 −1 −2 y Similarly, the slope in the y direction, with x held fixed, is called the partial derivative of f (x, y) with respect to y at the point (a, b) ¯ f (a, b + h) − f (a, b) ∂f ¯¯ ∆z = f (a, b) = lim = lim y ∆y→0 ∆y h→0 ∂y ¯(a,b) h Normal rules of differentiation apply, we simply think of x as a constant. 70 2. PARTIAL DERIVATIVES AND TANGENT PLANES Exercise. Find the partial derivatives ∂f ∂f , of ∂x ∂y z = f (x, y) = x sin(y) + y cos(x). Exercise: Find fx (1, 2) and fy (1, 2) if f (x, y) = xy 3 + x2 . 2.2. PARTIAL DERIVATIVES 2.2.3 71 Partial Derivatives for f (x, y, z) Example The volume of a box V (x, y, z) = xyz If x change by a small amount ∆x, denote the change in V by ∆V . We can visualise that ∆V = yz∆x. Therefore ∆V = yz. ∆x ∂V = yz. ∂x For partial derivatives only one independent variable changes and all other independent variables remain fixed. Letting ∆x → 0 we have Example Parallel Resistance In an electrical circuit, the combined resistance R, from three resistors R1 , R2 and R3 connected in parallel, is 1 1 1 1 = + + R R1 R2 R3 What is the rate of change of the total resistance R with respect to R1 ? 72 2. PARTIAL DERIVATIVES AND TANGENT PLANES 2.2.4 Higher Derivatives The second partial derivatives of f (x, y), if they exist, are written as fxx = ∂2f , ∂x2 fxy = ∂ 2f ∂x∂y fyx = ∂2f , ∂y∂x fyy = ∂ 2f ∂y 2 If fxy and fyx are both continuous, then fxy = fyx . 2.2.5 Main Points • You should know the defintion and meaning of partial derivatives • You should be able to evaluate partial derivatives of functions. 2.3. THE TANGENT PLANE 2.3 73 The Tangent Plane This section is covered in Stewart, Section 15.4. 2.3.1 Review for f (x) Recall that for a function y = f (x) of one variable, the tangent line at the point (a, f (a)) is given by y − f (a) = f 0 (a)(x − a) ⇔ y = f (a) + f 0 (a)(x − a) Example Find the tangent line to the curve f (x) = x2 at x = 1. 74 2.3.2 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example Find the equation (of the form z = mx + ny + z0 ) for the tangent plane to the surface z = 1 − x2 − y 2 at the point P = (1, −1, −1). 1−x2−y2 2 0 −2 −4 −6 −2 −1 2 0 1 1 x 0 2 −1 −2 y 2.3. THE TANGENT PLANE 2.3.3 75 Equation for a Tangent Plane In general, the tangent plane to a given surface f (x, y) at (a, b), is ¯ ¯ ∂f ¯¯ ∂f ¯¯ z = f (a, b) + (x − a) + (y − b). ∂x ¯(a,b) ∂y ¯(a,b) Or equivalently, the tangent plane to f (x, y) at the point (x0 , y0 , z0 ) is z − z0 = fx (x0 , y0 )(x − x0 ) + fy (x0 , y0 )(y − y0 ). Example. What is the plane tangent to the surface z = f (x, y) = 4 − x2 + 4x − y 2 at (1, 1)? 76 2. PARTIAL DERIVATIVES AND TANGENT PLANES Tangent Plane at (1,1,6) to 2 2 z=4−x +4x−y 20 10 0 −10 −20 −30 −40 −50 −4 −2 0 2 4 4 −2 Tangent0 plane 2 is z=6+2x−2y Example Find the tangent plane to z = f (x, y) = e−x 2.3.4 2 −y 2 Smoothness Can we always find partial derivatives and tangent planes? −4 at (1, 3). 2.3. THE TANGENT PLANE 77 You cannot define a tangent plane to the surface at x=0. z=f(x,y)=3−|x| fx is undefined 3 at x=0 2 Example. Simple cusp like functions are not smooth. 1 0 4 −1 10 2 0 5 −2 0 −5 −4 A surface z = f (x, y) is smooth at (a, b) if f (x, y), fx and fy are both continuous at (a, b). When you zoom in close enough to a smooth surface it looks like a plane. One way to see this is to look at the contours. The contours of a plane are straight parallel lines, the same perpendicular distance apart. As you zoom into a smooth surface the contours straighten out. This means that close to (a, b) the surface is approximated by a plane; in fact it can be approximated by the tangent plane. Do straight contours imply smoothness? Example. Brownian motion is not smooth. Look at the figure below. No matter how much you zoom in, it always looks rough—in fact, Brownian motion is a fractal. There are surface analogues to Brownian motion, demonstrated with the fractal surface below. 6 5 4 3 2 1 0 −1 −2 0 1 2 3 4 5 6 7 Brownian motion 2.3.5 8 9 10 Fractal surface Main Points • You should know how to find a tangent plane to a smooth surface, and recognise when a tangent plane or partial derivatives do not exist. 78 2.4 2. PARTIAL DERIVATIVES AND TANGENT PLANES Linear Approximations See Stewart, Section 15.4. 2.4.1 Review for f (x) In science and engineering practice, a function f (x), of one variable, is sometimes approximated by a straight line. In particular, if we know an exact value (a, f (a)), then the tangent line through this point is usually an accurate approximation close to the point. It is called a linear or first-order approximation. f (x) ' f (a) + f 0 (a)(x − a) Example Estimate e0.1 . Hint use a = 0 and f (x) = ex , to find an approximate value for f (0.1). 2.4. LINEAR APPROXIMATIONS 2.4.2 79 Linear Approximations for f (x, y) The corresponding linear or first-order approximation for a function f (x, y) of two variables, near a known point (a, b) is the tangent plane. The linear approximation (i.e. tangent plane) is accurate close to the known point, provided f is smooth. The linear approximation to f (x, y) at (a, b) is f (x, y) ' f (a, b) + fx |(a,b) (x − a) + fy |(a,b) (y − b). Example. If the temperature in a region is given by T (x, y) = 100 − x2 − y 2 . Find a linear approximation to T (x, y) near (0, 5). 2 2 Tangent plane at (0,5,75) is z=125−10y z=100−x −y 100 80 60 40 20 −2 −4 −2 0 2 x 0 4 2 6 8 4 y 80 2. PARTIAL DERIVATIVES AND TANGENT PLANES 2 Exercise. Find the tangent plane to e−x sin y at (1, 2 approximate value for e−(0.9) sin(1.5). π ) and use it to find an 2 Tangent Plane at (1,π/2,1/e) to 2 −x z=e 2 siny 1.5 1 0.5 0 −0.5 −1 −1.5 −5 0 4 5 −4 Tangent plane −2 0 is2 z=(1−2(x−1))/e 2.4. LINEAR APPROXIMATIONS 2.4.3 81 Estimating Small Changes We may rewrite the tangent plane equation as ∆z ≈ fx (a, b)∆x + fy (a, b)∆y where ∆x = (x − a), ∆y = (y − b) and ∆z = z − f (a, b) represent small change in x, y and z respectively. This is useful for estimating small changes in z arising from small changes in x and y. Exercise Electric Power is given by P (E, R) = E2 R where E is the voltage and R is the resistance. Find a linear approximation for P (E, R) if E ' 200 volts and R ' 400 ohms. Use this to find the effect that a change in E and R has on P . 82 2.4.4 2. PARTIAL DERIVATIVES AND TANGENT PLANES Estimating Error Similarly, if the error in x is at most E1 and in y is at most E2 , then a reasonable estimate of the worst case error in f is |E| ≈ |fx (a, b)E1 | + |fy (a, b)E2 |. Exercise Suppose when making up a metal barrel of base radius 1 m and height 2 m, you allow for an error of 5% in radius and height. Estimate the resulting error in volume? 2.4. LINEAR APPROXIMATIONS 2.4.5 83 Differentials If we reduce our small changes ∆x and ∆y down to infinitesmal changes dx and dy, we may rewite the linear approximation (tangent plane) as dz = df = fx (a, b) dx + fy (a, b) dy. This infinitesmal change dz is called the total differential. Note, this equation is not just an approximation. Example Work Done by a Gas If the work done by an expanding gas, is given by W = P V , where P denotes pressure, and V volume, write an expression for the total differential dW . 2.4.6 Main Points • You should be able to work out total differentials for a given function of several variables. • You should be able to calculate finite small deviations of the main function, given a small change in one of the variables. Note that this is a computationally more efficient method of approximation than simply evaluating the function at x and x + ∆x, and then taking the difference. • You should be able to estimate error using the linear approximation 84 2.5 2. PARTIAL DERIVATIVES AND TANGENT PLANES Gradients and Directional Derivatives The partial derivatives we have seen so far allow us to find the slope of z = f (x, y) ∂f ∂f and in the y direction: . We now turn our attention to in the x direction: ∂x ∂y the question of slope in arbitrary directions such as in the y = x direction. This is covered in Stewart, Section 15.6. To describe directions in the x − y plane we use unit vectors. Recall that i and j are unit vectors in the x and y directions respectively. A unit vector is a vector whose magnitude is 1. v̂ = v kvk Example Find the slope of f (x, y) = 4 − x2 − 4y 2 at (a, b) in the y = x direction. 1 The vector û = √ (i + j) is a unit vector in the y = x direction. 2 If we move a short distance h in this direction, then the horizontal displacement is h h hu = √ i + √ j 2 2 i.e. our x and y displacements are h ∆x = √ 2 h ∆y = √ 2 The corresponding change in height is h h ∆f = f (a + √ , b + √ ) − f (a, b). 2 2 Since h is small, the linear approximation ∆f ' ∂f ∂f ∆x + ∆y. ∂x ∂y = ∂f h ∂f h √ + √ ∂x 2 ∂y 2 gives and the approximate slope in this direction is ∆f 1 ∂f 1 ∂f '√ +√ . h 2 ∂x 2 ∂y 2.5. GRADIENTS AND DIRECTIONAL DERIVATIVES 85 Now let h → 0. 1 1 The rate of change of f in the u = √ i + √ j direction is 2 2 1 ∂f 1 ∂f √ +√ 2 ∂x 2 ∂y Exercise Find the slope of f (x, y) = 4 − x2 − 4y 2 at (1, 1) in the y = x direction. The rate of change of f in the direction u is called a directional derivative. 2.5.1 Directional Derivatives The directional derivative in the direction û = u1 i + u2 j at (a, b) is ¯ ¯ ∂f ¯¯ ∂f ¯¯ u1 + u2 . fu (a, b) = ∂x ¯(a,b) ∂y ¯(a,b) This is the slope of the surface f (x, y) in the direction û, where kûk = 1. 86 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example. If f (x, y) = x2 − 3y 2 + 6y, find the slope at (1, 0) in the direction i − 4j. 2.5.2 The Gradient Vector ∇f The gradient vector of f (x, y) consists of its partial derivatives grad f = ∇f = (fx , fy ) = fx i + fy j. Example Find the gradient vector of f (x, y) = x2 − 3(y − 1)2 + 3. Similarly, for a function f (x, y, z, t) of more than two variables, grad f = ∇f = (fx , fy , fz , ft ) 2.5. GRADIENTS AND DIRECTIONAL DERIVATIVES 87 So the directional derivative can be conveniently expressed as fu = ∇f · û = fx u1 + fy u2 . Example. Find the slope of f (x, y) = 1 − x2 − y 2 at (0, 1) in the direction (i − j). 2 Example Find the directional derivative of g(x, y) = ex cos y at (1, π) in the direction (−3i + 4j). 88 2.5.3 2. PARTIAL DERIVATIVES AND TANGENT PLANES Properties of the Gradient Vector Consider the contour diagram of a plane z = f (x, y) = mx + ny + c. The contours −m have slope in the (x, y) plane. n n The gradient vector, mi + nj, has slope and so is perpendicular to the contours. m It points in the direction of increasing f . In fact the direction in which it points is the direction of greatest slope. Consider f (x, y) = x2 +y 2 . The contours are circles, and ∇f = 2xi+2yj which points radially out. Once again, ∇f points in the direction of greatest slope, perpendicular to the contour lines. 2.5. GRADIENTS AND DIRECTIONAL DERIVATIVES In general, two important properties of the ∇f are The direction of ∇f (a, b) is perpendicular to the contour line through (a, b) and in the direction of increasing f . In fact, the direction and magnitude of steepest slope at (a, b) is given by ∇f (a, b). We can also prove these two facts by considering the value of cos θ in fu = ∇f · û = |∇f ||û| cos θ. 89 90 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example T (x, y) = 20 − 4x2 − y 2 describes the temperature on the surface of a metal plate. Here x and y are in cm and T is in ◦ C. In what direction from (2, −3) does the temperature increase most rapidly? 2.5. GRADIENTS AND DIRECTIONAL DERIVATIVES 91 Example A team of oceanographers is mapping the ocean floor to assist in the recovery of a sunken ship. Using sonar, they develop the model: ³ πy ´ D = 250 − 30x2 − 50 sin , 2 where x and y are distance in km, D is depth in metres, and −2 ≤ x ≤ 2 and −2 ≤ y ≤ 2. (a) The ship is located at (1, 0.5). What is its depth? (b) Determine the steepness of the ocean floor in the positive x direction and in the positive y direction. (c) Determine the magnitude and direction of greatest rate of change of depth from the position of the ship. 92 2. PARTIAL DERIVATIVES AND TANGENT PLANES 2.5.4 Main Points • You should be able to find the slope of a function in a given direction. • you should be able to find the direction and rate of steepest increase of a function • You should understand directional derivative and gradient 2.6. THE CHAIN RULE AND APPLICATIONS 2.6 93 The Chain Rule and Applications The chain rule is covered in Stewart, Section 15.5. 2.6.1 Review for f (x) For related functions of one variable, such as y = f (u) and u = g(x), we may use dy the chain rule to find dx . dy dy du = dx du dx Example Find dy dx or f 0 (x) = f 0 (u) g 0 (x) where y = f (x) = (x2 + 1)5 . Example. Suppose the radius of a cylinder is decreasing at a rate r0 (t) = −2 cm/s. How fast is the volume decreasing when r = 1 cm and h = 2 cm? 94 2. PARTIAL DERIVATIVES AND TANGENT PLANES 2.6.2 Chain Rule for f (x, y) Suppose we have f (x, y), where x and y are both functions of t and we want df : dt df ∆f f (x(t + ∆t), y(t + ∆t)) − f (x(t), y(t)) = lim = lim . ∆t→0 dt ∆t→0 ∆t ∆t Now if f is smooth and ∆f is small, we can relate it to ∆x and ∆y through the linear approximation: and so ∆f ' fx ∆x + fy ∆y ∆f ∆x ∆y ' fx + fy . ∆t ∆t ∆t Then we can let ∆t → 0, and if x(t) and y(t) are smooth too, we have df dx dy = fx + fy dt dt dt which is the Chain Rule for f (x(t), y(t)). Exercise (cont’d) Now suppose that the height h is also decreasing, and as before dr = −2 cm/s. What is the rate of change of volume? dt The chain rule can be extended to any number of dimensions, e.g. if V (a(t)), b(t), c(t)) = abc is the volume of a box then dV dt = dh dt = −1 cm/s, 2.6. THE CHAIN RULE AND APPLICATIONS 2.6.3 95 Larger Chain Rules To keep check of what depends on what, you can draw a tree diagram for z = f (x(t), y(t)) as follows: z ∂z ∂x ∂z ∂y x y dx dt dy dt t So dz ∂z dx ∂z dy = + . dt ∂x dt ∂y dt Or if z = f (u(x(t), y(t)), v(x(t), y(t))) then the tree diagram is ∂z ∂u ∂u ∂y u ∂z ∂v z ∂v ∂x v ∂v ∂y ∂u ∂x y x dx dt So t dy dt µ ¶ µ ¶ ∂z ∂u dx ∂u dy dz ∂z ∂v dx ∂v dy = + + + dt ∂u ∂x dt ∂y dt ∂v ∂x dt ∂y dt µ µ ¶ ¶ ∂z ∂u ∂z ∂v dy ∂z ∂u ∂z ∂v dx + + + . = ∂u ∂x ∂v ∂x dt ∂u ∂y ∂v ∂y dt 96 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example. Find the slope of z = f (x, y) = sin x cos y in the radial direction at ∂z (π, 2π). That is find . ∂r Exercise Given h = f (x, y, z) and each of x, y and z depend on both s and t, write ∂h an expression for . ∂t 2.6. THE CHAIN RULE AND APPLICATIONS 2.6.4 97 Implicit Differentiation Sometimes we cannot solve an equation explicitly for y, for example y + sin(x + y) = 0 or x3 − y 3 + xy + 1 = 0 We can however consider these as functions of two variables, F (x, y) = 0. This is called the Implicit Form. Note that the implicit form for the equation to a plane F (x, y, z) = ax+by+cz+d = 0 is more general than the function z = f (x, y) = mx + ny + k because the implicit form also includes vertical planes. When presented with the form f = F (x, y) = 0, it is sometimes of interest to dy calculate . To do this, consider both x and y as functions of x, and differentiate dx using the chain rule. df ∂F dx ∂F dy = + =0 dx ∂x dx ∂y dx so dy Fx = − dx Fy Example. Suppose x and y are related by: x3 + y 3 = 6xy; find dy/dx. A problem arises here when x = y = 0. If you produce the plot of the relationship, it becomes apparent what is happening, and why it fails. 98 2. PARTIAL DERIVATIVES AND TANGENT PLANES 2.6.5 Main Points • You should be able to differentiate a multivariable function of multivariable functions (using the chain rule). • You should be able to find dy from implicit forms. dx 99 3 Max and Min Problems on Surfaces The content of this chapter is found in Stewart, Chapter 15, Section 15.7 and some of 15.8. 3.1 Quadratic Approximation In the previous chapter, we looked at the first order, or linear approximation to a function. In this section we introduce the second order, or quadratic approximation to a function. 3.1.1 Review for f (x) (See Stewart 6th Ed, page 195) We noted in Section 2.4.1, that the height of a function f (x) can be approximated, near a value x = a, with a straight line f (x) ' f (a) + f 0 (a)(x − a) Adding a second order term improves the approximation f (x) ' f (a) + f 0 (a)(x − a) + f 00 (a) (x − a)2 . 2 This quadratic (or second order) approximation is, in fact, the parabola with the same height f (a), the same slope f 0 (a), and the same f 00 (a) when x = a. So, it is quite accurate near the point (a, f (a)). 100 3. MAX AND MIN PROBLEMS ON SURFACES Example Estimate e0.1 i.e. find linear and quadratic approximations for f (0.1) using a = 0 and f (x) = ex . Example Draw f (x) = cos(x) and its linear and quadratic approximations about the value x = a = 0. 3.1. QUADRATIC APPROXIMATION 3.1.2 101 Quadratic Approximations for f (x, y) This material is covered briefly in Stewart p. 969. The quadratic or second order approximation to f (x, y) near (a, b) is a function of the form Q(x, y) = h + mx + ny + ax2 + bxy + cy 2 which agrees with • the height f (a, b), • the slopes fx (a, b), fy (a, b) and • the second derivatives fxx (a, b), fxy (a, b), fyx (a, b), fyy (a, b) all at the point (x, y) = (a, b). (It is one of the quadric surfaces listed in Section 1.8) The Quadratic Approximation to f (x, y) near (a, b) is: Q(x, y) = f (a, b) + fx (a, b)(x − a) + fy (a, b)(y − b) + fxx (a, b)(x − a)2 + fxy (a, b)(x − a)(y − b) + fyy (a, b)(y − b)2 Exercise Check that heights, slopes and second derivatives of Q and f agree at the point (a, b). 102 3. MAX AND MIN PROBLEMS ON SURFACES Example Find the quadratic approximation of f (x, y) = 1 − x2 + y 2 + xy + x3 + x2 y 2 about the point (0, 0). If |x|, |y| < 0.1 then we are ignoring terms on the order of (0.1)3 = 0.001. In this example the function is a polynomial in x and y and we can almost read off the quadratic approximation. But in the next example we really need to use the formula. 3.1. QUADRATIC APPROXIMATION 103 Exercise Find the linear and quadratic approximations of 2 −y 2 z = f (x, y) = e−x about the point (0, 0). 104 3. MAX AND MIN PROBLEMS ON SURFACES Exercise Find the quadratic approximation to z = f (x, y) = x2 y at (1, 2). 3.1. QUADRATIC APPROXIMATION 105 The quadratic approximation can be used in applications to get a better estimate of error. Example Consider a barrel, again with base radius 1 and height 2. V (r, h) = πr2 h ⇒ V (1, 2) = 2π ∂V = 2πrh ∂r ⇒ ∂V (1, 2) = 4π ∂r ∂V = πr2 ∂h ⇒ ∂V (1, 2) = π ∂h ∂2V = 2πh ∂r2 ⇒ ∂2V (1, 2) = 4π ∂r2 ∂ 2V = 2πr ∂r∂h ⇒ ∂ 2V =0 ∂h2 ∂ 2V (1, 2) = 2π ∂r∂h . So V (r, h) ' Q(r, h) = 2π + 4π(r − 1) + π(h − 2) + 4π (r − 1)2 + 2π(r − 1)(h − 2) + 0 2 for (r, h) ' (1, 2). Let ∆r = r − 1 and ∆h = h − 2. Now if ∆r = 0.05 and ∆h = 0.1, what is the approximate error in volume ∆V ? 106 3. MAX AND MIN PROBLEMS ON SURFACES Last time we used linear approximations to get 0.3π. Using quadratic approximations, 3.1.3 Main Points • You should be able to find the quadratic approximation given a function of two variables. • In Matlab, you should be able to plot both and to make a comparison of the errors. • The quadratic approximation is superior to the linear approximation (tangent plane). 3.2. CRITICAL POINTS AND OPTIMIZATION 3.2 3.2.1 107 Critical Points and Optimization Review for f (x) Smooth functions of one variable y = f (x) have local maxima and minima where f (x) has zero slope, i.e. where the first derivative of f is zero df = 0. dx The second derivative test tells us whether f (x) is concave up (a minimum) or concave down (a maximum): d2 f > 0 ⇒ Local minimum, dx2 d2 f < 0 ⇒ Local maximum. dx2 d2 f =0 ⇒ ? dx2 Example Find the local maxima and minima of y = f (x) = x2 − x4 . 108 3. MAX AND MIN PROBLEMS ON SURFACES Note, however, that the local minimum in the above example is not a global minimum. At the local minimum, x = 0 and f (0) = 0. But for |x| > 1, we have f (x) < 0. If we define the domain as [−2, 2], the global minimum occurs on the boundary at f (±2) = 4 − 16 = −12. 1 1 1 1 The global maximum, however, is at the local maximum, i.e. f ( √ ) = − = . 2 4 4 2 The global maximum or minimum of a continuous function f (x) occurs either at a local minimum, a local maximum, or on the boundary. In some circumstances there may be local maxima or minima when a derivative is not even defined (at cusps, for instance). df dx Example. f (x) = |x − 3|. A more general definition of a local minimum or maximum is: • A local minimum occurs at a point x0 where f (x) > f (x0 ) for all x near to x0 . • A local maximum occurs at a point x0 where f (x) < f (x0 ) for all x near to x0 . If f (x) is continuous, a local maximum or minimum occurs either where df df = 0 or where is undefined. dx dx 3.2. CRITICAL POINTS AND OPTIMIZATION 3.2.2 109 Critical Points for f (x, y) In this section we find and classify critical points of a function of two variables (see Stewart, Section 15.7. There are also some useful Matlab functions which perform minimisation (fmin and fminsearch). First we define local maxima and local minima: • f (x, y) has a local maximum at (a, b) if f (a, b) ≥ f (x, y) for all (x, y) near to (a, b) • f (x, y) has a local minimum at (a, b) if f (a, b) ≤ f (x, y) for all (x, y) near to (a, b). If f is differentiable, a local maximum or minimum occurs where the tangent plane ∂f ∂f is horizontal, i.e. where = 0 and = 0. ∂x ∂y We can put this more succinctly by using the gradient operator ∇f = ∂f ∂f i+ j ∂x ∂y and noting that ∇f = 0, if and only if, tangent plane at (a, b) is horizontal). ∂f ∂f = 0 and = 0 (if and only if, the ∂x ∂y So, when f is differentiable, the local maxima and minima occur where ∇f = 0. Note that if either ∂f ∂f or is undefined then ∇f is undefined. ∂x ∂y Definition. A critical point of z = f (x, y) is a point where either ∇f = (0, 0) or when ∇f is undefined. Local maxima and minima occur at critical points, but not all critical points are local maxima or minima: they may be saddle points. 110 3. MAX AND MIN PROBLEMS ON SURFACES Exercise. Find the critical points of z = f (x, y) = x2 + 3x + y 2 . Exercise. Find the critical points of z = f (x, y) = x2 + 3x − y 2 . Definition. A function f has a saddle point at P0 if P0 is a critical point of f and if within any distance of P0 (no matter how small) there are points P1 and P2 with f (P1 ) > f (P0 ) and f (P2 ) < f (P0 ). 3.2. CRITICAL POINTS AND OPTIMIZATION 111 Exercise. Find all the critical points of µ ¶ 1 2 2 2 2 f (x, y) = − x + y e(1−y −x ) . 2 Then use a quadratic approximation to investigate the curvature. ∂f = ∂x ∂f ∂y = ∇f = 0 ⇒ ∂f = 0 and ∂x ∂f =0 ∂y ⇒ There are four cases to consider: (1) x = 0 and y = 0; (2) x = 0 and − 12 − x2 + y 2 = 0; (3) 3 − x2 + y 2 = 0 and y = 0; 2 3 1 − x2 + y 2 = 0 and − − x2 + y 2 = 0. 2 2 Solving each of these gives the critical points r 1 ), (1) (0, 0), (2) (0, ± 2 (4) Note that case (4) has no solution. r (3) (± 3 , 0). 2 112 3. MAX AND MIN PROBLEMS ON SURFACES 2 2 2 2 z = ((1/2) − x + y ) exp(1 − x − y ) 2 1.5 1 0.5 0 −0.5 −1 3 2 3 1 2 0 1 0 −1 −1 −2 y Now ∂ 2f = ∂x2 so ∂2f ∂x2 ∂2f ∂x2 ∂ 2f (0, 0) = 2 à ∂x r ! 1 0, ± = 2 à r ! 3 ± ,0 = 2 −3 −2 −3 x 3.2. CRITICAL POINTS AND OPTIMIZATION Next, ∂ 2f = ∂x∂y so ∂2f ∂x∂y ∂2f ∂x∂y ∂ 2f (0, 0) = ∂x∂y à r ! 1 0, ± = 2 à r ! 3 ± ,0 = 2 Finally, ∂ 2f = ∂y 2 so ∂2f ∂y 2 ∂2f ∂y 2 ∂ 2f (0, 0) = ∂y 2 à r ! 1 0, ± = 2 à r ! 3 ± ,0 = 2 113 114 3. MAX AND MIN PROBLEMS ON SURFACES The quadratic approximation at (0, 0) is thus Q(x, y) = The point (0, 0) is a saddle point. r The quadratic approximation at (0, ± 1 ) is 2 Q(x, y) = r 1 ) are local maxima. 2 r 3 , 0) is The quadratic approximation at (± 2 The points (0, ± Q(x, y) = r The points (± 3 , 0) are local minima. 2 3.2. CRITICAL POINTS AND OPTIMIZATION 115 So, the signs of x2 and y 2 in the quadratic approximation usually show whether f (x, y) has a maximum, minimum or saddle. However, what if Q(x, y) has cross terms such as xy ? Example. The function f (x, y) = x2 + xy + y 2 has a critical point at (0, 0). We can complete the square, So f (x, y) has a local minimum at (0, 0). Now consider the function f (x, y) = x2 + xy + 1 y2 = (x + y)2 − 8 2 1 = (x + y)2 − 2 This function has a saddle point at (0, 0). But can we get a rule? y2 y2 + 4 8 2 y . 8 116 3. MAX AND MIN PROBLEMS ON SURFACES Suppose our quadratic approimation to f (x, y) is Q(x, y) = ax2 + bxy + cy 2 ! õ ¶2 b2 y 2 c 2 by = a x+ − 2 + y 2a 4a a õ ! ¶2 by (4ac − b2 ) 2 = a x+ + y . 2a 4a2 Then a > 0 and a < 0 and 4ac − b2 > 0 2 implies f has a local minimum, 4ac − b > 0 implies f has a local maximum, 4ac − b2 < 0 implies f has a saddle. Applying this argument to our quadratic approximation formula Q(x, y) = f (a, b) + fx (a, b)(x − a) + fy (a, b)(y − b) + fxx (a, b)(x − a)2 + fxy (a, b)(x − a)(y − b) + fyy (a, b)(y − b)2 gives The Second Derivative Test for f (x, y) If f (x, y) and its first and second order partial derivatives are all continuous at the point (a, b) and ∇f (a, b) = (0, 0), define ¯ ¯ f (a, b) fxy (a, b) D = ¯¯ xx fyx (a, b) fyy (a, b) ¯ ¯ ¯ ¯ then fxx (a, b) > 0 and D>0 implies (a, b) is a local minimum fxx (a, b) < 0 and D>0 implies (a, b) is a local maximum D<0 implies (a, b) is a saddle. However, if D = 0 the second derivative test is inconclusive. 3.2. CRITICAL POINTS AND OPTIMIZATION 117 The beauty of this test is that we do not even have to write down the quadratic approximation. Exercise. Find the critical points of z = f (x, y) = e−x saddle points, local maxima or local minima. 2 +y 2 and classify them as 118 3. MAX AND MIN PROBLEMS ON SURFACES Exercise. Find and classify the critical points of z = f (x, y) = x2 + 4xy + z = f(x,y) = x2 + 4xy + y3/3, −2 < x < 2, −2 < y < 2 25 20 15 10 5 0 −5 −10 −15 2 1 2 1 0 0 −1 y −1 −2 −2 x y3 . 3 3.2. CRITICAL POINTS AND OPTIMIZATION 3.2.3 119 Global Maxima and Minima To find the global maximum or minimum values of f (x, y) over a closed and bounded domain D, we need to compare both • the values of f at the critical points (where ∇f = 0 or undefined), and • the extreme values of f on the boundary curves of the domain. Example What are the global maximum and minimum values of the function z = f (x, y) = x2 + 4xy + y3 on the domain [−2, 2] × [−2, 2]? 3 120 3. MAX AND MIN PROBLEMS ON SURFACES 3.2.4 The Matlab comand fminsearch The Nelder-Mead algorithm is a search method built into the Matlab function fminsearch. One advantage of this method is that it works with nonsmooth functions as well, so it extends beyond traditional calculus methods. The syntax is that fminsearch will try to minimise a function f which you must store as a function in an M-file. Then you give fminsearch a starting point and let it run. For example, to minimise f (x, y, z) = (x − 1)2 + (y − 2)2 + (z − 3)2 , use the following code: function output = f(x) output = (x(1)-1)^2 + (x(2)-2)^2 + (x(3)-3)^2; Note that x(1) corresponds to x, x(2) to y and x(3) to z. This is because fminsearch requires that f takes a single vector as input (here the input is a three dimensional vector x). Now, to run the minimisation, call fminsearch(’f’,[0,5,10]) where x = 0, y = 5, z = 10 is the initial guess. For functions with several local minima, fminsearch can get stuck within a local minimum. Starting it with different initial guesses or restarting it when it thinks it has converged may help it to look for ‘better’ minima. How would you use fminsearch to find a maximum value of f ? 3.2.5 Main Points • You should know that the local minimum or maximum is obtained when either all the partial derivatives are zero or some of the partial derivatives are not defined. • You should know how to find critical points and classify them using either the quadratic approximation, or the second derivative test. • You should be able to find global maxima and minima of a function on a bounded domain. • You should be competent finding extrema with fminsearch. 3.3. CONSTRAINED OPTIMISATION AND LAGRANGE MULTIPLIERS 3.3 121 Constrained Optimisation and Lagrange Multipliers Lagrange multipliers are covered in Stewart, Section 15.8. Often in a problem you need to maximise some function, given that it satisfies a constraint. Example. Find the minimum value of x2 + y 2 when x + y = 1. We want to minimise f (x, y) = x2 + y 2 subject to the constraint g(x, y) = x + y = 1. In this case we can solve for the constraint and substitute back. Here g(x, y) = x + y = 1, so y = 1 − x. Substituting y = 1 − x into f (x, y) gives f (x, 1 − x) = F (x) = x2 + (1 − x)2 = 2x2 − 2x + 1 which is a function of x alone. The critical points of f occur when dF = −2 + 4x = 0 dx 1 So x = is a critical point (there is only one critical point in this case). Since 2 2 dF 1 = 4 > 0, it follows that F (x) has a minimum at x = . dx2 2 1 1 So x2 + y 2 achieves a minimum value at x = and y = . The minimum value is 2 2 1 = . 2 There is a better way to do it which involves gradients. Think graphically! Plot the contours of f (x, y) = x2 + y 2 and the constraint g(x, y) = 1 on the same graph. y 1 0.7 0.7 1 x z=0.5 z=1 z=1.5 x+y=1 122 3. MAX AND MIN PROBLEMS ON SURFACES The minimum occurs where the two curves just touch, which means that ∇f and ∇g are parallel. So ∇f = λ∇g for some constant λ; this constant is called the Lagrange Multiplier. So to minimise f (x, y) = x2 + y 2 subject to g(x, y) = 1, we solve ∇f = λ∇g. This method can be extended to any number of dimensions. Example. Minimise x2 + y 2 + z 2 subject to x + y + z = 1. 3.3. CONSTRAINED OPTIMISATION AND LAGRANGE MULTIPLIERS 123 The Method of Lagrange Multipliers To optimise f (x, y, z) subject to a constraint g(x, y, z) = c, solve ∇f = λ∇g with g(x, y, z) = c ; (λ is called the Lagrange Multiplier.) Example. Find the maximum volume of a box resting on the xy-plane with one vertex at the origin (0, 0, 0) and lying inside the plane z + 2x + 3y = 6. We want to maximise v = xyz subject to g(x, y, z) = 2x + 3y + z = 6. 124 3. MAX AND MIN PROBLEMS ON SURFACES Example - The Distance to a Plane We now return to the example from Chapter 1 on finding the distance from a point to the plane. The problem was to find the distance D from the point (x1 , y1 , z1 ) to the plane described by ax + by + cz + d = 0. In the context of this section of the notes, this means that we want to minimise D subject to the constraint ax + by + cz + d = 0. Now D is a function of (x, y, z), representing the distance from (x, y, z) to (x1 , y1 , z1 ): D= p (x − x1 )2 + (y − y1 )2 + (z − z1 )2 . Note that minimising D is the same as minimising D2 . So it’s easier to work with √ D2 (because it eliminates the operators) rather than with D directly. So our problem is to minimise D2 = (x − x1 )2 + (y − y1 )2 + (z − z1 )2 subject to the constraint F (x, y, z) = ax + by + cz + d = 0. Using the method of Lagrange Multipliers, we solve ∇(D2 ) = λ∇F, to get 2(x − x1 ) = λa, 2(y − y1 ) = λb, 2(z − z1 ) = λc. Solving this (along with ax + by + cz + d = 0) gives the same value for D as before, and in fact, you can obtain the (x, y, z) points which are on the plane and closest to (x1 , y1 , z1 ). 3.3.1 Main Points • You should understand that the method of Lagrange multipliers will find extremals subject to a constraint. • It is important that you can visualise Lagrange multipliers work by matching the direction of the gradients between the constraint and the optimising function. 125 4 Ordinary Differential Equations 4.1 4.1.1 Introduction Main Points In this section we shall cover the following questions and concepts: • What is a differential equation (DE)? • Examples of how DEs arise. • What does it mean to have a solution? • What is an Initial Value Problem (IVP)? dy = f (x). dx • What is meant by the order of a DE? • How to solve DE of the form In almost all fields of human scientific endeavour, quantifying time-dependent occurrences almost inevitably comes down to a differential equation. A differential equation (DE) is an equation which contains one or more derivatives of an unknown function. There are two types of DEs: • ordinary differential equations (ODEs) where the unknown function is a function of only one variable, and • partial differential equations (PDEs) where the unknown function is a function of more than one variable. We are not studying PDEs in this course. 4.1.2 Examples of ODEs • population growth dP dt = kP ¡ • logistic equation Ṗ = kP 1 − P K ¢ 2 • motion due to gravity m ddt2y = −mg • spring system mẍ = −kx • flow meter in a pipeline V 0 = 2 + sin(t) In ODEs, the independent variable is often t for time, and the unknown function is often called y(t). Also, derivatives y 0 (t) and y 00 (t) with respect to time are often written ẏ and ÿ respectively. See chapter 10 of the Stewart text. 126 4. ORDINARY DIFFERENTIAL EQUATIONS 4.1.3 How do these ODEs arise? 1. Assume population grows at a rate proportional to the size of the population. Identify the variables: • t = time (independent variable) • P = population (dependent on time) The model states that dP ∝P dt or dP = kP, dt where k is the constant of proportionality. 2. Motion due to gravity. Newton’s second law of motion (in one dimension) states mass × acceleration = force Let y be the vertical displacement from ground of a particle of mass m, then d2 y acceleration = 2 dt d2 y ⇒ m 2 = −mg, dt where g is the constant of acceleration due to gravity. 4.1.4 Solution to a DE Say we have a DE involving y, an unknown function of x. A function f is a solution of the DE if the equation is satisfied when y = f (x) and its derivatives are substituted into the equation. For the ODE y 0 = xy, y = f (x) is a solution means f 0 (x) = xf (x) for all values of x on some interval. 4.1. INTRODUCTION 4.1.5 127 1 2 Verify that y = Ae 2 x satisfies y 0 = xy. When asked to solve a DE, you are expected to find all possible solutions. You already know how to solve ODEs of the form dy d2 y = f (x), = g(x). dx dx2 What can we do with the constants of integration? 128 4. ORDINARY DIFFERENTIAL EQUATIONS 4.1.6 Initial Value Problem: Fluid Flow A flow meter in a pipeline measures flow-through as 2 + sin(t) litres/second. How much fluid passes through the pipeline from time zero up until time T ? The problem of solving an ODE that satisfies some initial condition of the form y(t0 ) = y0 is called a (first order) Initial Value Problem (IVP). 4.1.7 The Order of an ODE The order of the highest order derivative in an ODE gives the order of that ODE. • dP = kP dt • mẍ = −kx • x(y 00 )2 + y 0 y 000 + 4y 5 = yy 0 • y 0 = xy 4.1. INTRODUCTION 4.1.8 129 Extra reading: A Car at Constant Velocity Find the displacement of a car travelling at 60 km/h. If y is the distance covered by a car travelling at a constant speed of 60 km/h then dy = 60. Here we can simply integrate to solve for the position y(t); dt dy = 60 Z dt Z dy = 60 dt ⇒ or y = 60t + c To solve for c we need more information such as the initial position of the car. Solution Curves: For different values of c you will get different solutions. Here we graph some of these solutions. If c = 0 then y = 60t. All the other solutions are parallel to this line. Solution curves for the differential equation dy/dt = 6 6 4 y 2 0 −2 −4 −6 −6 −4 −2 0 t 2 4 6 The “curves” y = 60t + c are called solution curves to the differential equation dy = 60. dt 130 4. ORDINARY DIFFERENTIAL EQUATIONS They all have slope 60, just as the equation tells us. To solve for a particular solution we need to specify some point that the solution curve must pass through. Often the point is an initial condition y(0). In that case dy = 60 with y(0) = 2 is an initial value problem (IVP). the problem: Solve dt The solution y = 60t + 2 is called a particular solution. 4.1.9 Extra reading: Ice Thickness Ice is actually a good insulator (think of igloos). When a lake freezes over, a thin layer of ice forms and insulates the lake. This makes it harder for heat to leave the water, and the ice grows only a little more. This increase in ice thickness improves the insulation, so the water below is better insulated. Consequently, the ice grows √ dy even slower. A model for this behaviour is: = 2/ t, where y is the ice thickness dt and t is time. Find and sketch the solution curves of this equation. We will separate and solve this equation: dy 2 = √ dt Z Zt 1 dy = 2t− 2 dt 1 y = 4t 2 + C. Note when t = 1 the slope √ dy is 2 and when t = 4 the slope is 2/ 4 = 1, etc. dt General solution 10 9 8 7 6 5 4 3 2 1 0 0 0.5 1 1.5 2 2.5 t 3 In Matlab, the plotting command is: ezplot(’4*t^0.5+0’,[0,5,0,10]); hold on; ezplot(’4*t^0.5+2’,[0,5,0,10]); 3.5 4 4.5 5 4.2. MOTION OF PROJECTILES 4.2 4.2.1 131 Motion of Projectiles Main Points You should be able to • derive, using Newton’s second law, equations of motion in a force field (such as gravity) • solve these equations in certain simple cases, • be aware of the difference between an analytical and numerical solution. 4.2.2 Gravity in One Dimension Consider an apple falling under gravity. Find an expression for the height y of the apple. Note: In MATH1052, you are expected to derive solutions to the differential equations directly, rather than using formulas such as s = ut + 21 at2 . We will find that motions with nonconstant acceleration or other complications do not fit these formulas. 132 4.2.3 4. ORDINARY DIFFERENTIAL EQUATIONS In Two Dimensions Now suppose you are firing apples over the neighbours’ fence with the aim of hitting a target on the ground, 15 metres away. Find an expression for the position (x(t), y(t)) of the apple. 4.2. MOTION OF PROJECTILES 4.2.4 133 Example: Hitting a Target (a) If you throw at 20m/s, at what angle do you need to throw in order to hit a target 15m away? (b) What is the equation to the curve that the projectile traces in space? y a x 134 4. ORDINARY DIFFERENTIAL EQUATIONS 4.2. MOTION OF PROJECTILES 4.2.5 135 Realistic Models More realistic models of projectile motion deal with air resistance. • For fast moving objects (eg. rockets and bullets) a good model includes air resistance proportional to square of the velocity. ¯ ¯ dy ¯¯ dy ¯¯ d2 y = −g − f ¯ ¯ dt2 dt dt • For slower objects (throwing apples), a good enough model has air resistance proportional to velocity. Realistic models may also include the fact that gravity diminishes as you move further away from the Earth (Newton’s inverse square law). 4.2.6 A Model with Air Resistance Consider the motion of a ball suffering air resistance which is proportional to the square of the speed. Note that the air resistance vector is always directed against the direction of motion. Decompose the coordinates into x and y directions, and again apply Newton’s law F = ma. 136 4. ORDINARY DIFFERENTIAL EQUATIONS Note that: sin(θ) = √ ẏ ẋ2 +ẏ 2 , and cos(θ) = √ ẋ ẋ2 +ẏ 2 Then F = ma in the y direction gives: ⇒ ⇒ and similarly, mÿ = −mg − FD sin(θ) ⇒ mÿ = −mg − kv 2 sin θ ẏ mÿ = −mg − k(ẋ2 + ẏ 2 ) × p ẋ2 + ẏ 2 k p ÿ = −g − ẏ ẋ2 + ẏ 2 m k p 2 ẍ = − ẋ ẋ + ẏ 2 . m This is a coupled system of equations which is extremely difficult to solve analytically. However, numerical solutions are easy to perform, and you can undertake this a little later in the semester. A sample trajectory (x vs y) is shown in the figure above. There is no easy way to work with these equations. They cannot be solved analytically, but we can find accurate solutions numerically. Definitions • To solve a DE (IVP) analytically means to give a solution curve in terms of continuous functions defined over a specified interval, where the solution was obtained exactly by analytic means (eg. by integration). The solution then satisfies the DE (and initial conditions) on direct substitution. • To solve a DE (IVP) numerically means to use an algorithm to generate a sequence of points which approximates a solution curve. 4.3. SLOPE FIELDS AND EQUILIBRIUM SOLUTIONS 4.3 4.3.1 137 Slope Fields and Equilibrium Solutions Main Points By the end of this section, you should understand the following concepts: • How to generate and interpret slope fields. • What is meant by an equilibrium solution and how to find one. • How to determine the stability of an equilibrium solution. • How uniqueness of solutions to an IVP implies that solutions cannot cross. dy We have seen that to solve = f (t), we only need to integrate. However, for the dt general first order ODE dy = f (t, y) dt we cannot simply integrate. This equation, however, immediately gives us a qualitative picture: ⇒ at t = a, y = b, the slope of y(t) is f (a, b). • In the t-y plane, at (t, y) = (a, b) draw a small straight line with slope f (a, b). • Repeat the process for many different values of t and y. • Resulting diagram is called the slope field. Note the slope field can be generated without having to solve the ODE. 138 4.3.2 4. ORDINARY DIFFERENTIAL EQUATIONS Example: the slope field of y 0 = 2y Slope field for dy/dt=2y 1 0.8 0.6 0.4 0.2 0 −0.2 −0.4 −0.6 −0.8 −1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 dy From the slope field of = 2y we can see that y = 0 is one of the solution curves. dt It is a constant or equilibrium solution. 4.3.3 Equilibrium solutions An equilibrium solution is a constant solution to the ODE dy = f (t, y) dt The graph of an equilibrium solution is a horizontal line, so the slope is zero. ⇒ dye = 0 ⇒ f (t, ye ) = 0. dt 4.3. SLOPE FIELDS AND EQUILIBRIUM SOLUTIONS 4.3.4 139 Example: y 0 = −3(y − 1) Find the equilibrium solutions of dy = −3(y − 1). dt Slope field for dy/dt=−3(y−1) 3 2 1 0 −1 −2 −3 −3 −2 −1 0 1 2 3 140 4.3.5 4. ORDINARY DIFFERENTIAL EQUATIONS Example: y 0 = 2t + 1 Find the equilibrium solutions of dy = 2t + 1. dt Slope field for dy/dt=2t+1 3 2.5 2 1.5 1 0.5 0 −0.5 −1 −1.5 −2 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2 2.5 3 4.3. SLOPE FIELDS AND EQUILIBRIUM SOLUTIONS 4.3.6 141 Example: y 0 = y(1 − y) Find the Equilibrium Solutions of y 0 = y(1 − y). Slope field for dy/dt=y(1−y) 2 1.5 1 0.5 0 −0.5 −1 −1.5 −2 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2 142 4.3.7 4. ORDINARY DIFFERENTIAL EQUATIONS Stability of Equilibrium Solutions A pencil sitting balanced vertically is in an equilibrium state. But make one small perturbation and it will topple over. This is an unstable equilibrium. On the other hand, a pendulum hanging vertically is also in an equilibrium state. But if you perturb it slightly, it will eventually (with friction) return to its equilibrium. This is a stable equilibrium. From the slope field, you can decide if an equilibrium solution is stable or not. = f (t, y) Formally, an equilibrium solution y(t) = y0 to the differential equation dy dt is stable if the initial value problem: dy = f (t, y), dt y(0) = y0 ± ² has a solution y(t) which satisfies lim y(t) = y0 . t→∞ In other words, if you start sufficiently close to a stable equilibrium solution, then you will approach that equilibrium solution. Slope field for dy/dt=sin(4y) 1 0.8 0.6 0.4 0.2 0 −0.2 −0.4 −0.6 −0.8 −1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 4.3. SLOPE FIELDS AND EQUILIBRIUM SOLUTIONS 4.3.8 143 Example: Stable or Unstable? In the previous diagram, which equilibrium solutions are stable and which are unstable? 4.3.9 Existence and Uniqueness of Solutions Take an initial value problem dy = f (t, y), dt y(t0 ) = y0 . Then if f (t, y) is “smooth” in some rectangle about (t0 , y0 ) there is (i.e. exists) a unique solution y = y1 (t) at least in some neighbourhood of (t0 , y0 ). y y0 Rectangle t t0 It is an important point that equilibrium solutions cannot be crossed (by other solution curves). In fact no solutions curves can cross each other. Equilibrium solutions therefore divide up the (y, t) solution space nicely. 144 4.3.10 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Long-term Behaviour True or False: If a solution to dy = −3(y − 1) dt starts with y(t0 ) < 1 then it stays less than 2 (as time t increases). 4.3.11 Extra reading: consider the stability of ẏ = y 2 The only equilibrium solution in this case is y = 0. For y > 0, ẏ > 0 so that y(t) is increasing and hence any solution with y > 0 tends away from y = 0. For y < 0, ẏ > 0 so that y(t) is increasing and hence any solution with y < 0 approaches y = 0. Therefore y = 0 is neither stable nor unstable. 4.4. EULER’S METHOD FOR SOLVING ODE’S NUMERICALLY 4.4 4.4.1 145 Euler’s Method for Solving ODE’s Numerically Main Points You should know • how slope fields lead to Euler’s method. • how to approximate a solution to an IVP using Euler’s method by hand. • how to encode the Euler method in Matlab. • how errors can creep in to the approximate solution. The Stewart text (section 10.2) covers the Euler method. This technique gives a simple method to approximate solutions to ODEs and is closely related to the slope fields. 4.4.2 Revision: Equation of a Straight Line What is the equation of the straight line with slope m that passes through the point (a, b)? Euler’s method uses tangent lines as approximations to solution curves. The dy = f (t, y) at (t0 , y0 ) is tangent line to a solution curve of dt y = y0 + f (t0 , y0 )(t − t0 ) which approximates the curve when t is close to t0 . Now imagine a family of solution curves to the differential equation. We can calculate an approximate value for y at some later time by taking lots of small steps in time. At each step we will use the tangent line to the solution through that point. 146 4. ORDINARY DIFFERENTIAL EQUATIONS Let t1 = t0 + ∆t , t2 = t1 + ∆t, t3 = t2 + ∆t, ... , tn = tn−1 + ∆t. To find approximate y values at these times, use the following sequentially y1 = y0 + f (t0 , y0 )∆t y2 = y1 + f (t1 , y1 )∆t y3 = y2 + f (t2 , y2 )∆t and so on. 4.4.3 Example: y 0 = 2t, y(0) = 0 by Euler’s Method Use Euler’s Method with ∆t = 0.2, to find an approximate solution to y(0.6) for the initial value problem. dy = 2t, y(0) = 0 dt Compare your answer with the actual value. 4.4. EULER’S METHOD FOR SOLVING ODE’S NUMERICALLY 147 Of course the method is only accurate if you make ∆t small and then the number of steps you have to take is large. So use Matlab. Define a t vector and an initial y value, then use the for command to iterate. In the example ∆t = 0.05 and 12 steps have been chosen to take t to 0.6. t=(0:0.05:0.6); y(1)=0; for i=1:12 y(i+1)=y(i)+2*t(i)*0.05; end y(13) plot (t,y,’-’) Using Matlab we find y(0.6) = 0.33 which is much much better than the previous estimate value for y(0.6). Note that we start with y(1) = 0, whereas in the theoretical work we write y(0) = 0. Matlab does not allow you to put an index of zero. Keep this in mind in case you ever get the Matlab error message: ??? Index into matrix is negative or zero. 0.35 0.3 0.25 0.2 0.15 0.1 0.05 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 148 4.4.4 4. ORDINARY DIFFERENTIAL EQUATIONS Example. Euler’s Method using Matlab Given the differential equation dy = sin(xy), dx with initial condition y(0) = 1. Matlab code to estimate y(2), using Euler’s method with step size ∆x = 0.01 is: x=(0:0.01:2); y(1)=1; for i=1:200 y(i+1)=y(i)+sin(x(i)*y(i))*0.01; end y(201) plot (x,y,’-’) Using Matlab we find y(2) = 1.8243. 2 1.9 1.8 1.7 1.6 1.5 1.4 1.3 1.2 1.1 1 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 4.4. EULER’S METHOD FOR SOLVING ODE’S NUMERICALLY 4.4.5 149 Demonstrating errors in Euler’s method Use Euler’s method to approximate the solution curve of the initial value problem y 0 = y(1 − y), y(−2) = 2. Use the slope field below as a guide to draw in the approximate solution curve. What goes wrong in this case? Slope field for dy/dt=y(1−y) 2 1.5 1 0.5 0 −0.5 −1 −1.5 −2 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2 150 4. ORDINARY DIFFERENTIAL EQUATIONS 4.5 Separable ODEs By the end of this section, you should understand the following concepts: • How to identify a first order separable ODE • How to solve a separable ODE • How to solve the logistic equation (an important example) • The difference between an implicit and explicit solution Separable first order ODEs are one of several classes of ODE we will study in MATH1052. It is very important that you become skilled at identifying and solving these types of equations. 4.5.1 Definition A first order ODE is called separable if it can be written in the form dy = f (x)g(y). dx Which of the following ODEs are separable? • dy = y(1 − y) dt • dy = ex+y dx • y 0 = e(x+y) • ẏ = • 2 ty + y t2 dy = ty + y 2 . dt 4.5. SEPARABLE ODES 4.5.2 151 Solving separable ODEs 1. Rewrite the equation as 1 dy = f (x). g(y) dx 2. Integrate both sides with respect to x: Z Z 1 dy ⇒ dx = f (x) dx. g(y) dx 3. Note the integral on the LHS is a dy “substitution”, so that in the integral, dx can be replaced by dy: dx Z Z dy ⇒ = f (x) dx. g(y) Evaluating both integrals gives an implicit solution to the ODE. 4.5.3 Understanding implicit solutions: solve dy x = . dx y 152 4. ORDINARY DIFFERENTIAL EQUATIONS With an initial condition it may be possible to express the solution explicitly. 4.5.4 Solve the IVP dy x = , y(0) = 3. dx y 4.5.5 Solve the IVP dy sin x = , y(0) = 1. dx y 4.5. SEPARABLE ODES 153 dy = y. dx 4.5.6 Solve 4.5.7 Logistic equation: solve the IVP dy = y(1 − y), y(0) = y0 dt The logistic equation has many uses in population dynamics, physical science and even economics. 154 4. ORDINARY DIFFERENTIAL EQUATIONS 4.5. SEPARABLE ODES 4.5.8 155 Extra reading: Ice Thickness 2 Earlier we looked at the problem of water freezing over a pond. The ice acts as an insulator and slows the heat flow from the pond, so as the ice gets thicker, the growth of the ice slows. Find an expression for the ice thickness y, given dy 8 = dt y Simply rearrange this to give and y(0) = 0. Z Z ydy = and integrate to give 8dt 1 2 y = 8t + C 2 then make y the subject to give √ y = ± 16t + 2C. Of course, we choose the positive solution because thickness is positive, and if y = 0 √ when t = 0 then this means C = 0. So our solution is y = 4 t which is the same answer as before, but coming from a different equation. 4.5.9 Extra reading: dy −t = dt y Solve and sketch the solution curves in (t, y) space. Separate: y dy = −t dt y2 t2 =− +c 2 2 √ ⇒ y 2 + t2 = 2c circles with radius 2c. Integrate: Solution curves for the DE dy/dt = −t/y 6 4 y 2 0 −2 −4 −6 −6 −4 −2 0 t 2 4 6 156 4. ORDINARY DIFFERENTIAL EQUATIONS 4.6 Applications, Heat Equation, Population Growth By the end of this section, you should understand the following concepts: • How to apply Newton’s law of cooling and solve and the resulting IVPs. • How to model population growth and solve the resulting IVPs. • Interpret the obtained solutions. • At this stage you should be comfortable with solving separable first order ODEs. Here, and in chapter 10.4 of Stewart, we present some exponential models and extend these to more realistic non-linear models. 4.6.1 Newton’s law of cooling Newton’s Law of Cooling states that the rate at which a body cools is proportional to the difference between the temperature of the body and the surrounding temperature. So if T is the temperature of the body and S is the surrounding temperature then Rate of change of temperature = dT = −λ(T − S) dt where λ is the constant of proportionality. Note that this ODE is separable. Use this fact to solve it. 4.6. APPLICATIONS, HEAT EQUATION, POPULATION GROWTH 157 Often in a real problem λ is not given and you have to work it out from the information you are given. 4.6.2 Example: Police Report • Body discovered at 11 pm. • Doctor arrived at 11.30 pm., body temp 34.8◦ C. • Doctor took the temperature again at 12.30 and found the body temperature to be 34.1◦ C. • It was noted that the temperature of the room was 21.1◦ C. What was the time of death? (Normal body temperature is about 37◦ C). 158 4. ORDINARY DIFFERENTIAL EQUATIONS 4.6. APPLICATIONS, HEAT EQUATION, POPULATION GROWTH 4.6.3 159 Exponential Population Growth Without inhibiting factors the rate at which a population grows is proportional to the existing population. So if P is the population at time t then dP = rP, dt where r is then the growth rate per head per year. Under this model, how long will it take a population to double? Note: world population estimates put r ≈ 0.02. 160 4.6.4 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Rabbits If in two years a rabbit population trebles, how long does it take for the population to increase 9 fold (assuming the exponential model)? 4.6. APPLICATIONS, HEAT EQUATION, POPULATION GROWTH 4.6.5 161 Nonlinear Model of Population Growth In most animal populations some factor, such as over crowding or limited food supply, inhibits growth. To model this we introduce a nonlinear term which only becomes important if the population is large. µ ¶ rP 2 P dP = rP − = rP 1 − where L is large. dt L L We recognize this as the logistic equation. There are two equilibrium solutions: P = 0 and P = L. dP dP If 0 < P < L then > 0 ie P increases towards P = L and if L < P then <0 dt dt ie P decreases. P dy If we let y = and τ = rt the equation becomes = y(1 − y) which has the L dτ solution y0 eτ . y= (1 − y0 ) + y0 eτ So P = LP0 ert . (L − P0 ) + P0 ert For 0 < P0 < L we see that P → L as t → +∞ and P → 0 as t → −∞. Also for L < P0 we see that P → L as t → +∞. (L is called the Carrying Capacity.) 162 4.6.6 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Halibut The Pacific Halibut Fishery uses a logistic model plus an extra term to take into account their harvesting, which is at a rate proportional to the existing population (or mass P (t)). If E is the constant of proportionality for harvesting, the model is ¶ µ P dP − EP. = rP 1 − dt L t is measured in years, r = 0.71 per year, the carrying capacity without harvesting is L = 8 × 107 kg What restriction must be set on E so that the population does not die out? 4.6. APPLICATIONS, HEAT EQUATION, POPULATION GROWTH What value of E will ensure a maximum sustainable harvest? 163 164 4.6.7 4. ORDINARY DIFFERENTIAL EQUATIONS Extra reading: solving ODE’s Numerically Matlab has some more sophisticated numerical methods than Euler’s. Matlabs ode45 uses a Runge Kutta, fourth order integration technique. To use it you need to write two M -files. The first one defines the differential equation. Say call it logistic.m: function dy=logistic(t,y) dy=y*(1-y) Use a second M -file to integrate and plot your solution; y0=0.1; tfinal =6; [t,y]= ode45 (‘logistic’, tfinal, y0); plot (t,y) 4.7. SOLVING LINEAR FIRST ORDER EQUATIONS 4.7 165 Solving Linear First Order Equations At the end of this section you should understand: • How to identify a first order linear ODE. • How to solve a first order linear ODE using an integrating factor. If you cannot solve a differential equation by separation, your next plan of attack should be to test if it is linear and use the integration factor method described below. Further expositions and examples are given in Stewart section 10.5. 4.7.1 Definition A first-order ODE is linear if it can be put in the form dy + p(t)y = q(t) dt for some functions p(t) and q(t). Exercise: Which of the following are linear? • y0 = x • y 0 = lnx • y0 = y2 • y0 = y • y0y = 1 • y 0 = y + 3 sin x • y 0 = t5 − t2 y • y 0 = sin y + ex • (y 0 )2 = y 166 4. ORDINARY DIFFERENTIAL EQUATIONS Recall the product rule for differentiation: d dg df (f.g) = f + g. dt dt dt We previously used to change from the LHS to the RHS of this rule. The key to solving first order linear ODEs is to do the product rule in reverse. i.e. we start with the RHS and change it to the LHS. 4.7.2 dy + Example: dt µ 2t 2 t +1 ¶ y = t2 + 1 The lucky thing about the above example was that after multiplying by t2 + 1, the equation exactly matched the product rule. Question Can we always multiply a first order linear ODE by some function I(t), so that the LHS is in the same form as the product rule? dy + p(t)y = q(t) dt dy + I(t)p(t)y = I(t)q(t) dt dg df product rule: f + g dt dt −→ I(t) 4.7. SOLVING LINEAR FIRST ORDER EQUATIONS 167 If this is possible, then I(t)p(t) must be the derivative of I(t): dI = I(t)p(t). dt This condition is in fact a separable equation. We can solve this to find I(t). R A solution is I(t) = e p dt . The function I(t) is known as the integrating factor. Summary To solve a first order linear ODE, follow these steps: 1. Write the equation in the form dy + py = q. dt 2. Calculate the integrating factor R I=e p dt . 3. Multiply the ODE by I and apply the product rule in reverse to get d (Iy) = Iq. dt 4. Integrate both sides with respect to t (don’t forget the constant of integration) and write down the solution y(t). 168 4.7.3 4. ORDINARY DIFFERENTIAL EQUATIONS Example: dy = y + t. dt 4.7. SOLVING LINEAR FIRST ORDER EQUATIONS 4.7.4 Example: x2 dy + xy = 1 dx with 169 y(1) = 2. 170 4.7.5 4. ORDINARY DIFFERENTIAL EQUATIONS Extra reading: Electric Circuits The equation for the current in a circuit with a resistor and an inductor turns out to be linear. To get the equation consider the change in voltage around the circuit. The voltage drop due to a resistor is RJ where R is the resistance (in Ohms) and J is the current (in amps). R E L dJ where L is the inductance (henries). dt Now Kirchhoff’s Law tells us that the sum of the voltage drops is equal to the supplied voltage E(t) (volts). The voltage drop due to the inductor is L So E(t) = RJ + L dJ dJ or L + RJ = E(t). dt dt Example: Circuit Suppose in an L, R circuit a battery supplies a constant voltage of 80V, the inductance is 2H and the resistance is 10Ω. a) Find J(t). b) What is the current after 0.1 sec if J(0) = 0? 2 dJ dJ + 10J = 80 ⇒ + 5J = 40 dt dt Integrating factor µ(t) = e R 5dt = e5t ⇒ d 5t (e J) = 40e5t . dt Integrate 40 5t e +c⇒ 5 (b) Now if t = 0 then J = 8 + c. e5t J = J = 8 + ce−5t . So if J(0) = 0 then c = −8 and J(t) = 8 − 8e−5t . Now J(0.1) = 8 − 8e−0.5 = 3.1478 amps. 4.7. SOLVING LINEAR FIRST ORDER EQUATIONS 171 Example: Circuit 2 If a generator supplies a variable voltage, so that E(t) = 80 sin 3t, find an expression for J(t). dJ + 10J = 80 sin 3t dt dJ + 5J = 40 sin 3t. dt The Integrating factor is as before µ(t) = e5t 2 d 5t (e J) = 40e5t sin 3t. dt Integrate! Which means we need to know how to evaluate the integral Z A ≡ e5t sin 3t dt. ⇒ This can be done by integration by parts twice! Z Z udv = uv − vdu. − cos 3t Let u = e5t and dv = sin 3t dt ⇒ du = 5e5t dt and v = . 3 Z Z e5t cos 3t 5 5t e5t cos 3t dt. A = e sin 3t dt = − + 3 3 sin 3t Now let u = e5t and dv = cos 3t dt ⇒ du = 5e5t and v = . 3 µ ¶ Z e5t cos 3t 5 e5t sin 3t 5 5t + − e sin 3t dt A = − 3 3 3 3 e5t 25 = − (3 cos 3t − 5 sin 3t) − J 9 9 25 e5t ⇒ (1 + )A = − (3 cos 3t − 5 sin 3t) 9 9 e5t ⇒ A = − (3 cos 3t − 5 sin 3t). 34 Now back to the DE 40 e5t J = − e5t (3 cos 3t − 5 sin 3t) + c 34 40 ⇒ J(t) = − (3 cos 3t − 5 sin 3t) + ce−5t . 34 120 Now if J(0) = 0 ⇒ c = . 34 60e−5t 40 . The exponential term soon dies. So J(t) = − (3 cos 3t − 5 sin 3t) + 34 17 172 4. ORDINARY DIFFERENTIAL EQUATIONS 4.8 Linear Homogeneous Second Order Differential Equations with Constant Coefficients On completion of this section, you should understand the following: • How to identify a linear second order ODE. • The superposition principle for linear homogeneous ODEs. • The general solution theorem. • How to solve linear homogeneous second order ODEs with constant coefficients (and the corresponding IVPs). Second order ODEs are covered in Stewart in sections 18.1 and 18.2. They often correspond with unforced (homogeneous) oscillators in our applications, but they arise in many more problems. 4.8.1 Definition If the unknown function and its derivatives appear linearly in an ODE, the ODE is said to be linear. Otherwise it is nonlinear. Which are linear? • y 0 − y 00 = x + y • y 00 + y 0 = ylnx • y 0 = y 2 + y 00 • y 00 y 0 = y • y 0 y = y 00 • y 0 + x3 y 00 = xy + 3 sin x • y 0 = y 00 t5 − t2 y • y 00 = sin y + ex • y 00 + (y 0 )2 = y A second order linear ODE can always be written in the form dy d2 y + p(t) + q(t)y = r(t). 2 dt dt If r(t) = 0 it is said to be homogeneous. The functions p(t) and q(t) are called the coefficients. 4.8. LINEAR HOMOGENEOUS SECOND ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS 4.8.2 The Superposition Principle If y1 (t) and y2 (t) are solutions of d2 y dy + p(t) + q(t)y = 0, 2 dt dt then so is y(t) = c1 y1 (t) + c2 y2 (t). Proof: 173 174 4. ORDINARY DIFFERENTIAL EQUATIONS Theorem: General Solution If y1 (t) and y2 (t) are solutions of d2 y dy + p(t) + q(t)y = 0 2 dt dt such that y2 is not simply a multiple of y1 , then the general solution is y(t) = c1 y1 (t) + c2 y2 (t). 4.8.3 Constant Coefficients In MATH1052, we will study the case of constant coefficients. This corresponds to the case when p(t) = b and q(t) = c for constants b, c. dy = λy and dt This motivates the strategy for solving Note that if y = eλt then d2 y = λ2 y. dt2 dy d2 y + b + cy = 0. 2 dt dt We guess the solution is some exponential function y = eλt . Substituting into the equation gives d2 y dy + b + cy = 0 2 dt dt 2 λt λt ⇒ λ e + bλe + ceλt = 0 ⇒ (λ2 + bλ + c)eλt = 0 ⇒ λ2 + bλ + c = 0 since eλt 6= 0. This quadratic equation in λ is called the auxiliary or characteristic equation. The √ −b ± b2 − 4c . There are roots of this characteristic equation is are given by λ = 2 2 three cases to consider, all of which depends on the value of b − 4c (i.e. if it is positive, zero or negative). 4.8. LINEAR HOMOGENEOUS SECOND ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS 4.8.4 Case 1: Two real different roots r1 and r2 . This happens if b2 > 4c. In this case the general solution is y = c1 er1 t + c2 er2 t . Example: ÿ − 3ẏ + 2y = 0. 175 176 4.8.5 4. ORDINARY DIFFERENTIAL EQUATIONS Case 2: Equal roots This happens if b2 = 4c. b b So y1 = e− 2 t is a solution and since the equation is linear c1 e− 2 t is a solution for any constant c1 . To find a second solution we try a method called variation of b parameters. The idea is to look for a solution of the form y = v(t)e− 2 t where v(t) is some function of time. For example, to find the general solution of ÿ + 2ẏ + y = 0, the auxillary equation is r2 + 2r + 1 = 0 ⇒ (r + 1)2 = 0. So one solution is e−t . µ 2 ¶ dy dv −t d2 y d v 2dv −t −t Now let y = v(t)e ⇒ = e − ve and = − + v e−t . dt dt dt2 dt2 dt Substituting into the equation gives (v̈ − 2v̇ + v)e−t + 2(v̇ − v)e−t + ve−t = 0 ⇒ v̈ = 0 ⇒ v̇ = c2 and v(t) = c2 t + c1 . So y = (c2 t + c1 )e−t = c1 e−t + c2 te−t which is the general solution. For any problem with equal roots, the general solution is b b y = c1 e− 2 t + c2 te− 2 t . Example: ÿ + 6ẏ + 9y = 0, y(0) = 2, ẏ(0) = 0. 4.8. LINEAR HOMOGENEOUS SECOND ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS 4.8.6 177 Case 3: Complex Roots This case occurs when (b2 < 4c). Since the coefficients b and c of the auxillary equation r2 + br + c = 0 are real it follows that the complex roots √ −b ± b2 − 4c r= = α ± iβ 2 appear as α + iβ and its complex conjugate α − iβ. In the complex plane the general solution is y = D1 e(α+iβ)t + D2 e(α−iβ)t where D1 and D2 are complex constants. In real applications, however, we need real solutions. Recall Euler’s formula: e±iθ = cos θ ± i sin θ. So y+ = e(α+iβ)t = eαt ei(βt) = eαt (cos βt + i sin βt) and y− = e(α−iβ)t = eαt (cos βt − i sin βt) . We know that for linear, homogeneous ODEs, any linear combination of solutions is also a solution. It is possible to take linear combinations of the complex solutions we have to obtain real solutions. In particular, we take the linear combinations 1 y1 = (y+ + y− ) = eαt cos βt 2 and i y2 = − (y+ − y− ) = eαt sin βt. 2 The general solution is then y = Ay1 + By2 . 178 4. ORDINARY DIFFERENTIAL EQUATIONS 4.8.7 Example: ÿ + 9y = 0. 4.8.8 Example: ÿ − 4ẏ + 5y = 0, y(0) = 1 , ẏ(0) = 0. 4.8. LINEAR HOMOGENEOUS SECOND ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS 4.8.9 179 The Three Cases To solve ÿ + bẏ + cy = 0. Let y = ert then r is given by the auxillary equation r2 + br + c = 0. • Case 1. b2 > 4c ⇒ two real distinct roots r1 and r2 the general solution is y = c1 er1 t + c2 er2 t . • Case 2. b2 = 4c ⇒ equal roots r = − b b y = c1 e− 2 t + c2 te− 2 t . b the general solution is 2 • Case 3. b2 < 4c ⇒ complex roots r = α ± iβ the general solution is y = c1 eαt cos βt + c2 eαt sin βt. 4.8.10 Extra reading: numerical solutions Numerical solutions of second order ODEs usually require one second order equation to be split into two first order equations. Then the two coupled ODEs are solved together. For example, the equation z 00 + z 0 − z = cos(x), z(0) = 1, z 0 (0) = 2 can be converted into: z0 = y y 0 = −y + z + cos(x) with initial conditions z(0) = 1, y(0) = 1. Then an Euler method or ode45 can be used on these equations. For instance, the code to implement Euler’s method would look like: deltax=0.1; x(1)=0; z(1)=1; y(1)=2; for i = 1:100 x(i+1) = x(i) + deltax; z(i+1) = z(i) + deltax*y(i); y(i+1) = y(i) + deltax*(-y(i) + z(i) + cos(x(i))); end plot(x,z) 180 4. ORDINARY DIFFERENTIAL EQUATIONS 4.9 Linear Nonhomogeneous Second Order Differential Equations After this section you should understand the following concepts: • The strategy for guessing a particular solution for some specific functions on the RHS of a nonhomogeneous linear ODE. • This procedure of guessing is known as the method of undetermined coefficients. Second order ODEs with forcing (nonhomogeneous) are covered in Stewart in section 18.2. They are very common in applications and often they require a bit of ingenuity to solve. We will deal with second order nonhomogeneous linear differential equations with constant coefficients of the form: y 00 + by 0 + cy = r(x) where r is a continuous function. 4.9.1 Some Observations The function y = Ae9t + Be−3t is the general solution to ÿ − 6ẏ − 27y = 0 Exercise 1. Substitute the function y = Ae9t + Be−3t + t2 − 1 into the LHS of the same ODE. Take note of the non-zero right hand side that is produced. 4.9. LINEAR NONHOMOGENEOUS SECOND ORDER DIFFERENTIAL EQUATIONS 181 Exercise 2. Now substitute the function y = Ae9t + Be−3t + cos t into the LHS of the same ODE. Again, take note of the non-zero right hand side that is produced. From the above exercises we may note that • In both cases, the solution to the homogeneous equation had no effect on the right hand side. • Only the additional function (which had no arbitrary constants) influenced the RHS. Theorem The general solution of a nonhomogeneous linear ODE consists of two parts y = yH + yP • The homogeneous solution yH , is the solution to the corresponding homogeneous ODE. This part contains all the arbitrary constants and has no effect on the RHS. • The particular solution yP , is the solution which contributes to the non-zero RHS. It contains no arbitrary constants. 182 4. ORDINARY DIFFERENTIAL EQUATIONS 4.9.2 Example: dy + 3y = x dx The solution to the corresponding homogeneous ODE dy + 3y = 0 is dx yH (x) = ce−3x and the solution to this non-homogeneous ODE is y(x) = yH (x) + yP (x) 1 1 = ce−3x + ( x − ). 3 9 4.9.3 The Method of Undetermined Coefficients We already know how to find yH in the case of constant coefficients. To find yP there are several methods of solution. In MATH1052 we introduce one method called the method of undetermined coefficients. Based on the function appearing on the RHS, we make an educated guess for yP and substitute it into the ODE. • If the function on the RHS is a polynomial of order n, we guess the most general polynomial of order n. • If the function on the RHS is cos(at) or sin(at) for any a, then we guess yP = k cos(at) + l sin(at), and substitute it into the ODE to solve for k, l. • If the function on the RHS is eat for any a, then we guess yP = keat and substitute it into the ODE to solve for k. • If the function on the RHS is any sum of functions of the form of either polynomial, cos/sin or exponential, then the guess will be a sum of guesses corresponding to each term. However, if a first guess already appears in the homogeneous solution, then it must be multiplied by t to contribute to the RHS. If it is still in the homogeneous solution, keep multiplying by t until it is not. This method is studied in more detail in MATH2000. 4.9. LINEAR NONHOMOGENEOUS SECOND ORDER DIFFERENTIAL EQUATIONS 4.9.4 Example: y 00 − y = x2 + e2x 183 184 4.9.5 4. ORDINARY DIFFERENTIAL EQUATIONS Example: y 00 + y = 3 + sin x 4.9. LINEAR NONHOMOGENEOUS SECOND ORDER DIFFERENTIAL EQUATIONS 4.9.6 185 Extra reading: y 00 − y = −2x2 + 5 + 2ex The homogeneous equation is y 00 −y = 0 which has characteristic equation r2 −1 = 0, giving r = ±1 and so the homogeneous solution is yh = Aex + Be−x . We now seek a particular solution. From the ODE, it appears reasonable to guess a solution of the form yp = µx2 + νx + α + βex where µ, ν, α, β are constants. Don’t worry too much if the guess is not quite right: if there are too many functions, some of the constants will come to zero, and if there are not enough functions, you will get an indeterminate system that has no solution and you can try another guess. In our case, we do not need βex , because it is already part of the homogeneous solution. So we will extend the solution somewhat and try yp = µx2 + νx + α + βxex This would give us yp0 = 2µx + ν + βex + βxex and yp00 = 2µ + 2βex + βxex Substitute into the original ODE and µ = 2, ν = 0, α = −1 and β = 1. So yp = 2x2 − 1 + xex and the general solution is y = yh + yp = Aex + Be−x + 2x2 − 1 + xex for arbitrary constants A, B. 186 4. ORDINARY DIFFERENTIAL EQUATIONS 4.10 Simple Harmonic Motion and Damped Oscillations 4.10.1 Main Points • You should know how to derive and solve the equations of motion for a damped oscillator given just the mass, damping and spring constants. • You should also be able to classify the motion into under, over or critically damped, and know what these notions mean. This material is covered in Stewart section 18.3 and deals with the modeling of oscillators by second order ODEs. 4.10.2 Hooke’s law and Newton’s second law. • Hooke’s law - the force exerted by a spring is proportional to the displacement. F = −ky, force F , displacement y, spring constant k. • Newton’s second law of motion - the sum of forces acting on an object equals the product of its mass and acceleration. F = ma 4.10.3 Mass on a Spring. For a spring hanging under gravity with no damping (only the force exerted by the spring), we have d2 y m 2 = −ky. dt 4.10. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS Let’s derive this equation and find the general solution: 187 188 4.10.4 4. ORDINARY DIFFERENTIAL EQUATIONS Example A spring with mass 9kg and spring constant 4N/m is pulled down 1m and given a kick of −0.5m/s. Solve for position as a function of time and give the amplitude of oscillations. 4.10. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 4.10.5 189 Damped Oscillations It is realistic to also consider external forces which resist the motion, ie. are proportional to the velocity in the opposite direction to the motion. Such an effect is called damping. In this case, Newton’s second law gives d2 y dy m 2 = −ky − a , dt dt where a is a damping constant. Roots of the characteristic equation are λ= −a ± √ a2 − 4mk . 2m There are 3 cases to consider 1. “Overdamping”: a2 − 4mk > 0. 2. “Critical damping”: a2 − 4mk = 0. 3. “Underdamping”: a2 − 4mk < 0. 4.10.6 Overdamping: a2 − 4mk > 0. λ2 √ a2 − 4mk √2m −a − a2 − 4mk = 2m λ1 = −a + ⇒ y = Aeλ1 t + Beλ2 t . 190 4.10.7 4. ORDINARY DIFFERENTIAL EQUATIONS Example A mass of 1kg is attached to a spring hanging under gravity with damping constant 5 and spring constant 4. Find the position of the mass after time t if it is pulled 1m from the equilibrium position and released with no kick. 4.10. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 4.10.8 191 Critical damping: a2 − 4mk = 0. λ1 = −a 2m ⇒ y = Aeλ1 t + Bteλ1 t . 4.10.9 Example With the same mass and spring constant of the previous example, what value of the damping constant implies critical damping? 192 4.10.10 4. ORDINARY DIFFERENTIAL EQUATIONS Underdamping: a2 − 4mk < 0. √ −a ± i 4mk − a2 λ = 2m = α ± iβ ⇒ y = eαt (A cos(βt) + B sin(βt)). 4.10.11 Example With the same mass and spring constant of the previous example, describe the motion if the damping constant has value 0.2. 4.10. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 4.10.12 193 Summary of Motion of a Spring: d2 y dy + a + ky = 0 2 dt dt where m is the mass, a is the damping coefficient and k is the spring constant. m The auxiliary equation mr2 + ar + k = 0 has complex roots if a2 < 4km. r (1) a = 0 No damping - Pure oscillatory motion: r = ± r y = c1 cos √ (2) a < 2 km k t + c2 sin m r k t. m Damped oscillations: r = α ± iβ where α = − y = c1 eαt cos βt + c2 eαt sin βt. √ (3) a = 2 km Critical Damping: Equal roots r = − a a y = c1 e− 2m t + c2 te− 2m t . √ (4) a > 2 km k i m Over Damping: r1 < 0 and r2 < 0 y = c1 er1 t + c2 er2 t . a 2m a <0 2m 194 4.10.13 4. ORDINARY DIFFERENTIAL EQUATIONS Extra reading: the pendulum A Pendulum also executes simple harmonic motion and damped oscillations. Suppose a bob moves in an arc of a circle radius `. The distance moved by the bob is `θ. Newton’s 2nd law tells us d2 (`θ) = force in the direction of motion dt2 ⇒ m`θ̈ = −mg sin θ ⇒ `θ̈ = −g sin θ which is a non linear equation. mass × But if θ is small sin θ ' θ. So for θ small `θ̈ + gθ = 0. g = 0 is the auxillary equation. ` r g i r=± ` r r g g θ = c1 cos t + c2 sin t. ` ` r ` Oscillations period is 2π . g Let θ = ert ⇒ r2 + What is the effect of damping? As before damping will be proportional to velocity ⇒ `θ̈ + Rθ̇ + gθ = 0 where R is damping coefficient. Then auxillary equation is `r2 + Rr + g = 0 p R2 − 4`g −R ⇒r= ± . 2` 2` If R2 < 4`g √ R = 2 `g √ R > 2 `g r is complex ⇒ damped oscillations critical damping solutions are overdamped. More realistic modeling leads to nonlinear equations, like the true pendulum, which can have interesting and unexpected solutions. 195 5 Parametrisation of Curves and Line Integrals Stewart covers parametrisation in chapters 11 and some of 13. There are some features of Matlab, especially plotting which will work with parametric forms. 5.1 Parametrisation of Curves By then end of this section, you should understand what a parametric representation is, and you should be able to parametrise ellipses, circles, spirals, helices and piecewise linear paths (although we will cover piecewise linear paths in more detail in the next section). You should be able to plot parametric forms in Matlab and roughly sketch them on paper. Imagine a particle moving along a curve in the x-y plane. The x and y coordinates are functions of time so we could describe the curve by ½ x = f (t) y = g(t) These equations are called parametric equations. The third, auxilliary variable t is called a parameter. Each value of t determines a point in the x-y plane. As t varies, the point (x, y) = (f (t), g(t)) traces out the curve called a parametric curve. Parametric equations are especially useful for curves where y is not a function of x, nor x of y. 196 5.1.1 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: x = t2 − 2t, y = t + 1 Sketch and identify the curve defined by the these parametric equations Are there other parametric equations representing the same curve? Sometimes we restrict t to a finite interval. In general, the curve with parametric equations x = f (t), y = g(t), a ≤ t ≤ b starts at the point (f (a), g(a)) and finishes at the point (f (b), g(b)). 5.1.2 Example: x = t2 − 2t, y = t + 1, 0 ≤ t ≤ 4 5.1. PARAMETRISATION OF CURVES 5.1.3 The Circle A circle centred at (h, k) of radius r has equation (x − h)2 + (y − k)2 = r2 . Use the fact that cos2 θ + sin2 θ = 1 to parametrise the circle: 197 198 5.1.4 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: A Circle with Specified Starting Point Parametrise a circle with centre (2, 3) radius 2, starting at (0, 3) and traversing in a counter clockwise direction. y (x-2)^2 + (y-3)^2 = 4 3 x 2 5.1. PARAMETRISATION OF CURVES 5.1.5 199 The Ellipse An ellipse centred at (h, k) with semi-major axis of length A running parallel to the x-axis and semi-minor axis of length B has equation (x − h)2 (y − k)2 + = 1. A2 B2 Find parametric equations for the ellipse. 200 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS 5.1.6 The Spiral A spiral can be parametrised as a circle with a variable radius; ½ x(t) = r(t) cos(ωt + φ) ⇒ y(t) = r(t) sin(ωt + φ) t Example If r(t) = e− 100 the radius tends to 0 as t → +∞. x = e−t/100 cos(t) , y = e−t/100 sin(t) 0.8 0.6 0.4 y 0.2 0 −0.2 −0.4 −0.6 −0.8 −1 −0.5 0 x 0.5 1 • eg. If r(t) = e−t , the spiral would decay exponentially. • eg. If r(t) = t, the spiral would grow linearly. 5.1.7 The Helix In general, a curve in three dimensions can be paraemtrised in the form x = f (t), y = g(t), z = h(t). A helix is a three dimensional curve that has the wound shape of a spring, or a piece of string wrapped around a cylinder. One parametrisation is x(t) = cos t, y(t) = sin t, z(t) = t, 0 ≤ t ≤ 1. Note this parametrisation is not unique, since x = cos ωt, y = sin ωt, z = ωt, 0 ≤ t ≤ 1/ω parametrises the same helix. 5.1. PARAMETRISATION OF CURVES 5.1.8 201 Piecewise linear paths Sketch the curve parametrised by x(t) = 1, y(t) = t, 0 ≤ t ≤ 1, x(t) = 2 − t, y(t) = t, 1 ≤ t ≤ 2, x(t) = 2 − t, y(t) = 2, 2 ≤ t ≤ 3, 5.1.9 Extra reading: Matlab Plotting Parametrised Equations Early in the semester, you performed many types of plots, including parametric plotting. The ezplot function handles parametric plots, so to plot the ellipse in the earlier example, the command is simply ezplot(’2*cos(t)’,’1/3*sin(t)’) To plot the spiral, simply use ezplot(’exp(-t/100)*cos(t)’,’exp(-t/100)*sin(t)’) Executing this command indicates the default range for the t values. To get a longer spiral, 0 ≤ t ≤ 100, implement ezplot(’exp(-t/100)*cos(t)’,’exp(-t/100)*sin(t)’,[0,100]) 202 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS 5.2 Position Vectors, Velocity and Acceleration By the end of this section, you should understand the following concepts: • The relationship between a position vector and the parametric equations of a curve. • The relationship between position, velocity and acceleration vectors. Stewart covers the following material in section 14.4. Parametric equations are used to describe position, velocity and acceleration. 5.2.1 Position Vector The position vector r(t) = x(t)i + y(t)j traces out the path given by the parametrisation (x(t), y(t)). Similarly in three dimensions, the position vector r(t) = x(t)i + y(t)j + z(t)k traces out the path given by the parametrisation (x(t), y(t), z(t)). 5.2.2 Parametrising straight lines Say we want to parametrise the straight line between the points (a, b) and (c, d). We also require that (a, b) corresponds to parameter value t = α, and (c, d) corresponds to t = α + ∆t (where ∆t > 0). Let r 0 be the position vector for (a, b) and r 1 the position vector for (c, d). The vector v = r1 − r0 is parallel to the line between (a, b) and (c, d). A parametrisation for the line is then µ ¶ µ ¶ t−α α + ∆t − t t−α r(t) = r 0 + v= r0 + r1. ∆t ∆t ∆t 5.2. POSITION VECTORS, VELOCITY AND ACCELERATION 5.2.3 203 Example: Parametrise a piecewise linear path ?>=< Parametrise the path given in the following diagram, so that path 89:; 1 is given for ?>=< 89:; 0 ≤ t ≤ 1 and the path 2 is given for 1 ≤ t ≤ 3. (−1,0) 2 (−1,−1) 1 (0,−2) 204 5.2.4 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: position vector Find the position vector of any point on the line passing through the point (0, 1, 3) 1 and parallel to the vector 2i − 3j + k. 2 5.2. POSITION VECTORS, VELOCITY AND ACCELERATION 5.2.5 205 Velocity Vector The vector tangent to the path of motion, with magnitude equal to the speed is the velocity vector. If r(t) is the position vector, then ∆r = r(t + ∆t) − r(t) is approximately tangent to the curve traced out by r(t). The approximation gets better as ∆t → 0. So the velocity vector v is given by r(t + ∆t) − r(t) ∆t→0 ∆t ∆r = lim ∆t→0 ∆t dr = dt v(t) = lim In component form, if r(t) = x(t)i + y(t)j + z(t)k then the velocity is v(t) = dr dx dy dz = i + j + k. dt dt dt dt Speed is the magnitude of velocity sµ speed = kv(t)k = dx dt ¶2 µ + dy dt ¶2 µ + dz dt ¶2 Note that if an object moves with constant velocity, then that means it has constant speed and direction ie. it travels in a straight line. 5.2.6 Acceleration The acceleration vector is defined as a(t) = d2 x d2 y d2 z dv = 2 i + 2 j + 2 k. dt dt dt dt 206 5.2.7 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: Given constant v(t), find r(t) The velocity of an object is v = 2i + 3j + 4k and at t = 0 the object passes through (−2, 1, 0). Find the position vector. 5.2.8 Example: Find v(t) for a helical path Find the position and velocity vectors of a car moving on the helical path; x = cos 5t , y = sin 5t , z = 2t . 5.2. POSITION VECTORS, VELOCITY AND ACCELERATION 5.2.9 207 Quiz Which position vector fits which curve? Locate the starting point in each case. 3 (1) r(t) = −ti + ( − t)j 2 (2) r(t) = i + (−1 + t)j 1 1 (3) r(t) = − sin ti + cos tj 2 2 (4) r(t) = 2 cos 3ti + 2 sin 3tj (I) (II) y y 0.5 x 1 x 0.5 -1 (III) (IV) y y 2 1.5 0.5 x x 2 -1 208 5.2.10 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Extra reading: stone on a string Imagine you spin a stone on a string above your head so that it moves in a circle of radius 1m, and 2m above the ground at a constant speed with period π seconds. If the string breaks find the position, velocity and acceleration vectors of the stone after the string breaks. Before the string breaks r(t) = cos 2ti + sin 2tj + 2k v(t) = −2 sin 2ti + 2 cos 2tj. So r(0) = i + 2k and v(0) = 2j. After the string breaks the stone moves under gravity ma(t) = −mgk So ⇒ a(t) = −gk ⇒ dv = −gk. dt v = −gtk + v(0) = 2j − gtk dr 1 (t) = v ⇒ r(t) = 2tj − gt2 k + r(0) dt 2 1 2 r(t) = i + 2tj + (2 − gt )k. 2 5.3. LINE INTEGRALS AND WORK DONE 5.3 209 Line Integrals and Work Done By the end of this section, you should understand the following concepts: • How to use a line integral to calculate the arc length of a curve. • How to use a line integral to calculate the work done by a field of force. The content of this chapter is covered in Stewart over a few sections: Arclength is in 9.1 and 11.4 and general line integrals are in 17.2. 5.3.1 Length of a Curve Say we have a curve in R3 represented by parametric equations x = x(t), y = y(t), z = z(t), a ≤ t ≤ b. We aim to calculate the length of the curve. This is commonly referred to as the arc length. We first approximate the curve as a series of straight lines: 210 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS In this case, with ∆si representing the length of a small piece of the curve corresponding to a small change ∆t in the parameter, and given r(t) = x(t)i+y(t)j+z(t)k, we have the approximation ¯ ¯ ¯ dr ¯ ∆si ≈ ¯¯ ¯¯ ∆t. dt The length of the curve is approximately the sum of the lengths of each line segment: 5.3. LINE INTEGRALS AND WORK DONE 211 This approximation improves for smaller (and hence more) line segments. In the limit as the number of line segments → ∞, we obtain an exact result, giving the arclength formula s Z b µ ¶2 µ ¶2 µ ¶2 dx dy dz arclength = + + dt dt dt dt a Z = ds (example of a line integral). C 5.3.2 Example: Length of a Helix Find the length of the helix x = cos 5t , y = sin 5t , z = 2t , 0 ≤ t ≤ 2π. 212 5.3.3 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Work Done by a Constant Force In 1 dimension, work done by a constant force F in moving an object along a straight line of length d is W = F d. In 2 or 3 dimensions, if force is constant, work done in moving a particle along a −→ line from P to Q is W = F · P Q. 5.3. LINE INTEGRALS AND WORK DONE 5.3.4 213 Work Done over a Curve We now consider the more general case of the work done by a field of force (which may vary with position) F (x, y, z) = F1 (x, y, z)i + F2 (x, y, z)j + F3 (x, y, z)k and which moves an object along a curve C. First we give an approximation: divide C into n arcs, so that the ith arc has length ∆si . We approximate ∆si as a straight line (for ∆si small enough), and F as constant over ∆si by evaluating F at a specified point Pi on the arc. Let T (Pi ) be a unit tangent vector to the curve C at the point Pi . We then approximate the direction over the arc as the vector T (Pi )∆si . Thus the work done over the ith arc is approximately Wi ≈ F (Pi ) · T (Pi )∆si Summing up over all arcs gives an approximation to the total work done: X X W ≈ Wi ≈ F (Pi ) · T (Pi )∆si . Taking the limit ∆si → 0 gives Z W = F · T ds. C This is the definition of a line integral. 214 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS To evaluate the line integral, we use a parametrisation of the curve C. Let C be parametrised by r(t) = x(t)i + y(t)j + z(t)k a ≤ t ≤ b. We let ∆t represent a small change in t corresponding to ∆si . Let Pi correspond to parameter value ti . As we saw previously, as ∆si → 0, ∆si ≈ |r 0 (ti )|∆t, and we can write the unit tangent vector to the curve at the point Pi in terms of the parametrisation as r 0 (ti ) T (Pi ) = 0 . |r (ti )| Hence the work done over the ith arc is approximately Wi ≈ F (r(ti )) · r 0 (ti )∆t. 5.3. LINE INTEGRALS AND WORK DONE 215 Summing up over all arcs gives an approximation to the total work done: X X W ≈ Wi ≈ F (r(ti )) · r 0 (ti )∆t. Finally, taking the limit as ∆t → 0, we have Z b W = F (r(t)) · r 0 (t) dt. a It is common to express these integrals as Z F · dr. C This is merely a notational convenience. This notation means Z b Z F · dr = F (r(t)) · r 0 (t) dt, C a where C is parametrised by r(t), a ≤ t ≤ b. This is another example of a line integral. 216 5.3.5 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: Work on a Circular Path Find the work done by moving a book mass 2kg along an arc of the circle x2 + z 2 = h2 from (−h, 0, 0) to (0, 0, h). 5.3. LINE INTEGRALS AND WORK DONE 5.3.6 217 Extra reading: length of a spiral Find the length of the spiral t x(t) = e− 10 cos t t and t y(t) = e− 10 sin t for t ≥ 0. t r(t) = e− 10 cos ti + e− 10 sin tj µ ¶ µ ¶ t t 1 −t 1 −t − − v(t) = − e 10 cos t − e 10 sin t i + − e 10 sin t + e 10 cos t j 10 10 r √ 1 −t 101 − t t kv(t)k = e 5 + e− 5 = e 10 100 10 Z Arc length Arc length √ ∞ 101 10 Z ∞ t kv(t)k dt = e− 10 dt 0 0 √ Z A t 101 e− 10 dt = lim A→∞ 10 0 √ ³ iA t 101 − 10 = lim −10e A→∞ 10 ´0 √ ³ √ A − 10 = 101. = lim 101 1 − e = A→∞ 5.3.7 Extra reading: length of a parabola Find the length of the curve y = x2 from x = 2 to 0. First we parametrise the path. Let x = t then y = t2 for 0 ≤ t ≤ 2. So r(t) = ti + t2 j ⇒ kv(t)k = p 1 + (2t)2 ⇒ ⇒ v(t) = i + 2tj Z 2p Arc Length = 1 + (2t)2 dt. 0 218 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS 5.4 Gradient Fields 5.4.1 Main Points By the end of this section, you should understand the following: • relation between the gradient field and the potential function • conservative fields and gradient fields are equivalent • The potential function will give you an easy method to calculate line integrals for gradient fields (the fundamental theorm for line integrals). Gradient fields are a special type of force field which are conservative, and it gives a relatively easy way to determine if the field is conservative. Stewart covers this in section 17.1 and in section 17.3. 5.4.2 Z Conservative Fields B If F · dr is independent of the path taken, then F is called a conservative field. A Gravity is a conservative field because Z B Z B F .dr = −mg dz = −mg(z(B) − z(A)) A A and it does not matter how you get from A to B. Any constant field must be conservative by the same reasoning. Take the field F = xi + yj, which you can picture as a set of vectors F (x, y) at each point (x, y). y F .dr = (xi + yj).(dxi + dyj) = xdx + ydy x 5.4. GRADIENT FIELDS 219 So Z Z B B F .dr = A A Z B = A xdx + ydy ¶ µ 2 ¶ µ 2 y2 x y2 ± (x2 + y 2 ) ± x + = + − . d B A 2 2 2 2 2 Once again F is a conservative field. But it was chosen in rather a special way. F is a gradient field ie. F = ∇f for some f . µ 2 ¶ x y2 ∂f ∂f F =∇ + , recall that ∇f = i+ j. 2 2 ∂x ∂y Any gradient field is a conservative field and so is path independent. To show this let F = ∇f = chain rules). So Z ∂f ∂f ∂f ∂f i+ j then F .dr = dx + dy = df (recall ∂x ∂y ∂x ∂y Z B B F .dr = A df = f (B) − f (A). A Finding the work done against a conservative field really comes down to finding an f such that F = ∇f . 220 5.4.3 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: Work done by an Electric Field Find the work done by the Electric Field E= α(xi + yj + zk) x2 + y 2 + z 2 along any path from (1, 0, 0) to (1, 1, 1). 5.4. GRADIENT FIELDS 5.4.4 221 Example: Work done by a force Evaluate work done by the force F = ∇f 5.4.5 where f = xy from (0, 0) to (1, 2). Example: Show that F = (y + x)i + xj is a gradient field. 222 5.4.6 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: Not a Gradient Field Show that F = (x + y) y i + j is NOT a gradient field. 2 2 5.5. WORK DONE OR PATH INTEGRALS FOR NONCONSERVATIVE FIELDS 5.5 223 Work Done or Path Integrals for Nonconservative Fields After this section, you should know how to recognise a nonconservative field, and you should be able to perform parametrisations and calculate path integrals. Line integrals for nonconservative fields are harder to evaluate because you must perform the parametrisation of the path. Section 17.2 in Stewart covers some useful theory and examples. Path integrals for forces which are not conservative depend on the path. To work them out we need to parametrise the path by t say, and convert the integral to one over t. Say F (x, y) = F1 (x, y)i + F2 (x, y)j is a non conservative field and x(t) and y(t) provide a parametrisation of the path. Then F .dr = F1 (x, y)dx + F2 (x, y)dy Z Z t=b F .dr = C t=a µ dx dy F1 (x(t), y(t)) + F2 (x(t), y(t)) dt dt ¶ dt 224 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS 5.5.1 Example 1 µ Z Evaluate F .dr where F = C y 1 C2 C1 x 1 x+y 2 ¶ y i + j and the path is as shown. 2 5.5. WORK DONE OR PATH INTEGRALS FOR NONCONSERVATIVE FIELDS 5.5.2 225 Example 2 Z Evaluate F .dr were F = (x + y)i + yj and C is the arc of the ellipse C from (0, 1) to (2, 0). y 1 -2 2 -1 x x2 + y2 = 1 4 226 6. MATLAB GUIDE 6 MATLAB Guide 6.1 6.1.1 Introduction Getting Started • Log in using your library username and password. Ask your tutor if you have a problem. • Open MATLAB just like any other Windows program. Start→Programs→MATLAB→MATLAB R2008b • Save all files to your student directory on H:/ or your own USB flash drive. Files saved onto individual computers or S:/ will be deleted at the end of semester. As MATH1051 is a companion for MATH1052, it is generally assumed that you will still be in the process of developing a basic knowledge of MATLAB. Such basic knowledge includes navigating to the correct directory, executing functions and defining variables. Many of these fundamentals will be re-introduced in this document, but some simple operations, etc. may be used without explanation. If you need further guidance then consult MATLAB Help, the MATH1051 notes or the online guide written by Graeme Chandler located at http://www.maths.uq.edu.au/~gac/mlb/mlb.html. 6.1.2 Getting Help There are a number of resources available should you need assistance when using MATLAB. The most easily accessible is MATLAB’s own built-in Help system. • Clicking the blue question mark button in the toolbar will open the Help window, as will typing doc in the command window. You can search for the topic in question or navigate using the panel on the left. • If you know the name of the function or operation you need help with, typing help <function name> in the command window will display a short description of the function and how to use it. Typing doc <function name> will open the Help window and navigate to the page dedicated to that function or operation. 6.2. THE BASICS 227 If you have trouble finding what you need with MATLAB’s Help system, there is a great deal of documentation and useful articles available online. The Mathworks website (http://www.mathworks.com/products/matlab/) has a lot of helpful descriptions and solutions which go beyond what is available in the MATLAB Help, as well as m-files written and uploaded by users. A Google search often turns up results as well. Of course, your tutors and lecturers will be able to help you out during class or consultation times, and the First Year Learning Centre is open from 2-4pm in 67641 every day. 6.2 The Basics The MATLAB Command Window can be used like an advanced calculator. Mathematical operations like addition, subtraction, multiplication, division and exponentiation are executed just as with a calculator, using +, −, ∗, / and ˆ respectively. MATLAB also has many more complicated functions built in, like sin, cos, tan, log and exp, as well as constants like π and i. Consult the MATH1051 notes or MATLAB Help for more details on these or other functions. >> 1+6*2 ans = 13 >> log(10) ans = 2.3026 >> exp(cos(pi)) ans = 0.3679 Variables are defined using an equals sign, and can then be used just like regular numbers. >> x = 7 x = 7 >> 2^x ans = 128 Vectors and matrices are defined in MATLAB using square brackets. Spaces or commas separate columns and semicolons separate rows. MATLAB has many inbuilt operations for use with matrices, such as addition and multiplication, and more 228 6. MATLAB GUIDE complex functions like eig and rref. Note that matrix indices start at 1, not 0 (so the top-left cell in a matrix is in the (1,1) position). >> x = [1 2; 5 6] x = 1 2 5 6 >> x^2 ans = 11 14 35 46 >> [2 3]*x ans = 17 22 >> x(1,2) ans = 2 Use the command clear <variable name> to remove a variable definition from MATLAB’s memory, and the command clear all to remove all variables at once. Variables should usually be cleared in between MATLAB sessions to avoid using incorrect values. 6.3 M-files M-files are programs written in MATLAB’s own programming language. They are essentially a series of commands which are executed successively when the M-file is run. There are two main types of M-files in MATLAB: • M-file scripts are sequences of commands which could normally be entered into the command window. They are usually intended to be executed a number of times, so time is saved by putting the commands into the M-file. The operations inside a M-file script have access to all the variables, etc. which have been saved in the command window. • M-file functions begin with function [output variables] = function name(input variables) and operate like ordinary functions such as sin or log. They can take input variables, perform operations on them, and produce output variables. The 6.3. M-FILES 229 operations inside an M-file function do not have access to the variables saved in the command window – they can only access the input variables. Anything followed by a % in a M-file is a comment, and will not affect the execution of the file. You should comment all your M-files to help yourself or others understand how the program works. For example, put the following commands into a new M-file, and save it as example script.m. x = [1 2 3 4 5]; y = 2*x’; z = x*y Now run this script by typing the command example script in the command window: >> example script z = 110 Note that only the value of z was ouput, because the definitions of x and y were followed by semicolons (suppressing their output). Put the following into an M-file and save it as example function.m. It will evaluate the polynomial 4x3 + 2x2 − 10x − 7. function output = example function(x) % Evaluates the 3rd order polynomial at x output = 4*x^3 + 2*x^2 - 10*x - 7; Execute the function as you would any regular function in MATLAB: >> example function(4) ans = 241 Note that the name of the function (the word following the = sign at the top of the M-file) must match the name of the M-file itself. 6.3.1 If Statements An if statement in MATLAB is a special type of command which is executed only if a certain condition is satisfied. The general structure of an if statement is always 230 6. MATLAB GUIDE if 1st condition 1st commands elseif 2nd condition 2nd commands else 3rd commands end The conditions above should be boolean statements; i.e. they should take the value 0 or 1. MATLAB has many of these built in; for example, x == y returns 1 if x = y and 0 otherwise, and x <= y returns 1 if x ≤ y and 0 otherwise. If 1st condition takes the value 1, then 1st commands will be executed, and the if statement will be exited. Otherwise, the next condition will be checked, and so on. If none of the conditions take the value 1, then the commands following else will be executed. You can have as many elseif lines as you want (or none at all), and you do not need to have an else line (in which case MATLAB will do nothing if none of the conditions hold). For example, the following function will return the absolute value of a real number, as well as stating whether it is positive, negative or zero. function output = abs val check(x) % Outputs the absolute value of x and states whether x % is positive, negative or zero if x > 0 output = x; display(’positive’) elseif x < 0 output = -x; display(’negative’) else output = 0; display(’zero’) end The function can now be executed in the command window in the usual way. >> abs val check(5) positive ans = 5 6.3. M-FILES 6.3.2 231 For and While Loops Loops allow MATLAB to perform repetitive operations automatically. There are two main types of loops: • For loops instruct MATLAB to perform a fixed number of operations. • While loops instruct MATLAB to repeatedly perform operations for as long as a certain condition holds. The basic structure of a for loop is for index = vector commands end For each value in the vector, the variable index is assigned that value, and commands are executed. For example, the following function will calculate the sum of the first 10 integer powers of x. function output = sum powers(x) % Evaluates the sum of the first 10 powers of x temp = 0; for j = 1:10 temp = temp + x^j; end output = temp; Evaluate the function in the usual way: >> sum powers(2) ans = 2046 While loops look a bit like if statements – their basic structure is while condition commands end On the first iteration of the loop, MATLAB checks whether condition holds. If it does, then commands are executed, and the process is repeated. If on any iteration condition does not hold, then MATLAB exits the loop. 232 6. MATLAB GUIDE For example, the following function performs the same operation as the one above. function output = sum powers2(x) % Evaluates the sum of the first 10 powers of x temp = 0; j = 1; while j <= 10 temp = temp + x^j; j = j + 1; end output = temp; Here we are increasing the value of j by 1 in each iteration, and we finish the loop after j = 10. There is a danger with while loops: if the condition always holds, then the loop will keep iterating forever. When writing while loops, make sure that there will always be some point at which the condition will fail. If you happen to find yourself running an infinite loop, use the command Ctrl+c to terminate it. 6.4 Visualising Functions There are many situations, especially when dealing with multivariate calculus, when a function or a set of data is difficult to visualise. There are a number of ways to plot and view functions and data in MATLAB. 6.4.1 Ezplot The ezplot command allows you to plot functions of one variable. You can control the domain and range if you wish, or let MATLAB assign the default domain of [−2π, 2π]. For example, to plot y = x sin(x) over the domain [−π, π] and range [−10, 10], use the command >> ezplot(’x*sin(x)’,[-pi,pi,-10,10]) Remember to insert the quotation marks ‘ ’ around the function. To plot multiple functions at once, use the hold on command before you start plotting. You can change the domain and range of the figure without re-plotting by using the axis command; for example, >> axis([0,pi,-10,10]) 6.4. VISUALISING FUNCTIONS 233 You can edit the plot or provide a title and axis labels by using the Property Editor in the View menu. The ezplot command can also be used to plot points of the form f (x, y) = 0. For example, to plot the points which satisfy sin(x) + cos(y) = 0 in the domain and range [−2π, 2π], use the command >> ezplot(’sin(x)+cos(y)’,[-2*pi,2*pi,-2*pi,2*pi]) If you wish to open a new figure window to plot functions on a new axis, use the figure command. The ezplot command, despite only being able to plot functions of one variable, can help in visualising functions of two or more variables. This is because it can be used to plot cross sections. For example, to plot cross sections of a function z = f (x, y), use hold on and simultaneously plot the functions z = f (x, −2), z = f (x, −1), . . . , z = f (x, 2). Using different colours, line types and/or labels for each plot may make it easier to visualise the cross sections. Consider also plotting cross sections where x instead of y is held constant (but not on the same axis!). For example, to plot cross sections of the function z = f (x, y) = sin(x) cos(y) for −2π ≤ x ≤ 2π and −2 ≤ y ≤ 2, you could use the commands >> >> >> >> >> >> hold on ezplot(’sin(x)*cos(-2)’) ezplot(’sin(x)*cos(-1)’) ezplot(’sin(x)*cos(0)’) ezplot(’sin(x)*cos(1)’) ezplot(’sin(x)*cos(2)’) 6.4.2 Ezcontour Cross sections may help in visualising a multivariate function, and can often be done easily by hand. However there are many cases where they only produce very confusing or uninformative pictures. Contour diagrams are sometimes a better way of visualising multivariate functions. The ezcontour command is the easiest way to produce contour diagrams for functions. It is used in much the same way as ezplot. The default x and y domains are both [−2π, 2π], but can be controlled in the same way as for ezplot. >> ezcontour(’sin(x)*cos(y)’) >> ezcontour(’sin(x)*cos(y)’,[-10 10 -10 10]) 234 6. MATLAB GUIDE The lines in the contour diagram represent points where z takes the same value. The colours of the lines represent the value of z on that line. Use the command colorbar or go to Insert → Colorbar to insert a colourbar, which will show the values of z corresponding to the colours. The ezcontourf command operates in the same way as ezcontour, but colours in the gaps between the contours. On the MATH1052 webpage is a M-file called MATH1052quiver.m. This script looks for a M-file function called quivfunc.m (which you must define yourself), and produces a contour diagram for the function, as well as an arrow plot. The arrow plot consists of many small arrows which point in the direction of the gradient at their location (ie. up the slope of the function at that point). The quivfunc function must accept inputs x and y. For example, you could define quivfunc as function output = quivfunc(x,y) output = 2*x^2 - y^2; Running MATH1052quiver will then produce a contour diagram and arrow plot: >> MATH1052quiver You can alter the x and y axis ranges in the first few lines of the MATH1052quiver M-file: 6.4. VISUALISING FUNCTIONS xmin xmax ymin ymax 6.4.3 = = = = 235 -1; 1; -1; 1; Ezsurf and Ezmesh In addition to cross sections and contour diagrams, MATLAB can display multivariate functions using surface plots. There are two main MATLAB functions for plotting surfaces: ezsurf and ezmesh. Their use is virtually the same as for ezcontour: the function is specified in exactly the same way, as is the domain(s). The ezsurf command plots a coloured surface, while ezmesh plots a coloured mesh. In both cases the colour at any point represents the value of the function at that point; a colourbar can be inserted to aid in viewing the plot. For example, try using >> ezsurf(’sin(x)*cos(y)’) >> ezmesh(’sin(x)*cos(y),[-10 10 -10 10]) Note that Figure Windows have a button called Rotate 3D. Clicking this button allows you to use the cursor to rotate a plot in three dimensions. Doing so may help you visualise the function more clearly. 6.4.4 Functions of 3 Variables Since functions of three variables require four dimensions in order to be plotted properly, they are often hard to visualise effectively. One helpful method essentially uses the four-dimensional analog of cross sections. To visualise a function w = f (x, y, z), we can make a number of surface plots where z (or x or y) is held constant. x−y For example, consider the function w = f (x, y, z) = x2 +y 2 +z 2 sin(x − z) for −5 ≤ x ≤ 5, −5 ≤ y ≤ 5 and 0 ≤ z ≤ 2. We can hold z constant at a number of values between 0 and 2, and produce surface plots for each resulting two-variable function: f (x, y, 0), f (x, y, 0.5), f (x, y, 1), f (x, y, 1.5) and f (x, y, 2). Obviously we don’t want all these plots to be on the same axis, so use the figure command before each plot to create a new Figure Window. >> >> >> >> figure ezsurf(’(x-y)/(x^2+y^2+0^2)*sin(x-0)’) figure ezsurf(’(x-y)/(x^2+y^2+0.5^2)*sin(x-0.5)’) and so on. 236 6. MATLAB GUIDE 6.5 Visualising Data The commands described in the previous section are useful for functions which can be easily described using a number of variables. However, there will be times when you wish to visualise data which cannot be described using a simple function, or for which such a function is unknown. MATLAB has several commands for plotting and viewing such data. 6.5.1 Plot and Plot3 MATLAB’s plot command is used for plotting two-dimensional data points. To plot the points (x1 , y1 ), . . . , (xn , yn ), use the command plot(x,y) where x = (x1 , . . . , xn ) and y = (y1 , . . . , yn ). For example, to plot the points (1, 2), (4, 2), (3, 1), (7, 4), (2, −3), use >> x = [1,4,3,7,2]; >> y = [2,2,1,4,-3]; >> plot(x,y) The plot command can take an optional third argument, consisting of a string of letters or symbols which tells MATLAB how to plot the data. For example, plot(x,y,’ro’) will plot the points as red circles, and plot(x,y,’g--’) will plot the points connected by a green dotted line. See MATLAB’s help files for the full list of plotting options. Three-dimensional data can also be plotted in MATLAB: simply use plot3 instead of plot, and use vectors x, y and z. The same optional arguments are available. For example, to plot the points (1, 2, 3), (2, 4, 2), (4, 3, −1), (3, 2, 5) as black stars, use the commands >> >> >> >> x = [1,2,4,3]; y = [2,4,3,2]; z = [3,2,-1,5]; plot3(x,y,z,’k*’) 6.5.2 Contour, Surf and Mesh Just as plot is the point-wise analog of ezplot, the other plotting commands described in the previous section have point-wise versions. They are contour, surf and mesh. These work slightly differently to plot3; instead of taking three vectors as arguments, they take two vectors x, y and a matrix Z. These arguments describe 6.6. ANIMATIONS 237 a table of values for the data: x and y respectively give labels for the columns and rows of Z, producing a set of (x, y, z) points. For example, consider the following table: -1 0 1 2 -1 0 1 2 -2 -1 0 1 -1 0 1 2 0 1 2 3 1 2 3 4 Here, x and y are both [−1, 0, 1, 2], and Z is the 4 × 4 matrix inside the table. Using surf(x,y,Z) would instruct MATLAB to create a surface using all the (x, y, z) points, where x and y come from the column and row labels respectively, and z comes from the values inside the table. So the points plotted would be (−1, −1, −2), (0, −1, −1), (1, −1, 0), . . . , (2, 2, 4). If x is of length n and y is of length m, then Z must be a m × n matrix. So to produce a surface plot of the above table, one would use the commands >> >> >> >> x = [-1, 0, 1, 2]; y = x; Z = [-2, -1, 0, 1; -1, 0, 1, 2; 0, 1, 2, 3; 1, 2, 3, 4]; surf(x,y,Z) As with ezsurf, a colourbar can be inserted to assist in viewing the plot. The surf command can in fact be used with only the argument Z; in this case, MATLAB assumes that x and y are the vectors [1, 2, . . . , n] and [1, 2, . . . , m], where Z is a m × n matrix. The mesh and contour commands function in exactly the same way as surf. As one would expect, mesh produces a mesh plot of the data, and contour produces a contour diagram. A coloured-in version of contour, called contourf, also exists. 6.6 Animations The final method of presenting multivariate data is with animations. This essentially involves plotting a sequence of cross sections (or the three-dimensional analog thereof), and using each plot as a single frame. Animations are particularly useful if one of the independent variables is time. Basically the method for producing an animation is: 238 6. MATLAB GUIDE • Write a loop which will vary t (or another independent variable) over some range; • For each value of t, produce a plot; • Use the command M(j) = getframe at each plot to store each image into the ‘vector’ M ; • Use the command movie(M,n) to play the movie n times. For example, the following will produce an animation of the function f (x, t) = cos(x) cos(t) for 0 ≤ x ≤ 6.3 and 0 ≤ t ≤ 20. We cannot use ezplot here because MATLAB will be unable to correctly plot the function (it has two variables); we will instead use plot, with the points close enough together as to make the curve sufficiently smooth. The outermost loop (with index j) will cycle through the t values, and the inner loop (index k) will cycle through x values. % MATLAB program to produce animation for cos(x)cos(t) for j=1:101 t=(j-1)/5; for k=1:64 x(k)=(k-1)/10; z(k)=cos(x(k))*cos(t); end plot(x,z); axis([0,6.3,-2,2]); M(j)=getframe; end movie(M,5) Note that we could not start the j and k loops at 0, because both of those indices were used to index vectors: j was used to index M and k was used to index x and z. MATLAB always starts matrix indices at 1, not 0; so we had to start those indices at 1 and then compensate by setting t = (j − 1)/5 and x(k) = (k − 1)/10. The process is much the same with three-dimensional ‘cross sections’. The only major difference is that there will now be three nested for loops, instead of two: one each to cycle through t, x and y. For example, the following will produce an animation for f (x, y, t) = cos(x) cos(y) cos(t), with 0 ≤ x ≤ 6.3, 0 ≤ y ≤ 6.3 and 0 ≤ t ≤ 20. % MATLAB program to produce animation for cos(x)cos(y)cos(t) for j=1:101 6.7. OPTIMISATION 239 t=(j-1)/5; for k=1:64 x(k)=(k-1)/10; for m=1:64 y(m)=(m-1)/10; Z(m,k)=cos(x(k))*cos(y(m))*cos(t); end end surf(x,y,Z); axis([0,6.3,0,6.3,-1,1]); M(j)=getframe; end movie(M,5) 6.7 6.7.1 Optimisation Using Fminsearch An important objective of much mathematical analysis is to locate global maxima and minima of multivariate functions. MATLAB contains a powerful inbuilt command called fminsearch, which is very effective at finding maxima and minima. The application of fminsearch is as follows: [xval,fval] = fminsearch(’functionname’,x0) where: • functionname is the name of the M-file function you wish to minimise; it must take a single vector as input. • x0 is the initial guess for the location of the minimum; it must be a vector of the same dimensions as the input of functionname. • xval is the the location of the minimum. • fval is the value of the function at the minimum. For example, consider the function f (x, y) = (x − 1)2 + y 2 + 3. We first need to write the M-file function: function output = myfun(input) % input is a vector: we will separate its elements 240 6. MATLAB GUIDE % for simplicity x = input(1); y = input(2); output = (x-1)^2 + y^2 + 3; Then if our initial guess for the (x, y) coordinate of the minimum is (1, 2), we would use the command >> [xval,fval] = fminsearch(’myfun’,[1,2]) xval = 1.0000 0.0000 fval = 3.0000 So the (x, y) coordinate of the minimum is (1, 0) and the value of the function at that point is 3. Note that you may have to try a number of different initial guesses before you find the global minimum. To maximise a function, you need to find the minimum of the negative of the function. 6.7.2 Lines of Best Fit When given a set of multi-dimensional data, we often wish to fit it to some kind of function. This can help us understand the relationships between different properties and make predictions for future data. A line of best fit is the simplest way to do so. While MATLAB has its own built-in functions for doing this (for example, look up polyfit), we will demonstrate here how to use fminsearch to fit a line to two-dimensional data. Suppose we have a collection of two-dimensional points (x1 , y1 ), (x2 , y2 ), . . . , (xn , yn ) and we wish to find a line of best fit of the form f (x) = y = mx + c (where x and y are the independent and dependent variables respectively). The most accurate such line is the one which minimises the errors between the actual data points and the corresponding points on the line; that is, it must minimise the errors f (xi ) − yi = mxi + c − yi . This does not quite make sense: minimising the errors could mean letting them all go to −∞. We actually wish to minimise the absolute value of the errors. This can be done, but it turns out to be easier to minimise the squares of the errors, which are also always positive. This is not quite specific enough – we may have many data points, so how can we effectively minimise the square of each error? In fact, we must minimise the sum of the squares of the errors: 6.7. OPTIMISATION 241 n X (mxi + c − yi )2 i=1 From here, the rest is easy. We need to write a function which takes as input m and c (in a single vector), and outputs the above sum. This function can then be fed into fminsearch, which will find the appropriate values for m and c which minimise this sum of squares. For example, say our experimental data consists of the (x, y) points (20, 3), (17, 5), (21, 2), (19, 4), (16, 5), (23, 2) Then our function may look like the following: function output = best fit(input) % Finds the m and c values for a line of best fit m = input(1); c = input(2); x = [20 17 21 19 16 23]; y = [3 5 2 4 5 2]; temp = 0; for i = 1:length(x) temp = temp + (m*x(i) + c - y(i))^2; end output = temp; and to find the m and c values for the line of best fit, we apply fminsearch in the usual way. >> [coeff,fval] = fminsearch(’best fit’,[0,1]) coeffs = -0.5100 13.3600 fval = 0.83 So the line of best fit for the above example is f (x) = y = −0.51x + 13.36. Plotting the data points and this line on the same axis demonstrates that this is indeed a very good line of best fit. This method works just as well for higher-dimensional examples. For example, a plane of best fit would have the equation f (x, y) = z = mx + qy + c, where x and y 242 6. MATLAB GUIDE are the independent variables and z is the dependent variable. The M-file function would then take as input m, q and c, and output the sum of the squares of the errors: n X (mxi + qyi + c − zi )2 i=1 6.8 6.8.1 Differential Equations Slope Fields A first order differential equation dy/dx = f (x, y) determines the gradient of any solution curve y(x) at any (x, y) point. A slope field is essentially a visual representation of this differential equation: it uses small lines to indicate the gradient at (x, y) points on a cartesian plane. Given an initial condition, you can use a slope field to visually ‘solve’ a differential equation, by following the direction of the field from the given point. The M-files field.m and func.m can be downloaded from the MATH1052 website. The field.m file is a MATLAB function that produces a slope field for dy/dx = f (x, y), and func.m is used to describe the function f (x, y). For example, to produce a slope field for the differential equation dy/dx = y − 2x2 , change func.m to function ydash = func(x,y) ydash = y - 2*x^2; and then run field.m with appropriate input arguments: >> field(-1,1,20) The first argument is the lower bound for the horizontal and vertical axes, the second argument is the upper bound, and the third argument is the number of intervals between the field lines. Note that this function will always produce a plot on a square. You should get the following output. 6.8. DIFFERENTIAL EQUATIONS 243 A possible solution to the differential equation is shown below. Taking the initial condition y(0) = 0 and following the field lines to the left and to the right yields a curve like the one shown. 244 6. MATLAB GUIDE 6.8.2 Euler’s Method Euler’s method is a numerical method for solving differential equations. The algorithm essentially just uses the slope field ‘solution’ described above: • Start at a point (x0 , y0 ), described by an initial condition; • Determine the value of the gradient f (x0 , y0 ) at that point; • Follow in the direction of the gradient for a short distance, to the point (x1 , y1 ); • Determine the gradient f (x1 , y1 ) at this new point; • Follow in the direction of the gradient for a short distance, to the point (x2 , y2 ); and so on. This algorithm is very easy to code in MATLAB. For the differential equation dy/dx = f (x, y) with initial condition y(x0 ) = y0 , you need to define the function f (x, y) (as we did earlier using func.m), and then write a for loop which performs the calculation yi+1 = yi + f (xi , yi )∆x where ∆x is a small (constant) value which will be the distance along the x-axis between successive points. The yi points will need to be saved in a vector, as you will need to store them all for the final solution. The xi points should also be saved in a vector; you can do this before starting the loop with the linspace command, or with the ‘:’ (colon) vector notation. You should produce two sequences of points: the xi points, which will all be a constant distance ∆x apart, and the yi points, which were calculated in each iteration of the for loop. Plotting the y vector against the x vector will illustrate your numerical solution. You should experiment with changing ∆x and/or the number of points used. Euler’s method tends to become inaccurate after several thousand steps; you should remember that this is really only an approximate solution to the differential equation. If possible, find the analytic solution and plot it on the same axis to determine if and where the numerical solution becomes inaccurate. 6.8. DIFFERENTIAL EQUATIONS 6.8.3 245 Runge-Kutta Method Euler’s method is referred to as a first-order approximation for a differential equation, because the approximation essentially uses a first-order Taylor expansion. Higher-order methods can yield far more accurate solutions. One such is the RungeKutta method, which is technically halfway between a fourth and fifth order method. Consequently, the inbuilt MATLAB program for implementing the method is called ODE45. Consider a differential equation problem of the form dy/dt = f (t, y), with f (t0 ) = y0 for t0 ≤ t ≤ T . The MATLAB syntax for solving this problem using ODE45 is ode45(’func’,[t0,T],y0) where func evaluates f (t, y). For example, consider the differential equation described previously (with x changed to t): dy/dt = y − 2t2 , y(0) = 0. We will restrict the time interval to [0, 5]. We can use the same func.m (you can change x to t if you wish). We now only need to execute the ODE45 command: >> ode45(’func’,[0,5],0) 246 6. MATLAB GUIDE Alternatively, we could use the command >> [t out,y out] = ode45(’func’,[0,5],0); In this second case, t out will be a vector consisting of the t coordinates of the points used to calculate and plot the function, and y out will be the y coordinates. The plot will not be automatically produced. Using the command >> plot(t out,y out) will produce similar output to the plot above. 6.8.4 Systems of Differential Equations You have so far seen how to use MATLAB to numerically solve single first-order ODEs. MATLAB’s inbuilt ODE solvers can also solve higher-order differential equations, like d2 y + ey = 0, dt2 y(0) = 1, dy (0) = 0 dt and coupled systems of ODEs, like dx = f (x, y) = = 2x − 3y, dt du = g(x, y) = = x + 2y, dt x(0) = 2 y(0) = 0 We will start with the system of equations above. We can use ODE45 in more or less the same way, with only a couple of differences: • ODE45 stipulates that func must take two input variables: t and another. However, we have three here: t, x and y. We must write func so that it takes inputs (t, u), where u = [x, y]. • The function func will have two outputs: f (x, y) and g(x, y). ODE45 requires that these outputs come in the form of a column vector. We define our new function as follows: function output = system func(t,u) x = u(1); y = u(2); f = 2*x - 3*y; 6.8. DIFFERENTIAL EQUATIONS 247 g = x + 2*y; output = [f;g]; We can now use ODE45 in the same way as before (letting the time interval be [0, 2]): >> [t out,u out] = ode45(’system func’,[0,2],[2,0]); The vector t out has the same meaning as before. The first column of u out corresponds to the x values, and the second column corresponds to the y values. Plotting t out against each of these columns will show how x and y change with time. Plotting the columns of u out against each other will plot x against y (this is called a phase plot). >> plot(t out,u out(:,1)) >> plot(t out,u out(:,2)) >> plot(u out(:,1),u out(:,2)) Higher order ODEs can be solved with ODE45 by first converting them into systems of ODEs. Consider the second-order ODE mentioned previously: y 00 + ey = 0, y(0) = 1, y 0 (0) = 0 We introduce a ‘new’ variable x = y 0 (so x0 = y 00 ). This leads to the system of (first-order) ODEs: dx = f (x, y) = −ey dt dy = g(x, y) = x dt x(0) = 0 y(0) = 1 We can now write a function to evaluate these: function output = secondorder func(t,u) x = u(1); y = u(2); f = -exp(y); g = x; output = [f;g]; The command to execute ODE45 is essentially the same as before (note that our initial value is a vector, because we have two variables): >> [t out,u out] = ode45(’secondorder func’,[0,5],[0,1]); Since x was an extra variable inserted to assist with solving, we only actually care about y, ie. the second column of u out. So to plot y against t, use >> plot(t out,u out(:,2)) 248 6. MATLAB GUIDE 6.9 Parametric Functions One way of presenting a relationship between variables x and y is to write y as a function of x, ie. y = f (x). You have seen that MATLAB is well equipped to plot functions in this form. Not all relationships between variables can be expressed directly using functions, however. For example, the unit circle on the cartesian plane cannot be described with a simple function y = f (x). It can be plotted in MATLAB by plotting multiple functions with hold on: >> >> >> >> figure hold on ezplot(’sqrt(1 - x^2)’,[-1,1]) ezplot(’-sqrt(1 - x^2)’,[-1,1]) However this is needlessly complicated, and may not be possible for more complex relationships. Parametric functions are a much easier way to describe relationships such as these, and MATLAB can plot them just as easily. Parametric functions relate the variables (eg. x and y) through an additional variable, t, called the parameter. For example, the unit circle can be described with the parametric functions x(t) = cos(t) and y(t) = sin(t). In this particular case, t can be interpreted as the angle around the circle going anticlockwise from the x axis. The syntax for plotting a two-dimensional parametric relationship is ezplot(’xfunc’,’yfunc’,[a,b]) where xfunc and yfunc are the parametric representations of x and y respectively, and the parameter t varies over the interval [a, b]. So to plot the unit circle, we can use the command >> ezplot(’cos(t)’,’sin(t)’,[0,2*pi]) The default interval for t is [0, 2π]. MATLAB has a very similar inbuilt function for plotting three-dimensional parametric relationships, called ezplot3. Unsurprisingly, its syntax is ezplot3(’xfunc’,’yfunc’,’zfunc’,[a,b]) For example, to plot a helix (a spring-shaped object) of radius 1 and height 20, you could use the command >> ezplot3(’cos(t)’,’sin(t)’,’t/4’,[0,80]) 6.10. COMMON COMMANDS AND OPERATIONS 249 The ezplot3 command has an additional option for showing the progression of the (x, y, z) coordinate as t changes. Use the syntax ezplot3(’xfunc’,’yfunc’,’zfunc’,[a,b],’animate’) When using ezplot3, remember that you can rotate the figure, as well as change the axis ranges with the axis command. Also consider using the commands axis square and/or axis equal. 6.10 Common commands and operations 6.10.1 Common Numerical Operations & Constants The following table lists the MATLAB commands for a number of commonly used functions which operate on numbers, as well as some constants. Use MATLAB’s help command for more information. Operation x+y x−y xy x/y xn √ x ln(x) log10 (x) x × 10n 6.10.2 MATLAB Code x+y x-y x*y x/y x^n sqrt(x) log(x) log10(x) xen Operation ex |x| sin(x) cos(x) tan(x) arcsin(x) arccos(x) arctan(x) n! MATLAB Code exp(x) abs(x) sin(x) cos(x) tan(x) asin(x) acos(x) atan(x) factorial(n) Constant MATLAB Code π pi √ −1 i e exp(1) ∞ Inf −∞ -Inf Vector & Matrix Operations The following table lists the MATLAB commands for a number of commonly used operations which can be performed on vectors and/or matrices. 250 6. MATLAB GUIDE Operation ¡ ¢ Entering a row vector: a = 1 3 4 4 Entering a column vector: b = 0 7 µ ¶ 2 4 Entering a matrix: A = 0 1 Second component of vector b: Entry (1, 2) of a matrix A: Transpose: c = aT Adding vectors or matrices: b + c Dot product: b · c Cross product: b × c Norm: kak Multiplying matrices: AB Matrix determinant: det(A) Inverse: A−1 Vector dimension: Matrix dimensions: Minimum value in vector b: Maximum value in vector b: 6.10.3 MATLAB Code a=[1,3,4] b=[4;0;7] A=[2,4;0,1] b(2) A(1,2) c=a’ b+c dot(b,c) cross(b,c) norm(a) A*B det(A) inv(A) length(a) size(A) min(b) max(b) Special Matrices MATLAB has a number of special matrices built-in, and these can be produced with certain commands, given in the table below. Matrix MATLAB Code n × n identity matrix eye(n) m × n matrix of ones ones(m,n) m × n matrix of zeros zeros(m,n)
© Copyright 2026 Paperzz