MATH1052 Multivariate Calculus and Ordinary Differential Equations WORKBOOK First Semester, 2013 c School of Mathematics and Physics, The University of Queensland, Brisbane QLD 4072, Aus tralia. 2 How to use this workbook This workbook is designed for use during lectures. It contains copies 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. Take particular note of any highlighted formulae or results such as 1 + 1 = 2. These are formulae or results we expect you to remember for your exams. 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. For further information about the course, including copies of assignments and worksheets, please go to Blackboard at http://blackboard.elearning.uq.edu.au 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 (ODEs); one of the basic tools in mathematical modelling. ODEs are used to describe how things change with time. In engineering and the physical sciences ODEs 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 (7th Edition) (the references in the parentheses corresponds to the 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.1.1 Visualising functions . . . . . . . . . . . . . . . . . . . . . . . 12 1.1.2 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Conic Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.2.1 Parabolas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.2.2 Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.2.3 Ellipses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.2.4 Hyperbolas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.2.5 Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.2.6 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Scalar Equation for a Plane . . . . . . . . . . . . . . . . . . . . . . . 26 1.3.1 Horizontal planes . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.3.2 Vertical planes . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.3 Arbitrary planes . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.4 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Vector Equation of a Plane I . . . . . . . . . . . . . . . . . . . . . . . 32 1.4.1 Revision of vectors . . . . . . . . . . . . . . . . . . . . . . . . 32 1.4.2 Vector equation of a plane I . . . . . . . . . . . . . . . . . . . 36 1.4.3 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Equations for a Line . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 1.5.1 Parallel, skew and orthogonal Lines. . . . . . . . . . . . . . . . 45 1.5.2 Vector equation of a plane II . . . . . . . . . . . . . . . . . . . 48 1.5.3 Distance from a Point to a Plane . . . . . . . . . . . . . . . . 50 1.5.4 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Contour Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 1.6.1 1.7 11 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Cross-sections of a Surface . . . . . . . . . . . . . . . . . . . . . . . . 62 1.7.1 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 CONTENTS 5 2 Partial Derivatives and Tangent Planes 2.1 2.2 2.3 2.4 2.5 67 Limits and Continuity . . . . . . . . . . . . . . . . . . . . . . . . . . 67 2.1.1 Review of the 1-variable case . . . . . . . . . . . . . . . . . . 67 2.1.2 Multivariable limits . . . . . . . . . . . . . . . . . . . . . . . . 69 2.1.3 Multivariable continuity . . . . . . . . . . . . . . . . . . . . . 73 2.1.4 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Partial Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 2.2.1 Slope in the x-direction . . . . . . . . . . . . . . . . . . . . . . 74 2.2.2 Slope in the y-direction . . . . . . . . . . . . . . . . . . . . . . 75 2.2.3 Partial derivatives for f (x, y, z) . . . . . . . . . . . . . . . . . 77 2.2.4 Higher order derivatives . . . . . . . . . . . . . . . . . . . . . 79 2.2.5 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 The Tangent Plane . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 2.3.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 80 2.3.2 Equation for a tangent plane . . . . . . . . . . . . . . . . . . . 81 2.3.3 Smoothness . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 2.3.4 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Linear Approximations . . . . . . . . . . . . . . . . . . . . . . . . . . 87 2.4.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 87 2.4.2 Linear approximations for f (x, y) . . . . . . . . . . . . . . . . 88 2.4.3 Estimating small changes . . . . . . . . . . . . . . . . . . . . . 90 2.4.4 Estimating error . . . . . . . . . . . . . . . . . . . . . . . . . 91 2.4.5 Extra reading: Differentials . . . . . . . . . . . . . . . . . . . 92 2.4.6 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Gradients and Directional Derivatives . . . . . . . . . . . . . . . . . . 93 2.5.1 Directional derivatives . . . . . . . . . . . . . . . . . . . . . . 93 2.5.2 The gradient vector ∇f 2.5.3 Properties of the gradient vector . . . . . . . . . . . . . . . . . 97 . . . . . . . . . . . . . . . . . . . . . 95 6 CONTENTS 2.5.4 2.6 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 The Chain Rule and Applications . . . . . . . . . . . . . . . . . . . . 103 2.6.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 103 2.6.2 The chain rule for f (x, y) 2.6.3 Larger Chain Rules . . . . . . . . . . . . . . . . . . . . . . . . 105 2.6.4 Implicit Differentiation . . . . . . . . . . . . . . . . . . . . . . 107 2.6.5 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 . . . . . . . . . . . . . . . . . . . . 104 3 Max and Min Problems on Surfaces 3.1 3.2 3.3 Quadratic Approximation . . . . . . . . . . . . . . . . . . . . . . . . 110 3.1.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 110 3.1.2 Quadratic approximations of f (x, y) . . . . . . . . . . . . . . . 112 3.1.3 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Critical Points and Optimisation . . . . . . . . . . . . . . . . . . . . 117 3.2.1 Review for f (x) . . . . . . . . . . . . . . . . . . . . . . . . . . 117 3.2.2 Critical points for f (x, y) . . . . . . . . . . . . . . . . . . . . . 119 3.2.3 Global maxima and minima . . . . . . . . . . . . . . . . . . . 127 3.2.4 The Matlab command fminsearch . . . . . . . . . . . . . . . 129 3.2.5 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Constrained Optimisation and Lagrange Multipliers . . . . . . . . . . 130 3.3.1 Lagrange multipliers . . . . . . . . . . . . . . . . . . . . . . . 130 3.3.2 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 4 Ordinary Differential Equations 4.1 110 138 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 4.1.1 Examples of ODEs . . . . . . . . . . . . . . . . . . . . . . . . 138 4.1.2 How do ODEs arise? . . . . . . . . . . . . . . . . . . . . . . . 139 4.1.3 Solution to an ODE 4.1.4 Initial value problems . . . . . . . . . . . . . . . . . . . . . . . 141 . . . . . . . . . . . . . . . . . . . . . . . 140 CONTENTS 7 4.1.5 The order of an ODE . . . . . . . . . . . . . . . . . . . . . . . 141 4.1.6 Riding your bike at constant speed . . . . . . . . . . . . . . . 142 4.1.7 Motion of Projectiles . . . . . . . . . . . . . . . . . . . . . . . 143 4.1.8 Extra reading: Realistic models . . . . . . . . . . . . . . . . . 145 4.1.9 Analytical and numerical solutions . . . . . . . . . . . . . . . 147 4.1.10 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 4.2 4.3 4.4 4.5 Slope Fields and Equilibrium Solutions . . . . . . . . . . . . . . . . . 148 4.2.1 Example: the slope field of y 0 = 2y . . . . . . . . . . . . . . . 149 4.2.2 Equilibrium solutions . . . . . . . . . . . . . . . . . . . . . . . 149 4.2.3 Stability of equilibrium solutions . . . . . . . . . . . . . . . . 153 4.2.4 Existence and uniqueness of solutions . . . . . . . . . . . . . . 155 4.2.5 Long-term behaviour . . . . . . . . . . . . . . . . . . . . . . . 155 4.2.6 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Euler’s Method for Solving ODEs Numerically . . . . . . . . . . . . . 157 4.3.1 Euler’s method using tangent lines . . . . . . . . . . . . . . . 157 4.3.2 Euler’s method using Matlab; an example . . . . . . . . . . . 160 4.3.3 Demonstrating errors in Euler’s method . . . . . . . . . . . . 161 4.3.4 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Separable ODEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 4.4.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 4.4.2 Solving separable ODEs . . . . . . . . . . . . . . . . . . . . . 163 4.4.3 Singular solutions . . . . . . . . . . . . . . . . . . . . . . . . . 166 4.4.4 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Applications: Law of Cooling, Population Growth . . . . . . . . . . . 169 4.5.1 Newton’s law of cooling . . . . . . . . . . . . . . . . . . . . . 169 4.5.2 Malthus: “. . . doomed to misery and vice” . . . . . . . . . . . 172 4.5.3 Verhulst to the rescue; down with the Malthusian law . . . . . 174 4.5.4 Extra reading: numerically solving ODEs . . . . . . . . . . . . 177 8 CONTENTS 4.5.5 4.6 4.7 4.8 4.9 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Solving Linear First Order Equations . . . . . . . . . . . . . . . . . . 178 4.6.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 4.6.2 Worked-out example: RL circuit . . . . . . . . . . . . . . . . . 185 4.6.3 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Linear Second-Order Differential Equations with Constant Coefficients 187 4.7.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 4.7.2 The superposition principle . . . . . . . . . . . . . . . . . . . 188 4.7.3 Reduction of order . . . . . . . . . . . . . . . . . . . . . . . . 189 4.7.4 Homogeneous linear second-order ODEs with constant coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 4.7.5 Extra reading: numerical solutions . . . . . . . . . . . . . . . 199 4.7.6 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Simple Harmonic Motion and Damped Oscillations . . . . . . . . . . 200 4.8.1 The undamped spring . . . . . . . . . . . . . . . . . . . . . . 200 4.8.2 The damped spring . . . . . . . . . . . . . . . . . . . . . . . . 205 4.8.3 Underdamping . . . . . . . . . . . . . . . . . . . . . . . . . . 206 4.8.4 Critical damping . . . . . . . . . . . . . . . . . . . . . . . . . 209 4.8.5 Overdamping . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 4.8.6 Summary of motion of a spring: . . . . . . . . . . . . . . . . . 213 4.8.7 The pendulum 4.8.8 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Inhomogeneous Linear Second-Order ODEs . . . . . . . . . . . . . . . 216 4.9.1 Inhomogeneous linear second-order ODEs with constant coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 4.9.2 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 CONTENTS 9 5 Parametrisation of Curves and Line Integrals 5.1 5.2 5.3 5.4 5.5 224 Parametrisation of Curves . . . . . . . . . . . . . . . . . . . . . . . . 224 5.1.1 Sketching parametric curves . . . . . . . . . . . . . . . . . . . 225 5.1.2 Parametrising circles and ellipses . . . . . . . . . . . . . . . . 228 5.1.3 Parametric curves in R3 . . . . . . . . . . . . . . . . . . . . . 231 5.1.4 Piecewise linear paths . . . . . . . . . . . . . . . . . . . . . . 232 5.1.5 Plotting parametrised equations in Matlab . . . . . . . . . . . 232 5.1.6 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Position Vectors, Velocity and Acceleration . . . . . . . . . . . . . . . 234 5.2.1 Position vector . . . . . . . . . . . . . . . . . . . . . . . . . . 234 5.2.2 Parametrising straight lines . . . . . . . . . . . . . . . . . . . 235 5.2.3 Velocity vector . . . . . . . . . . . . . . . . . . . . . . . . . . 238 5.2.4 Acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 5.2.5 Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 5.2.6 Extra reading: stone on a string . . . . . . . . . . . . . . . . . 241 5.2.7 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Line Integrals and Work Done . . . . . . . . . . . . . . . . . . . . . . 242 5.3.1 Length of a curve . . . . . . . . . . . . . . . . . . . . . . . . . 242 5.3.2 Work done by a constant force . . . . . . . . . . . . . . . . . . 248 5.3.3 Work done over a curve . . . . . . . . . . . . . . . . . . . . . 249 5.3.4 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Gradient Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 5.4.1 Conservative fields . . . . . . . . . . . . . . . . . . . . . . . . 255 5.4.2 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Work Done or Path Integrals for Non-conservative Fields . . . . . . . 263 5.5.1 Main points . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 10 CONTENTS 6 MATLAB Guide 6.1 267 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 6.1.1 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . 267 6.1.2 Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 6.2 The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 6.3 M-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 6.4 6.5 6.3.1 If statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 6.3.2 For and while loops . . . . . . . . . . . . . . . . . . . . . . . 271 Visualising Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 6.4.1 Ezplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 6.4.2 Ezcontour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 6.4.3 Ezsurf and ezmesh . . . . . . . . . . . . . . . . . . . . . . . . 276 6.4.4 Functions of three variables . . . . . . . . . . . . . . . . . . . 276 Visualising Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 6.5.1 Plot and plot3 . . . . . . . . . . . . . . . . . . . . . . . . . . 277 6.5.2 Contour, surf and mesh . . . . . . . . . . . . . . . . . . . . . 277 6.6 Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 6.7 Optimisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 6.8 6.9 6.7.1 Using fminsearch . . . . . . . . . . . . . . . . . . . . . . . . 280 6.7.2 Lines of best fit . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 6.8.1 Slope fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 6.8.2 Euler’s method . . . . . . . . . . . . . . . . . . . . . . . . . . 285 6.8.3 Runge–Kutta method . . . . . . . . . . . . . . . . . . . . . . . 285 6.8.4 Systems of differential equations . . . . . . . . . . . . . . . . . 287 Parametric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 6.10 Common commands and operations . . . . . . . . . . . . . . . . . . . 290 6.10.1 Common numerical operations and constants . . . . . . . . . . 290 6.10.2 Vector and matrix operations . . . . . . . . . . . . . . . . . . 291 6.10.3 Special matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 291 11 1 Functions of Several Variables 1.1 Introduction From MATH1051 or earlier mathematics courses, you should be familiar with functions f of one variable. The tools of calculus were useful because you could: • Sketch the graph y = f (x) of f . • Find the minima and maxima of f . • Analyse the slope of f by calculating f 0 . • Find approximations of f using 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 is covered in Stewart, Chapter 14 (Chapter 15). Many familiar formulae are essentially just functions of more than one variable. For example, the volume V of a box is a function of its width, height and depth: V (w, h, d) = whd, and the profile of a guitar string is a function of time and position along the string: f (x, t) = A sin x cos t. 5 0 /&% -,. () *,+' &$ % % 3 1 −1 −3 0.8 −5 10 0.6 8 0.4 6 !" #! 4 0.2 2 0 0 12 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 `. A first method of visualising this function is to plot V for fixed ` and varying r. 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 as a function of `: 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 13 Better still is visualising the surface by sketching the graph of z = f (x, y). In Matlab, the ezsurf function will prove to be our quickest tool: ezsurf(’pi*r^2*l’,[0,2,0,2]) !"#$&%('*)+-,.0/' 200 C [email protected] ? A <>9;: = 150 100 50 0 4 12(4357628' 2 0 0 0.5 1 1.52 2.5 3 3.5 4 Note how you can change the point of view of the plot with the ‘rotate’ button. In Matlab we can plot contour lines for the volume function using 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 14.1 (Section 15.1). We shall start the course by looking at quadratic functions of two variables. 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. 14 1. FUNCTIONS OF SEVERAL VARIABLES 1.2 Conic Sections The goal of this section is for you to become an expert in graphing parabolas, circles, ellipses and hyperbolas (without use of a graphics calculator, of course). The key-trick to this is “completing the square”. See also Stewart, Section 10.5 (Section 11.5). 1.2.1 Parabolas Example: Sketch the following parabolas: y = x2 , y = x2 + 3, y = (x + 2)2 , y = 2x2 , y = 2(x + 2)2 + 3. 1.2. CONIC SECTIONS 15 Putting a quadratic y = ax2 + bx + c in the form y = a(x − h)2 + k, is called completing the square. Question: What is the interpretation of the point (h, k)? Completing the square makes use of the identity x2 + 2xh + h2 = (x + h)2 . Example: Sketch the parabola y = x2 + 6x + 8. 16 1. FUNCTIONS OF SEVERAL VARIABLES Example: Sketch the parabola y = −3x2 − 6x + 1. Example: Prove the quadratic formula. In other words, given ax2 + bx + c = 0, by completing the square, show that x= −b ± √ b2 − 4ac . 2a 1.2. CONIC SECTIONS 17 Parabolas, circles, ellipses and hyperbolas are all examples of conic sections; they all arise by intersecting a double cone such as z 2 = x2 + y 2 , with a plane. See the diagrams in Stewart p. 694 (p. 690). Slice an upright cone horizontally and you get a circle. Tip your knife a bit and you will get an ellipse. Keep tipping and you will get a parabola. Continue tipping and you will cut out an hyperbola. 18 1.2.2 1. FUNCTIONS OF SEVERAL VARIABLES Circles A circle centered at the origin (0, 0) with radius r is given by the equation x2 + y 2 = r 2 . A circle with radius r and center (h, k) corresponds to (x − h)2 + (y − k)2 = r2 . 1.2. CONIC SECTIONS Example: Sketch the circle x2 + 10x + y 2 − 4y + 20 = 0. 19 20 1.2.3 1. FUNCTIONS OF SEVERAL VARIABLES Ellipses In standard form, an ellipse centered at the origin is given by 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, and you should definitely ask your lecturer to use two pins and a piece of string to demonstrate this. 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). Question: What is the equation of an ellipse centered at the point (h, k)? 1.2. CONIC SECTIONS Example: Sketch the ellipse 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]) 21 22 1.2.4 1. FUNCTIONS OF SEVERAL VARIABLES Hyperbolas A hyperbola centered at the origin, with asymptotes y = ±bx/a, is given by the equation x2 y 2 − 2 =1 a2 b or by x2 y 2 − 2 + 2 = 1. a 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 such a hyperbola, the foci are at (±c, 0) and (0, ±c) respectively, where c2 = a2 + b2 . Question: What is the equation of a hyperbola centered at the point (h, k)? 1.2. CONIC SECTIONS Example: Sketch the hyperbola x2 − 4y 2 = 9. Another kind of hyperbola is the very familiar y = 1/x. 23 24 1.2.5 1. FUNCTIONS OF SEVERAL VARIABLES Quiz Can you match each equation with one of the graphs? (a) x2 − y 2 − 1 = 0 (d) x2 + 2x − y + 1 = 0. (b) x + y 2 − 1 = 0 (e) x2 − y 2 + 4 = 0. (c) x2 − 2x + y 2 = 0 (f) 4x2 − 8x + y 2 = 0. y y 2 1 x 1 x −1 −2 y y 1 −1 1 x 1 x −1 y y 2 1 −1 1 x −2 Double-check each answer by putting each equation in standard form. x 1.2. CONIC SECTIONS 1.2.6 25 Main points • By completing the square, you should be able to (i) sketch any curve of the form ax2 + by 2 + cx + dy + e = 0, (ii) identify this curve as a straight line (or lines), circle, parabola, ellipse or hyperbola, and (iii) point out its main features such as turning points, asymptotes and intercepts with the x- and/or y-axes. • You should also be able to obtain the equation of a curve of the above form from a sketch with features given. 26 1.3 1. FUNCTIONS OF SEVERAL VARIABLES Scalar Equation for a Plane The equation of a plane can be expressed using scalars or vectors. In this section we will sketch planes in R3 and determine their scalar equations. This material is covered in Stewart, Section 12.5 (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 the equation z = c. Example: Sketch the horizontal plane z = 2. 1.3. SCALAR EQUATION FOR A PLANE 1.3.2 27 Vertical planes A vertical plane has the form ax + by = d; it depends on x and y only and z does not appear. If you are not told this is an equation of a plane, or, equivalently, an equation in R3 , then you cannot distinguish it from the equation of a line in R2 . Example: Sketch the plane x + y = 1. First sketch the line x + y = 1 in the xy-plane; the plane x + y = 1 contains this line and is parallel to the z axis. 1.3.3 Arbitrary planes The general equation of a plane in R3 is given by ax + by + cz = d with a, b, c, d fixed real numbers. If the plane is not vertical, i.e., c 6= 0, this equation can be rearranged so that z is expressed as a function of x and y: z = F (x, y) = −(a/c)x − (b/c)y + (d/c) = 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 triangle method: If all of a, b, c 6= 0 the plane ax + by + cz = d intercepts each axis at precisely one point. These three points make up a triangle which fixes the plane. 28 1. FUNCTIONS OF SEVERAL VARIABLES Example: The plane x + 2y + z = 4 intersects the x-axis at x = 4, the y-axis at y = 2 and the z-axis at z = 4. "! 5 0 −5 −10 0 1 2 3 4 5 4 3 2 1 0 The triangle method is based on the simple fact that any three points that lie in a plane uniquely determine this plane provided these three points do not lie on a single straight line. 1.3. SCALAR EQUATION FOR A PLANE 29 Example: Use the triangle method to sketch the plane 2x + 3y − z + 6 = 0. In Matlab, the graphing command is simply: ezsurf(’2*x+3*y+6’,[-2,2,-5,5]) 20 10 0 −10 −2 −1 −2 −1 0 0 1 1 2 2 30 1. FUNCTIONS OF SEVERAL VARIABLES Important remark: It is customary to say the equation of a plane, even though it is not unique. Multiplying the equation of a plane by a nonzero constant gives another equation for the same plane. For example, x − 2y + 3z = 4 and −2x + 4y − 6z = −8 are equations of the same plane. Example: Find the equation of the plane through (0, 0, 5), (1, 3, 2) and (0, 1, 1). 50 0 −50 −5 −2 −1 0 0 1 5 2 The Matlab command to produce a plot of this plane would be: ezsurf(’9*x-4*y+5’,[-2,2,-5,5]) 1.3. SCALAR EQUATION FOR A PLANE 31 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. "!$#&%(' )*+ ,#.-"!$#&/ 50 0 −50 −5 −3 −2 −1 −1 1 ! 0 3 1 5 2 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 horizontal, 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 of a plane through three given points that are not on a straight line. • You should know the scalar equation of a plane. 32 1. FUNCTIONS OF SEVERAL VARIABLES 1.4 Vector Equation of a Plane I For this section see also Stewart, Sections 12.1–12.5 (Sections 13.1–13.5). 1.4.1 Revision of vectors Pythagoras gives the distance d between the points (x0 , y0 ) and (x1 , y1 ) as p d = (x0 − x1 )2 + (y0 − y1 )2 . In three dimensions, the distance d between (x0 , y0 , z0 ) and (x1 , y1 , z1 ) is p d = (x0 − x1 )2 + (y0 − y1 )2 + (z0 − z1 )2 . Example: 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 Recall that 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 . 1.4. VECTOR EQUATION OF A PLANE I 33 It is also true that a · b = kak kbk cos θ, where θ is the angle between a and b, and kak is the norm of a: q √ kak = a · a = a21 + a22 + a23 . If a · b = 0, then a and b are orthogonal. The projection of b onto a is “the component of b in the direction of a”: proja b = kbk cos θ × a a·b = a. kak kak2 34 1. FUNCTIONS OF SEVERAL VARIABLES Recall that the cross product a × b of two vectors in R3 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 is best memorised using a 3 by 3 determinant. The cross product points in the direction given by the right-hand-rule. 1.4. VECTOR EQUATION OF A PLANE I 35 It is also true that ka × bk = kak kbk sin θ. This means that ka × bk gives the area of the parallelogram spanned by the vectors a and b. Example: Compute the area of the triangle with vertices (1, 1), (2, 3), (4, 2). 36 1.4.2 1. FUNCTIONS OF SEVERAL VARIABLES Vector equation of a plane I We have seen in Section 1.3 that any three points in R3 which do not lie on a straight line determine a plane. There is another geometric way to view a plane, which gives rise to the vector equation of a plane. Let P be a plane and n be a vector perpendicular to P . Such a vector is called a normal to P . Let r 0 be a vector from the origin to a point r 0 in P and projn r 0 its projection onto n. Then the length of this projection vector, i.e., kprojn r 0 k is the distance of the plane to the origin. n projn r 0 r0 0 If we had taken another vector, say r, from the origin to a point r in P then projn r = projn r 0 . In other words, the plane P is given by the collection of all points r whose corresponding vectors r have the same orthogonal projection onto n as r 0 . Hence n · (r − r 0 ) = 0. 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 OF A PLANE I 37 We start with the vector equation, where n = (a, b, c) is a normal, r 0 = (x0 , y0 , z0 ) and r = (x, y, z). Then n · (r − r 0 ) = 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 where d = n · r 0 = ax0 + by0 + cz0 . Important remark: Given a plane ax + by + cz = d we now have a geometric interpretation of the vector (a, b, c): it is a normal to the plane. Obviously the normal is not unique since any vector of the form k(a, b, c), where k is a nonzero scalar, is also a normal. Example: Find the vector and scalar equations for the plane passing through the point (2, 4, −1) with normal n = 2i + 3j + 4k = (2, 3, 4). 38 1. FUNCTIONS OF SEVERAL VARIABLES Our understanding of normal vectors allows us to compute angles between planes, see also Stewart Section 12.5 (Section 13.5). Example: Find the angle between the planes x+4y−3z = 1 and −3x+6y+7z = 0. Important remark: Contrary to popular belief, the angle between two planes cannot exceed π/2. It is a cardinal offense to ever hand in work that claims otherwise. 1.4.3 Main points • You should be able to use the dot product, projections and the cross product. • You should be able to derive the scalar equation from the vector equation of a plane. • You should be able to find the equation of a plane given sufficient information. 1.5. EQUATIONS FOR A LINE 1.5 39 Equations for a Line There are three common ways to represent a line: (a) the vector representation; (b) the parametric representation; (c) the symmetric equations, obtained by eliminating parameters in (b). The representation that is best depends on the particular problem at hand. (a) Vector representation Let P0 = (x0 , y0 , z0 ) be a point on the line L with corresponding position vector r 0 and let v be a vector parallel to L, known as a direction vector. For an arbitrary point P = (x, y, z) lying on L we have −→ P0 P = (x − x0 , y − y0 , z − z0 ). P r P0 r0 v 0 −→ −→ Since P0 P , is parallel to v, we have P0 P = λv for some scalar λ. Hence −→ r = r 0 + P0 P = r 0 + λv. The equation r = r 0 + λv, λ∈R is know as the vector equation of the line L. Important remark: As with the equation of a plane, the vector equation of a line is not unique. One can choose any point P0 on the line as “starting point”and one can multiply the vector v by any nonzero constant. 40 1. FUNCTIONS OF SEVERAL VARIABLES Example: Find (a) the vector representation of the line passing through (5, 1, 3) and parallel to the vector (1, 4, −2) and (b) the vector representation of the line passing through (4, −3, 5) and parallel to the vector (−2, −8, 4). What is the relation between the two lines? 1.5. EQUATIONS FOR A LINE 41 (b) Parametric representation The parametric representation of a line is a scalar representation of the vector equation r = r 0 + λv. Writing each vector in component form, r = (x, y, z) r 0 = (x0 , y0 , z0 ) v = (a, b, c) gives (x, y, z) = (x0 , y0 , z0 ) + λ(a, b, c). Matching components results in three scalar equations x = x0 + aλ y = y0 + bλ z = z + cλ (∗) 0 known as the parametric equations of a line. Example: Find the parametric equations for the line passing through (5, 1, 3) and parallel to i + 4j − 2k. 42 1. FUNCTIONS OF SEVERAL VARIABLES (c) Symmetric equations The parameter λ can be eliminated from the parametric equations of a line. For example, by eliminating λ from each of the three equations x = 5 + λ, y = 1 + 4λ, z = 3 − 2λ for the line L, we obtain λ=x−5= y−1 z−3 = . 4 −2 The equations z−3 y−1 = 4 −2 are known as the symmetric equations of L. x−5= What these equation really are is a set of two non-identical, non-parallel planes x−5= y−1 4 and x − 5 = z−3 , −2 which, as we know, must intersect to give a line. The general form of the symmetric equations of the line (∗) from the previous page is given by x − x0 y − y0 z − z0 = = , a b c but some care is needed when one (or two) of a, b or c is equal to zero. For example, if a = 0 the above needs to be replaced by x = x0 , and so on. y − y0 z − z0 = b c 1.5. EQUATIONS FOR A LINE 43 Example: Find the symmetric equations of the line that passes through the points (2, 4, −3) and (3, −1, 1). 44 1. FUNCTIONS OF SEVERAL VARIABLES Example: Find the symmetric equations of the line r = (1, 2, 3) + λ(0, 1, 0). To plot a line in three-dimensional space, use the parametric form. The Matlab command is ezplot3. As input 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. 1.5. EQUATIONS FOR A LINE 1.5.1 45 Parallel, skew and orthogonal Lines. Two lines are parallel if, when written as r = r 0 +λv, and r = s0 +µu, the direction vectors u and v are linear multiples of each other, i.e., u = kv for some nonzero scalar k. Two straight lines in three dimensional space rarely intersect. Non parallel, nonintersecting lines are called skew lines. Example: Show that the lines L1 and L2 given by x = 1 + λ x = 2µ L2 : y = 3 + µ L1 : y = −2 + 3λ z = −3 + 4µ z = 4 − λ are skew lines. 46 1. FUNCTIONS OF SEVERAL VARIABLES Two lines are orthogonal if their direction vectors are orthogonal. An easy test for orthogonality is to take the dot product of the direction vectors; if and only if this gives zero are the lines orthogonal. Example: Show that the lines L1 and L2 given by x−1 z+5 =y−3= , 2 4 5−y = z − 5. L2 : 2 − x = 2 L1 : are orthogonal. Further theory Unlike lines, planes can not be skew in 3 dimensions. Given two planes, the only possibilities are that they intersect, or that they are parallel. If two planes are parallel, then their normal vectors must be parallel. If two (distinct) planes intersect, then they intersect in a line. 1.5. EQUATIONS FOR A LINE 47 Example: Find the parametric equations for the intersection of the planes z =x+y and 2x − 5y − z = 1. 48 1.5.2 1. FUNCTIONS OF SEVERAL VARIABLES Vector equation of a plane II Previously we discussed the scalar equation of a plane, which is an equation of the form ax + by + cz = d, and vector equation a plane, which is an equation of the form n · (r − r 0 ) = 0. There is a second type of vector equation of a plane, which is similar in form to the vector equation of a line. One fixes a point P0 on the plane with corresponding position vector r 0 , but now not one, but two (non-parallel) vectors u and v parallel to the plane are required to fully determine the plane: r = r 0 + λu + µv, λ, µ ∈ R. Example: Sketch a figure explaining the above equation geometrically. Important remark: In some sense the above vector equation of a plane is even less unique than the vector equation of a line. Again one can take r 0 to be any point on the plane and one can multiply both u and v by nonzero constants. But unlike a line, we can also replace u and v by any other pair of (independent) vectors parallel to the plane. For example, (x, y, z) = (1, 0, 1) + λ(0, 1, 2) + µ(3, 1, −1) and (x, y, z) = (4, 1, 0) + λ(−1, 0, 1) + µ(−3, 1, 5) are different vector representations of the same plane! So do not panic if in your exam the person next to you writes down a very different-looking equation; they still might have the right answer. . . 1.5. EQUATIONS FOR A LINE 49 Example: Find the second type of vector equation of the plane through (0, 0, 5), (1, 3, 2) and (0, 1, 1). Example: Find the second type of vector equation for the plane 2x + y − z = 4. 50 1.5.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 point to a plane, where, by distance, we always mean minimum distance. If P = (x1 , y1 , z1 ) is a point, with corresponding position vector p, and Π is a plane (with normal n = (a, b, c)) given by ax + by + cz = d, then the formula for the distance between P and Π is |n · p − d| |ax1 + by1 + cz1 − d| √ D= = . knk a2 + b 2 + c 2 Important remark: It is best not to try to memorise such a formula because, oddly enough, under exam pressure small details often subtly change for the worse. Also, it depends on one’s choice of representation of a plane. Stewart, for example writes ax + by + cz + d = 0 in which case the numerator contains +d instead of −d. Best is to understand the actual derivation given below. One derivation of the above distance formula using orthogonal projections is given in Stewart, Section 12.5 (Section 13.5). Below we present an alternative method, which is not quite as slick, but which is very easy to carry out for explicit examples, even under exam conditions. First we construct the line L through the point P and orthogonal to the plane Π: L: r = p + λn, λ ∈ R. Next we determine the point, say Q, where L intersects Π. That is, we substitute the equation for L into the equation r · n = d for Π: (p + λn) · n = d. Solving for λ yields d−p·n , knk2 so that the point Q, with position vector q, is given by λ= q =p+ d−n·p n. knk2 The distance between P and Π is now the distance between P and Q. But p−q = so that n·p−d n knk2 (n · p − d)2 (n · p − d)2 n · n = . knk4 knk2 The formula now follows by taking the square root on both sides and remembering that a distance can never be negative, whereas n · p − d can. D2 = kp − qk2 = 1.5. EQUATIONS FOR A LINE 51 Example: Find the distance from the point (1, 2, 3) to the plane z = 4. Important remark: In assignments or exams we do not provide boxes whose size might indicate how hard or how trivial an actual question is. Hence it is important to always think first before you start to calculate. Sometimes, just thinking will be enough. Example: Find the distance between the point (1, 2, 3) and the plane x+y +z = 1. 52 1. FUNCTIONS OF SEVERAL VARIABLES 1.5.4 Main points • You should know the various representations for a line. • Given one representation of 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. • You should know how to find the intersection of two planes or a plane and a line. • You should be able to find the distance from a point to a plane, or between two planes. 1.6. CONTOUR DIAGRAMS 1.6 53 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 3-dimensional surface in two dimensions. We now look at functions f of two variables. A contour is a curve corresponding to the equation z = f (x, y) = C, see also Stewart, Section 14.1 (Section 15.1). Consider the 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. 54 1. FUNCTIONS OF SEVERAL VARIABLES !"$#%'&)(* +-,!. & 0/213,4/ 4 3.5 3 % 2.5 2 1.5 1 0.5 0 −2 −1 0 1 , 2 2 1 0 −1 −2 Example: Draw a contour diagram of f given by p f (x, y) = x2 + y 2 . In Matlab, the command is ezcontour(’sqrt(x^2+y^2)’,[-10,10,-10,10]); If horizontal planes are equally spaced, 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. 1.6. CONTOUR DIAGRAMS 55 ! "$#&% '!()#*' 4 3.5 3 2.5 2 1.5 1 0.5 0 −4 −2 0 2 4 # 2 4 −4 −2 0 Note that the contours of the last two functions were all circles. Such surfaces have circular symmetry. When x and y only appear as x2 + y 2 in the definition of f , then the graph of f has circularp symmetry about the z axis. The height z depends only on the radial distance r = x2 + y 2 . Example: z = f (x, y) = e−x 2 −y 2 !#"$&%'"('% )*+-,* * ./0 !#"$&%'"('% 2 1 1.5 0.2 0.3 1 0 0.4 0.6 0.1 −0.5 0.3 0.2 0.9 0.8 0.7 0.4 0.5 0.1 0.5 0.6 4 0. 5 0. 0.1 0.8 −1 0.2 0 2 −1.5 1 2 1 0 0 −1 −1 −2 −2 −2 −2 −1 0 1 2 56 1. FUNCTIONS OF SEVERAL VARIABLES Example: Draw a contour diagram for z = f (x, y) = x2 + 4y 2 − 2x + 1. Plane z=1 z=2 z=3 Contour Description 1.6. CONTOUR DIAGRAMS 57 Example: A saddle z = x2 − y 2 has hyperbolic contours. z=0: z=1: z=2: z = −1 : z = −2 : !" 2 −2 −2 −1 1.5 1 0 −1 1 0.5 2 0 2 0 −0.5 0 −1 1 1 1 −1.5 −1 0 −1 −2 −2 −2 −2 −1.5 −1 −0.5 0 0.5 1 1.5 2 58 1. FUNCTIONS OF SEVERAL VARIABLES Example: Sketch the contour diagram for z = f (x, y) = 9x2 −4y 2 +2 with contours at z = −2, 2, 6 and 10. 1.6. CONTOUR DIAGRAMS 59 Example: Sketch a contour diagram for z = f (x, y) = x2 and use this to sketch the graph of f . 60 1. FUNCTIONS OF SEVERAL VARIABLES Example: Sketch a contour diagram for z = f (x, y) = x − y 2 . Example: Sketch contour diagrams of z = f (x, y) = x and z = g(x, y) = x + y. Note: Contour diagrams of functions whose graphs are planes consist of equidistant parallel lines. (Equidistant lines in R2 are in fact always parallel, unlike R3 . Why?) 1.6. CONTOUR DIAGRAMS 1.6.1 61 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 sketch simple contour diagrams. 62 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 = C, see also Stewart Section 12.6 (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(2πt) where 0 < x < 1. The snapshots where t is constant are cross-sections of the ‘surface’. Varying time we get t=0: 1 t= : 8 1 t= : 4 3 t= : 8 1 t= : 2 These represent sine curves, with z = A sin(πx) 1√ z= 2A sin(πx) 2 z=0 z=− 1√ 2A sin(πx) 2 z = −A sin(πx). amplitudes between 0 and A. We can also consider the cross-sections in x. For instance x = 21 (at the top of the sine wave), then z = A cos(2πt) which equals the amplitude of the sine wave. 1.7. CROSS-SECTIONS OF A SURFACE 63 "!$#&%&'()+*,! % 1 0 −1 1 1 0.8 0.6 # 0.4 0.5 0.2 0 0 Matlab can be used to make a movie of the 2-dimensional surface by plotting crosssections 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. 64 1. FUNCTIONS OF SEVERAL VARIABLES Example: Sketch the cross-sections of the surface V = 13 πr2 h (volume of a cone). Here is the 3-dimensional picture from Matlab. !"$#&%('*),+-/.0(1% A @> = ? :<798 ; 8 6 4 2 2 0 0 1.5 1 0.5 1 2&34656% 0.5 1.5 2 0 1.7. CROSS-SECTIONS OF A SURFACE 65 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, unless you are an equestrian. Here is the Matlab plot of the saddle z = x2 − y 2 and its contours. 4 2 0 −2 −4 2 1 2 1 0 0 −1 −1 −2 −2 66 1. FUNCTIONS OF SEVERAL VARIABLES Example: Use cross-sections to sketch the graph of z = f (x, y) = x2 . 1.7.1 Main points • You should be able to construct cross-sections of multivariate functions. • Cross sections are 2-dimensional graphs. • Animation of cross-sections is another way to visualise multivariate functions. 67 2 Partial Derivatives and Tangent Planes We will need to consider derivatives of functions of more than one variable. To do this, we first check how the familiar concepts of limits and continuity extend to functions of more than one variable. This material is covered in Section 14.2 (Section 15.2) of Stewart. 2.1 2.1.1 Limits and Continuity Review of the 1-variable case Let f : D → R be a function with domain D an open subset of R. For a ∈ D we say that the limit lim f (x) exists if and only if, (i) the limit from the left exists, (ii) x→a the limit from the right exists, and (iii) these two limits coincide, i.e., lim f (x) = lim+ f (x). x→a− x→a Furthermore, if the limit exists and is equal the actual value of f at a, i.e., if lim f (x) = lim+ f (x) = f (a), x→a− x→a we say that f is continuous at x = a. If f is continuous on all of D we say that f is a continuous function on D. 68 2. PARTIAL DERIVATIVES AND TANGENT PLANES We can also consider the limit for points on the boundary of the domain D of a function. For example, if f : (0, 2) → R is defined by f (x) = 1/x, then lim− f (x) = x→2 1 2 but lim f (x) x→0+ does not exist. Another instructive example is f : R\{0} → R given by f (x) = x2 . The domain is now the punctured real line, i.e., D = (−∞, 0) ∪ (0, ∞), but lim f (x) = lim+ f (x) = 0. x→0− x→0 In this situation we also say that limx→0 f (x) exists and in fact one can fix the hole by defining f (0) = 0, to extend f to a continuous function on all of R. Important remark: Never, ever compute limx→a f (x) by blindly substituting x = a in f . For example, if ( x2 for x 6= 3 f (x) = π for x = 3, then limx→3 f (x) exists and is given by 9 which is not equal to π: the function f is not continuous at x = 3. y 9 π x 3 As a second example, if f : D → R with D = R\{1} is given by f (x) = x2 − 1 , x−1 then limx→1 f (x) = 2. Those (and there will be some) who write “This limit gives 0/0 which does not exist” should hang their heads in shame. 2.1. LIMITS AND CONTINUITY 2.1.2 69 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 a given point of interest. Consider the function x2 f (x, y) = 2 x + y2 with domain given by R2 \{(0, 0)}. To see the graph of f in Matlab, type ezsurf(’(x^2/(x^2+y^2)’) ! !#" %" $ &'" 1 0.8 0.6 0.4 0.2 0 1 0.5 0 −0.5 −1 −1 −0.5 0 Next we consider the limit as (x, y) → (0, 0). (i) Approaching the origin along y = 0: 0.5 1 70 2. PARTIAL DERIVATIVES AND TANGENT PLANES (ii) Approaching the origin along x = 0: Does lim f (x, y) exist? (x,y)→(0,0) In general, for the limit lim f (x, y) to exist, it is necessary that every path in (x,y)→(a,b) D approaching (a, b) (the point (a, b) itself may or may not be in D) gives the same limiting value. This gives us the following method for finding if a limit does not exist. If Test for showing no limit exists f (x, y) → L1 as (x, y) → (a, b) along the path C1 ∈ D f (x, y) → L 2 as (x, y) → (a, b) along the path C2 ∈ D such that L1 6= L2 , then the limit lim f (x, y) does not exist. (x,y)→(a,b) Important remark: The above notation is somewhat deficient and perhaps one should write lim f (x, y) (x,y)→D (a,b) to indicate that only paths in D terminating in (a, b) (which itself may or may not be in D) are considered. For example, if f (x, y) = x2 + y 2 with D = {(x, y) : x2 + y 2 < 1} then lim(x,y)→(1,0) f (x, y) exists and is 1. However, if ( x2 + y 2 for D = {(x, y) : x2 + y 2 < 1} f (x, y) = 0 for D = {(x, y) : x2 + y 2 > 1} then lim(x,y)→(1,0) f (x, y) does not exist. 2.1. LIMITS AND CONTINUITY 71 Example: Let D = R2 \{(0, 0)} and f : D → R2 be given by f (x, y) = x2 − y 2 . x2 + y 2 Show that lim(x,y)→(0,0) f (x, y) does not exist. Example: With the same D as above but now f (x, y) = lim(x,y)→(0,0) f (x, y) does not exist. x2 xy , show that + y2 72 2. PARTIAL DERIVATIVES AND TANGENT PLANES Important remark: There are infinitely many paths terminating in a given point, say (a, b), in R2 , raising the question if one can ever prove that lim(x,y)→(a,b) f (x, y) does exist. The good news is that there are methods that can deal with infinitely many paths simultaneously. The bad news is that these methods (typically -δ proofs) are not part of this course. See Stewart Sec 14.2 (Sec 15.2), Example 4 for a rigorous -δ proof that lim f (x, y) = 0, (x,y)→(0,0) where f (x, y) = 3x2 y (x2 + y 2 ) and D = R2 \{(0, 0)}. Example: Give a non-rigorous proof that the above limit is indeed correct by writing x = r cos θ and y = r sin θ. 2.1. LIMITS AND CONTINUITY 2.1.3 73 Multivariable continuity Definition Given a function f : D → R2 , where D is an open subset of R2 . Let (a, b) ∈ D. Then f (x, y) is continuous at (a, b) if lim f (x, y) = f (a, b), (x,y)→(a,b) i.e., the limit (x, y) → (a, b) of f (x, y) exists and is equal to f (a, b). If a function is continuous on all of D we say simply that it is continuous on D. Most of the functions we will consider are continuous. For example, polynomials in x and y are continuous on R2 . As a rule of thumb, if a function with domain D is defined by a single expression it will be continuous on D. x2 − y 2 Example: Returning to the first example on page 71, where f (x, y) = 2 and x + y2 D = R2 \{(0, 0)}, is f (x, y) a continuous function? Example: If we edit the above example by instead defining f on all of R2 by taking f (0, 0) = 0, then is f a continuous function? 2.1.4 Main points • You should be able to show when a limit does not exist. • You should understand continuity of multivariate functions. 74 2.2 2. PARTIAL DERIVATIVES AND TANGENT PLANES Partial Derivatives This material is covered in Stewart, Section 14.3 (Section 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) on the surface. Use the “y-is-constant” cross-section through P to find the slope in the x-direction at P . "!$#%'&)(+*,.-/ 10 (32546-8794 7 0 & −2 −4 −6 −8 −2 −1 2 1 0 - 0 1 −1 2 −2 The slope in the x-direction, with y held fixed, is called the partial derivative of f with respect to x at the point (a, b) ∂f f (a + h, b) − f (a, b) (a, b) = fx (a, b) = lim . h→0 ∂x h 2.2. PARTIAL DERIVATIVES 2.2.2 75 Slope in the y-direction Use the “x-is-constant” cross-section to find the slope at P = (1, −1, −1) in the y direction, i.e., where x = 1. "!#%$&! $ 0 −2 −4 −6 −8 −2 '"(*)+,.-0/./.12/.3+4.52-06 2 −1 1 0 0 1 −1 2 −2 Similarly, the slope in the y-direction, with x held fixed, is called the partial derivative of f with respect to y at the point (a, b) ∂f f (a, b + h) − f (a, b) (a, b) = fy (a, b) = lim . h→0 ∂y h Important remark: Normal rules of differentiation apply, we simply think of the variables being held fixed as constants when doing the differentiation. 76 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example: Find the partial derivatives ∂f ∂f and of f (x, y) = x sin y + y cos x. ∂x ∂y Example: Given f (x, y) = xy 3 + x2 , find fx (1, 2) and fy (1, 2). 2.2. PARTIAL DERIVATIVES 2.2.3 77 Partial derivatives for f (x, y, z) Example: The volume of a box V (x, y, z) = xyz. If x changes by a small amount, say ∆x, denote the corresponding change in V by ∆V . We can easily 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 78 2. PARTIAL DERIVATIVES AND TANGENT PLANES 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 ? 2.2. PARTIAL DERIVATIVES 2.2.4 79 Higher order derivatives The second order partial derivatives of f , if they exist, are written as fxx = ∂ 2f , ∂x2 fyx = ∂ ∂f ∂ 2f = , ∂x∂y ∂x ∂y fyy = ∂ 2f , ∂y 2 fxy = ∂ ∂f ∂ 2f = . ∂y∂x ∂y ∂x If fxy and fyx are both continuous, then fxy = fyx . Example: Returning to the example on page 76 for which f (x, y) = x sin y + y cos x, ∂ 2f ∂ 2f calculate all of the second order partial derivatives of f and show that = . ∂x∂y ∂y∂x 2.2.5 Main points • You should know the definition and meaning of partial derivatives. • You should be able to evaluate partial derivatives of functions. 80 2.3 2. PARTIAL DERIVATIVES AND TANGENT PLANES The Tangent Plane This section is covered in Stewart, Section 14.4 (Section 15.4). 2.3.1 Review for f (x) Recall that if y = f (x) then 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 y = f (x) = x2 at x = 1. 2.3. THE TANGENT PLANE 2.3.2 81 Equation for a tangent plane In general, the equation of the tangent plane to a given surface z = f (x, y) at (a, b, f (a, b)), is z = f (a, b) + fx (a, b)(x − a) + fy (a, b)(y − b), or, equivalently, (x, y, z) = a, b, f (a, b) + λ 1, 0, fx (a, b) + µ 0, 1, fy (a, b) , λ, µ ∈ R. Indeed, the first two components of this vector equation for the tangent plane imply λ = x − a and µ = y − b. Substituting this into the third component gives z = f (a, b) + λfx (a, b) + µfy (a, b) = f (a, b) + fx (a, b)(x − a) + fy (a, b)(y − b). We also note that if we write ∆x = x − a, ∆y = y − b and ∆z = z − f (a, b) then the vector equation for the tangent plane may be rewritten as (∆x, ∆y, ∆z) = λ 1, 0, fx (a, b) + µ 0, 1, fy (a, b) , λ, µ ∈ R. This shows that if ∆x = 1 and ∆y = 0 (i.e., λ = 1 and µ = 0) then ∆z = fx (a, b) and if ∆x = 0 and ∆y = 1 (i.e., λ = 0 and µ = 1) then ∆z = fy (a, b), matching our interpretation of fx and fy as the respective slopes of f in the x or y direction. 82 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example: Find the equation for the tangent plane to the surface z = 1 − x2 − y 2 at the point P = (1, −1, −1). 0 −2 −4 −6 −8 −2 2 −1 1 0 0 1 −1 2 −2 2.3. THE TANGENT PLANE 83 Example: What is the plane tangent to the surface z = f (x, y) = 4 − x2 + 4x − y 2 at (1, 1)? !#" "$%&"(')*+,.- 0&/ 1/ 10 5 0 −5 −10 4 −15 2 −20 −25 4 0 3 2 1 0 −1 −2 −3 −4 −2 84 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example: Find the tangent plane of z = f (x, y) = e−x 2 −y 2 at (x, y) = (1, 3). 2.3. THE TANGENT PLANE 2.3.3 85 Smoothness Can we always find partial derivatives and tangent planes? Example: Simple cusp-like functions are not smooth: !#" " $&%(' )*+,-./-. 3 2.5 2 1.5 1 2 1 2 1 0 0 0 −1 −2 −1 −2 A surface z = f (x, y) is smooth at (a, b) if f , fx and fy are all 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? 86 2. PARTIAL DERIVATIVES AND TANGENT PLANES 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. 10 5 1500 1000 0 500 −5 0 −500 −10 −1000 −15 −1500 100 80 −20 100 60 40 −25 2.3.4 50 20 0 200 400 600 800 1000 0 0 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. 2.4. LINEAR APPROXIMATIONS 2.4 87 Linear Approximations See Stewart, Section 14.4 (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 . 88 2.4.2 2. PARTIAL DERIVATIVES AND TANGENT PLANES Linear approximations for f (x, y) The corresponding linear or first-order approximation for a function f 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 at (a, b) is f (x, y) ' f (a, b) + fx (a, b)(x − a) + fy (a, b)(y − b). Example: The temperature in a region is given by T (x, y) = 100 − x2 − y 2 . Find the linear approximation to T (x, y) near (0, 5). !"$#%&'# ()+* $,* (-(.*0/12( 345,67 100 80 60 40 20 −2 0 2 4 6 8 4 2 0 −2 −4 2.4. LINEAR APPROXIMATIONS 89 2 Example: Find the tangent plane to z = e−x sin y at (1, 2 approximate value for e−(0.9) sin(1.5). π ) and use it to find an 2 !#"%$& ')((*+,(.-/!10 3 24 1 0.5 0 −0.5 −1 0 1 2 3 $ 4 5 2 1 0 −1 −2 90 2.4.3 2. PARTIAL DERIVATIVES AND TANGENT PLANES Estimating small changes We may use the equation for the tangent plane to infer that ∆z ≈ fx (a, b)∆x + fy (a, b)∆y, where ∆x = x − a, ∆y = y − b and ∆z = z − f (a, b) represent a 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. Example: Electric power is given by P (E, R) = E 2 /R where E is the voltage and R is the resistance. Find a linear approximation for P (E, R) if E ' 200 (in Volts) and R ' 400 (in Ohms). Use this to find the effect that a change in E and R has on P . 2.4. LINEAR APPROXIMATIONS 2.4.4 91 Estimating error 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 the linear approximation of f at (a, b) is |E| ≈ |fx (a, b)E1 | + |fy (a, b)E2 |. Example: Suppose when making up a metal barrel of base radius 1 (in metres) and height 2 (in metres), you allow for an error of 5% in radius and height. Estimate the worst-case error in volume. 92 2. PARTIAL DERIVATIVES AND TANGENT PLANES 2.4.5 Extra reading: Differentials If we reduce our small changes ∆x and ∆y down to infinitesimal changes dx and dy, we may rewrite the linear approximation (tangent plane) as dz = df = fx (a, b) dx + fy (a, b) dy. This infinitesimal change dz is called the total differential. Note that this equation is not just an approximation. Example: Work done by a gas Let the work done by an expanding gas be given by W = P V , where P denotes pressure and V volume. An expression for the total differential dW is then dW = ∂W ∂W dP + dV ∂P ∂V = V dP + P dV. 2.4.6 Main points • You should be able to calculate finite small deviations of a 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. 2.5. GRADIENTS AND DIRECTIONAL DERIVATIVES 2.5 93 Gradients and Directional Derivatives This material is covered in Stewart, Section 14.6 (Section 15.6). 2.5.1 Directional derivatives The partial derivative fx (or fy ) corresponds to the slope of f (x, y) in the x-direction (or y-direction). We now turn our attention to the question of slopes in arbitrary directions, such as i + 2j or −j. Let u = (u1 , u2 ) be an arbitrary unit vector in R2 , i.e., kuk = 1. We have already seen that the equation of the tangent plane at (a, b) of a function f may be written as (∆x, ∆y, ∆z) = λ 1, 0, fx (a, b) + µ 0, 1, fy (a, b) , λ, µ ∈ R, where ∆x = x − a, ∆y = y − a and ∆z = z − f (a, b). This allows us to easily describe the slope of f at the point (a, b) in the direction of u: we must “measure” ∆z when ∆x = u1 and ∆y = u2 . Hence we take λ = u1 and µ = u2 in order to find that ∆z = u1 fx (a, b) + u2 fy (a, b) = fx (a, b), fy (a, b) · u. This is usually denoted as fu (a, b) and known as the slope of f at the point (a, b) in the direction of u, or the directional derivative of f at (a, b) in the direction of u. Because it is not always convenient to work with unit vectors, we more generally have that the directional derivative of f at (a, b) in the direction of an arbitrary nonzero vector u is given by u fu (a, b) = fx (a, b), fy (a, b) · . kuk 94 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example: Find the directional derivative of f (x, y) = 4 − x2 − 4y 2 at (a, b) in the (1, 1) direction. Example: If f (x, y) = x2 − 3y 2 + 6y, find the slope at (1, 0) in the direction i − 4j. 2.5. GRADIENTS AND DIRECTIONAL DERIVATIVES 2.5.2 95 The gradient vector ∇f The gradient vector or simply gradient of f is a vector with the partial derivatives as components grad f = ∇f = (fx , fy ) = fx i + fy j. Example: Find the gradient of f (x, y) = x2 − 3(y − 1)2 + 3. Similarly, for a function f (x, y, z, w) of four variables, grad f = ∇f = (fx , fy , fz , fw ). Note that directional derivative can be conveniently expressed in terms of the gradient as u . fu = ∇f · kuk Example: Find f(1,−1) (0, 1) for f (x, y) = x − x2 y 2 + y. 96 2. PARTIAL DERIVATIVES AND TANGENT PLANES 2 Example: Find the directional derivative of g(x, y) = ex cos y at (1, π) in the direction −3i + 4j. 2.5. GRADIENTS AND DIRECTIONAL DERIVATIVES 2.5.3 97 Properties of the gradient vector Example: Consider the contour diagram of a plane z = f (x, y) = mx + ny + c. For n 6= 0 the contours have slope −m/n in the xy-plane. The gradient vector, mi + nj (m 6= 0), has slope n/m and so is perpendicular to the contours. It points in the direction of increasing f . In fact, the direction in which it points is the direction of greatest slope. 98 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example: Consider f (x, y) = x2 + y 2 . The contours are circles centered at the origin, and ∇f = 2xi + 2yj 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 gradient of a function are: The gradient ∇f (a, b) is perpendicular to the contour line through (a, b) and points in the direction of increasing f . In fact, the direction and magnitude of steepest slope at (a, b) are given by ∇f (a, b) and k∇f (a, b)k. We can understand these two facts by considering the value of cos θ in u = k∇f k cos θ. fu = ∇f · kuk 99 100 2. PARTIAL DERIVATIVES AND TANGENT PLANES Example: T (x, y) = 20 − 4x2 − y 2 describes the temperature on the surface of a metal plate. In which direction away from the point (2, −3) does the temperature increase most rapidly? In which directions away from the point (2, −3) is the temperature not changing? 2.5. GRADIENTS AND DIRECTIONAL DERIVATIVES 101 Example: A team from a large British oil company is mapping the ocean floor to assist in the plugging of a leaking oil well in the Gulf of Mexico. Using sonar, they develop the model πy D(x, y) = 1700 − 30x2 − 50 sin , 2 where x and y are distance in kilometres, D is depth in metres, and −2 ≤ x ≤ 2 and −2 ≤ y ≤ 2. (a) The well is located at (1, 0.5). What is its depth? (b) Determine the slope of the ocean floor in the positive x-direction and in the positive y-direction in the area considered by the clean-up team. (c) Determine the magnitude and direction of greatest rate of change of depth at the position of the well. 102 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 maximal increase of a function. • You should understand directional derivative and gradient. 2.6. THE CHAIN RULE AND APPLICATIONS 2.6 103 The Chain Rule and Applications The chain rule is covered in Stewart, Section 14.5 (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 = or f 0 (x) = f 0 (u) g 0 (x). dx du dx Example: Find y 0 (x) where y = f (x) = (x2 + 1)5 . Example: Suppose the radius of a cylinder decreases at a rate of r0 (t) = −2 cm/s. How fast is the volume decreasing when r = 1 cm and h = 2 cm? 104 2.6.2 2. PARTIAL DERIVATIVES AND TANGENT PLANES The chain rule for f (x, y) Given f (x, y) with x and y functions of t. Then df ∆f f (x(t + ∆t), y(t + ∆t)) − f (x(t), y(t)) = lim = lim . ∆t→0 ∆t ∆t→0 dt ∆t Now if f is smooth and ∆f is small, we can relate it to ∆x and ∆y through the linear approximation ∆f ' fx ∆x + fy ∆y. Hence ∆f ∆x ∆y ' fx + fy . ∆t ∆t ∆t Now we let ∆t → 0, and provided x(t) and y(t) are smooth, dx dy df = fx + fy dt dt dt which is the chain rule for f (x(t), y(t)). Example: Continuing the previous example, suppose that not just the radius but dh also the height h is decreasing: = −1 cm/s. What is the rate of change in dt volume? The chain rule can be extended to any number of dimensions. Example: If V (a(t)), b(t), c(t)) = abc is the volume of a box then find dV . dt 2.6. THE CHAIN RULE AND APPLICATIONS 2.6.3 105 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 ∂x z ∂z ∂y y x dx dt dy dt t t Hence dz ∂z dx ∂z dy = + . dt ∂x dt ∂y dt If z = f u(x(t), y(t)), v(x(t), y(t)) then the tree diagram is z ∂z ∂u ∂z ∂v u ∂u ∂x x v ∂u ∂y ∂v ∂x y dx dt x dy dt t t ∂v ∂y y dx dt dy dt t Hence dz ∂z ∂u dx ∂u dy ∂z ∂v dx ∂v dy = + + + dt ∂u ∂x dt ∂y dt ∂v ∂x dt ∂y dt ∂z ∂u ∂z ∂v dx ∂z ∂u ∂z ∂v dy = + + + . ∂u ∂x ∂v ∂x dt ∂u ∂y ∂v ∂y dt t 106 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 Example: Given h = f (x, y, z) and each of x, y and z depend on both s and t, ∂h find an expression for . ∂t 2.6. THE CHAIN RULE AND APPLICATIONS 2.6.4 107 Implicit Differentiation Both y + sin(x + y) = 0 and x11 − y 7 + xy 2 + 1 = 0 correspond to curves in R2 . In neither of these examples can we explicitly solve for y as a function of x (or vice versa). This raises the following question: Can we compute y 0 (x)? The answer to this question is affirmative and the method to do this goes by the name of implicit differentiation. First we will try to find y 0 (x) by “brute force”. It is only advisable to use this method if you have a PhD in applications of the chain rule. Example: Find y 0 (x) on the curve y + sin(x + y) = 0. Important remark: Although we have found y 0 (x) it is almost never possible to express the answer in terms of x only (as this requires y to be solved for x). Extreme care has thus to be taken in using the above formula for y 0 (x). It only has meaning for points (x, y) that lie on the curve y + sin(x + y) = 0, such as (0, 0) or (−1 − π/2, 1). 108 2. PARTIAL DERIVATIVES AND TANGENT PLANES So what if you do not have PhD in applications of the chain rule? The idea is to view a given curve C in R2 (such as y + sin(x + y) = 0) as the contour z = 0 of a function F (x, y). That is, we define F (x, y) = y + sin(x + y). We already know that, for any given point (x, y) on C, ∇F (x, y) corresponds to a vector perpendicular to C at that point. (1, y 0 ) ∇F In other words, ∇F (x, y) · (1, y 0 (x)) = 0. Solving this equation for y 0 (x) gives dy Fx =− . dx Fy Example: Find y 0 (x) on the curve y + sin(x + y) = 0. 2.6. THE CHAIN RULE AND APPLICATIONS 109 Example: The Folium of Descartes is given by x3 + y 3 = 6xy. Find y 0 (x). Question: What happens at x = y = 0? 2.6.5 Main points • You should be able to differentiate a multivariable function whose variables depend on multivariable functions. • You should be able to find dy from implicit forms. dx 110 3 3. MAX AND MIN PROBLEMS ON SURFACES Max and Min Problems on Surfaces The content of this chapter is found in Stewart, Sections 14.7 and 14.8 (Sections 15.7 and 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) We saw in Section 2.4.1 that the value of f (x) near x = a can be approximated using a straight line: f (x) ' f (a) + f 0 (a)(x − a). Adding a second-order term will improve this approximation f (x) ' f (a) + f 0 (a)(x − a) + f 00 (a) (x − a)2 . 2 This quadratic (or second-order) approximation corresponds to the approximation of f by the parabola through (a, f (a)) with slope and second derivative at a matching those of f . Unless f is very wild around x = a this is generally quite a good approximation for x − a not too large. 3.1. QUADRATIC APPROXIMATION 111 Example: Compute e0.1 using a linear and a quadratic approximation. Example: In the same graph, sketch f (x) = cos x as well as its linear and quadratic approximations around 0. 112 3. MAX AND MIN PROBLEMS ON SURFACES 3.1.2 Quadratic approximations of f (x, y) This material is covered in Stewart p. 980 (p. 969). The quadratic or second-order approximation to f (x, y) around (a, b) is a function of the form Q(x, y) = h + mx + ny + ax2 + bxy + cy 2 such that Q(a, b) = f (a, b) and such that all first order and second order partial derivatives of f and Q agree at (a, b). It is not hard to verify that this gives Q(x, y) =f (a, b) + fx (a, b)(x − a) + fy (a, b)(y − b) 1 1 + fxx (a, b)(x − a)2 + fxy (a, b)(x − a)(y − b) + fyy (a, b)(y − b)2 . 2 2 Example: Check that Qxx (a, b) = fxx (a, b) and Qxy (a, b) = fxy (a, b). 3.1. QUADRATIC APPROXIMATION 113 Example: Find the quadratic approximation around (0, 0) of f (x, y) = 1 − x2 + y 2 + xy + x3 + x2 y 2 . 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 immediately read off the quadratic approximation. In the next example we really need to use the formula (or do we?). 114 3. MAX AND MIN PROBLEMS ON SURFACES Example: Find the linear and quadratic approximations around (0, 0) of f (x, y) = e−x 2 −y 2 . 3.1. QUADRATIC APPROXIMATION 115 Example: Consider, again, a barrel with base of 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 (1, 2) = 2π ∂r∂h ∂ 2V = 2πr ∂r∂h ∂ 2V = 0. ∂h2 Hence the quadratic approximation around (1, 2) is V (r, h) ' Q(r, h) = 2π + 4π(r − 1) + π(h − 2) + 2π(r − 1)2 + 2π(r − 1)(h − 2). Let ∆r = r − 1 and ∆h = h − 2. Use Q(r, h) to approximate the change ∆V in the volume of the barrel when ∆r = 0.05 and ∆h = 0.1. 116 3. MAX AND MIN PROBLEMS ON SURFACES 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 the linear and quadratic approximations of a function, and make a comparison of the errors. • The quadratic approximation is superior to the linear approximation. 3.2. CRITICAL POINTS AND OPTIMISATION 3.2 3.2.1 117 Critical Points and Optimisation Review for f (x) Smooth functions f of one variable 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 f (x) = x2 − x4 . 118 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 f (±2) = 4 − 16 = −12. 1 1 1 The global maximum, however, is at the local maximum, i.e. f √ = − = 2 4 2 at 1 . 4 The global maximum or minimum of a continuous function f (x) occurs either at a local minimum, a local maximum, or on the boundary of its domain. In some circumstances there may be local maxima or minima at points where the df is not defined (at cusps, for instance). derivative dx Example: f (x) = |x − 3|. A more general definition of a local minimum or maximum is: • A local minimum occurs at a point a where f (x) ≥ f (a) for all x sufficiently close to a. • A local maximum occurs at a point a where f (x) ≤ f (a) for all x sufficiently close to a. If f is continuous, a local maximum or minimum occurs either when f 0 (x) = 0 or when f 0 (x) is undefined. 3.2. CRITICAL POINTS AND OPTIMISATION 3.2.2 119 Critical points for f (x, y) In this section we will find and classify critical points of a function of two variables, see Stewart, Section 14.7 (Section 15.7). (There are also some useful Matlab functions which perform minimisation, such as 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) sufficiently close to (a, b) • f (x, y) has a local minimum at (a, b) if f (a, b) ≤ f (x, y) for all (x, y) sufficiently close to (a, b). If f is differentiable, a local maximum or minimum can only occur in points where the corresponding tangent plane is horizontal, i.e., where ∇f = 0. Note that if either one of ∂f ∂f or is undefined then ∇f is undefined. ∂x ∂y Definition: A critical point of f (x, y) is a point where either ∇f = (0, 0) or where ∇f is undefined. Local maxima and minima occur at critical points, but not all critical points are local maxima or minima. Remark: (For the mathematically inclined.) The rather vague description “sufficiently close” can be made much more precise. In the simplest case, when f : R2 → R, it goes as follows. The function f has a local maximum at (a, b) if there exists an > 0 such that f (a, b) ≥ f (x, y) for all (x, y) such that k(x, y) − (a, b)k < . 120 3. MAX AND MIN PROBLEMS ON SURFACES Example: Find the critical points of f (x, y) = x2 + 3x + y 2 . Example: Find the critical points of 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 ). Important remark: Not all saddle points look like a “horse saddle”, and in one of your Matlab sessions you will be asked to look at a so-called monkey saddle, described by an equation of the form z = x3 − 3xy 2 . 3.2. CRITICAL POINTS AND OPTIMISATION 121 Example: Find all the critical points of f (x, y) = x2 + y 2 (1 − x)3 and use a quadratic approximation to investigate the nature of the critical points. 122 3. MAX AND MIN PROBLEMS ON SURFACES "!$# &%('*) +$,-'.+ #/10 )32 0.2 0.15 0.1 0.05 0 −0.2 −0.2 −0.1 −0.1 0 0 0.1 ' 0.1 0.2 0.2 !" #%$ &(' 150 100 50 0 −50 −100 −2 −2 −1 0 0 1 2 2 4 3.2. CRITICAL POINTS AND OPTIMISATION 123 The plus or minus signs in front of x2 and y 2 in the quadratic approximation can indicate whether f has a maximum, minimum or saddle at a given critical point. However, when Q(x, y) has cross-terms such as xy one needs to be extremely careful. Example: Examine the critical point (0, 0) of f (x, y) = x2 + xy + y 2 . Next consider y2 1 2 1 = x + y − y2. 8 2 8 This function has a saddle point at (0, 0). f (x, y) = x2 + xy + Question: Can we find a general rule to determine the nature of critical points? 124 3. MAX AND MIN PROBLEMS ON SURFACES Let Q be the quadratic function Q(x, y) = ax2 + bxy + cy 2 + dx + ey + f by 2 b2 y 2 cy 2 =a x+ + dx + ey + f − 2 + 2a 4a a by 2 (4ac − b2 ) 2 =a x+ + y + dx + ey + f. 2a 4a2 Then • a > 0 and 4ac − b2 > 0 implies that f has a local minimum; • a < 0 and 4ac − b2 > 0 implies that f has a local maximum; • 4ac − b2 < 0 implies that f has a saddle. Applying this argument to our quadratic approximation formula for f around (a, b): Q(x, y) = f (a, b) + fx (a, b)(x − a) + fy (a, b)(y − b) 1 1 + fxx (a, b)(x − a)2 + fxy (a, b)(x − a)(y − b) + fyy (a, b)(y − b)2 2 2 gives what is known as the second-derivative test. Theorem: Assume that f and its first- and second-order partial derivatives are all continuous at (a, b) and ∇f (a, b) = (0, 0). Define the 2 × 2 determinant fxx (a, b) fxy (a, b) . D = fyx (a, b) fyy (a, b) Then: • fxx (a, b) > 0 and D > 0 implies that (a, b) is a local minimum; • fxx (a, b) < 0 and D > 0 implies that (a, b) is a local maximum; • D < 0 implies that (a, b) is a saddle point; • if D = 0 the second-derivative test is inconclusive. Remark: (For the connoisseurs.) The matrix fxx fxy fyx fyy is known as the Hessian and its determinant as the discriminant. 3.2. CRITICAL POINTS AND OPTIMISATION 125 The beauty of this test is that we do not even have to write down the quadratic approximation. Example: Find and classify the critical points of f (x, y) = e−x 2 +y 2 . 126 3. MAX AND MIN PROBLEMS ON SURFACES Example: Find and classify the critical points of f (x, y) = x2 + y 2 − 2x2 y 2 . 3.2. CRITICAL POINTS AND OPTIMISATION 3.2.3 127 Global maxima and minima To find the global maximum or minimum values of f on a closed and bounded domain D, we need to compare both • the values of f at the critical points (where ∇f = (0, 0) or undefined), and • the extreme values of f on the boundary of D. Example: What are the global maximum and minimum values of the function f (x, y) = x2 + y 2 − 2x2 y 2 on the domain [−1, 1] × [−1, 1]? 128 3. MAX AND MIN PROBLEMS ON SURFACES !"#%$" " &'( 0 0.4.3 0.9 0.7 0.8 0.5 0.6 0 0.6 0.7 0.8 0.9 0.4 0.9 0.1 0.5 0.3 −0.5 0.5 0.5 0 −0.5 0 0.5 1 −1 −1 −1 −0.5 0 0.9 .8 0 0.6 1 0.7 0.4 0.3 0.2 0 −1 0.2 0.4 0.8 0.4 0.5 0.30.4 0.5 0.3 0.2 0.6 0.7 0.5 0.8 0.3 0.4 1 1 ) 0.6 1 3.2. CRITICAL POINTS AND OPTIMISATION 3.2.4 129 The Matlab command 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 non-smooth functions as well, so it extends beyond traditional calculus methods. The syntax is that fminsearch will try to minimise a function f that 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 (in this example the threedimensional 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. 130 3.3 3.3.1 3. MAX AND MIN PROBLEMS ON SURFACES Constrained Optimisation and Lagrange Multipliers Lagrange multipliers In practical problems one often needs to maximise or minimise a function subject to certain constraints, see also Stewart, Section 14.8 (Section 15.8). Example: Find the minimum value of x2 + y 2 subject to the constraint x + y = 1. We want to minimise the function f given by f (x, y) = x2 + y 2 , subject to the constraint x + y = 1. In this baby-example we can explicitly solve the constraint-equation: y = 1 − x, so F (x) := f (x, 1 − x) = x2 + (1 − x)2 = 2x2 − 2x + 1, which is a function of x alone. The critical points of F occur when F 0 (x) = −2+4x = 0, yielding x = 1/2. Since F 00 (x) = 4 > 0 it follows that F has a minimum at x = 1/2. Therefore, f subject to the constraint x + y = 1 achieves its minimum value at x = y = 1/2, with actual value also 1/2. There is a much better way to solve the above problem not requiring the explicit solution of the constraint-equation. The trick is to define a second function, say g, such that the constraint-equation corresponds to g(x, y) = 0. In our example, g(x, y) = x + y − 1. Now graph the contour plot of f (x, y) = x2 + y 2 and add to this graph the single contour g(x, y) = 0: y the circles are the contours f = 21 , 1, 32 , 2, 25 x g=0 3.3. CONSTRAINED OPTIMISATION AND LAGRANGE MULTIPLIERS 131 The minimum occurs where the contour g(x, y) = 0 touches one of the contours of f , which means that ∇f and ∇g are parallel. Hence ∇f = λ∇g for some λ, known as the Lagrange multiplier. Example: Find the minimum value of x2 + y 2 subject to the constraint x + y = 1. 132 3. MAX AND MIN PROBLEMS ON SURFACES Important remark: When solving a constrained optimisation problem with Lagrange multipliers, it is typically not necessary to find the value for λ. So, when solving your equations obtained from ∇f = λ∇g and the constraint-equation, your aim is really to eliminate λ in order to solve for the variables in your problem. Important remark: If upon using the method of Lagrange multipliers you obtain simple, linear equations to solve then consider yourself lucky. In some cases you will in fact obtain nonlinear equations that you need to solve. If this happens we suggest the first thing you should try is taking the ratio of your equations obtained from ∇f = λ∇g. This allows you to immediately eliminate λ from your equations and proceed from there. The next example nicely illustrates this point. Example: Let A = 4xy describe the area of a rectangle centred on the origin with width 2x and height 2y. Maximise the area of this rectangle subject to the x2 y 2 constraint that it is inscribed within the ellipse 2 + 2 = 1. a b 3.3. CONSTRAINED OPTIMISATION AND LAGRANGE MULTIPLIERS 133 134 3. MAX AND MIN PROBLEMS ON SURFACES The method of Lagrange multipliers can be extended to any number of dimensions. Example: Let V (x, y, z) = xyz describe the volume of a box with vertices at (0, 0, 0), (x, 0, 0), (0, y, 0), . . . , (x, y, z), with x, y, z > 0. Maximise the volume of the box subject to the constraint that the vertex (x, y, z) lies in the plane z +2x+3y = 6. 3.3. CONSTRAINED OPTIMISATION AND LAGRANGE MULTIPLIERS 135 Example: The distance between a point and a plane. We now return to a problem considered Chapter 1 — that of finding the distance between a point and a plane. Recall that when we say distance, we always mean minimum distance. Let P = (x1 , y1 , z1 ) be a point and Π : ax + by + cz = d a plane. We want to compute the distance between P and Π. If we take an arbitrary point Q = (x, y, z) in R3 then the square of the distance between P and Q is given by D2 = (x − x1 )2 + (y − y1 )2 + (z − z1 )2 =: f (x, y, z). We want to minimise this distance subject to the constraint that Q lies in the plane Π, i.e., subject to the constraint ax + by + cz = d. Following the method of Lagrange multipliers we thus define g(x, y, z) = ax + by + cz − d and compute ∇f = λ∇g to get 2(x − x1 ) = λa 2(y − y1 ) = λb 2(z − z ) = λc. (∗) 1 First we use (∗) to find D2 = f (x, y, z) = (λa/2)2 + (λb/2)2 + (λc/2)2 = λ 2 2 (a2 + b2 + c2 ) from which we still need to eliminate λ. Second we use (∗) to find x = λa/2 + x1 , y = λb/2 + y1 , z = λc/2 + z1 . Substituting this into the constraint equation ax + by + cz − d = 0 we find that λ d − ax1 − by1 + cz1 = . 2 a2 + b 2 + c 2 Therefore, 2 D = λ 2 2 (d − ax1 − by1 + cz1 )2 (a + b + c ) = a2 + b 2 + c 2 2 2 2 as before. If needed, we can also obtain the point on the plane that is closest to (x1 , y1 , z1 ). 136 3. MAX AND MIN PROBLEMS ON SURFACES Example: Use the method of Lagrange multipliers to find the distance between the point (1, 2, 3) and the plane x + y + z = 1. 3.3. CONSTRAINED OPTIMISATION AND LAGRANGE MULTIPLIERS 3.3.2 137 Main points • You should be able to apply the method of Lagrange multipliers to solve problems in constrained optimisation. • You should understand the method of Lagrange multipliers geometrically in terms of gradient vectors. 138 4. ORDINARY DIFFERENTIAL EQUATIONS 4 Ordinary Differential Equations 4.1 Introduction This material is covered in Stewart, Section 9.1 (Section 10.1). In almost all fields of scientific endeavour, quantifying time-dependent occurrences almost inevitably comes down to a differential equation (DE). A 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 will not consider PDEs in this course. 4.1.1 Examples of ODEs • Unbounded population growth: P 0 (t) = kP . • Bounded population growth: P 0 (t) = kP (1 − P/θ). • Motion due to gravity: my 00 (t) = −mg. • Spring system: mx00 (t) = −kx. In ODEs, one often takes t instead of x for the independent variable, where t denotes time. Also, derivatives, such as x0 (t) and x00 (t) with respect to time are often written as ẋ and ẍ respectively. 4.1. INTRODUCTION 4.1.2 139 How do ODEs arise? Population modelling. Assume a population grows at a constant rate proportional to the size of the population. If P = P (t) stands for the population at time t, then the model states that dP ∝ P, dt i.e., dP = kP, dt where k is the growth constant. If k > 0 the population is growing (think humans) and if k < 0 the population is decreasing (think tigers). Question: What happens for k = 0? Newton’s eureka moment. Newton’s second law of motion states that mass × acceleration = force. Let y = y(t) be the vertical displacement from ground at time t of an apple of mass d2 y m, soon to land on Newton’s head. Then the acceleration is 2 so that dt m d2 y = −mg, dt2 where g is the constant of acceleration at the surface of the earth due to gravity. 140 4.1.3 4. ORDINARY DIFFERENTIAL EQUATIONS Solution to an ODE Suppose that we are given an ODE for y which is an unknown function of x. Then y = f (x) is said to be a solution to the ODE if the ODE is satisfied when y = f (x) and its derivatives are substituted into the equation. Example: Show that y = y(x) = A exp(x2 /2) is a solution to the ODE y 0 = xy. When asked to solve an ODE, you are expected to find all possible solutions. This means that you need to find the general solution to the ODE. For an ODE that involves only the unknown function y and its first derivative, the general solution will involve one arbitrary constant. You should already know how to solve ODEs of the form dy = f (x) or dx d2 y = g(x). dx2 Example: Find the general solution to the differential equation y 0 = x2 . 4.1. INTRODUCTION 4.1.4 141 Initial value problems An initial value problem (IVP) is the problem of solving an ODE subject to some initial conditions of the form y(t0 ) = a, y 0 (t0 ) = b, etc. The solution to an initial value problem no longer contains arbitrary constants from the general solution to the ODE — these are determined by the initial conditions of the problem at hand. Example: 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 to time T ? 4.1.5 The order of an ODE The order of the highest-order derivative in an ODE defines the order of the ODE. Exercise: What is the order of each of these ODEs? • dP = kP dt • mẍ = −kx • x(y 00 )2 + y 0 y 000 + 4y 5 = yy 0 • y 0 = xy 142 4.1.6 4. ORDINARY DIFFERENTIAL EQUATIONS Riding your bike at constant speed Find the position of your bike (at time t) if you are travelling at a constant speed of 60 km/h along a perfectly straight road. If x = x(t) is the distance you have travelled at time t then the corresponding ODE is dx = 60. (∗) dt We can directly integrate to find x(t): Z Z dx = 60 ⇒ dx = 60dt. dt This yields the general solution to the ODE x(t) = 60t + C. To determine the constant C we need more information, such as your initial position at time 0, in which case we would be solving an IVP. For different values of C we get different solutions, and below we graph some of these. If C = 0 then y = 60t. All the other solutions are parallel to this line. 600 500 *) '( & 400 300 200 100 0 0 1 2 3 4 5 !#"%$ 6 7 8 9 10 The curves y = 60t + C are called solution curves to the ODE (∗). Note that in this particular case all curves are straight lines with slope 60. 4.1. INTRODUCTION 4.1.7 143 Motion of Projectiles Example: Consider an apple falling under gravity. Find an expression for the height y of the apple at time t. 144 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Now suppose you are throwing apples over your fence to your neighbour. Find an expression for the position (x(t), y(t)) of the apple if you assume the initial position is x(0) = 0, y(0) = 0 and the initial velocity is ẋ(0) = u, ẏ(0) = v. y x 4.1. INTRODUCTION 4.1.8 145 Extra reading: Realistic models More realistic models of projectile motion deal with air resistance. • For fast moving objects a good model includes air resistance proportional to square of the velocity d2 y dy dy = −g − f . dt2 dt dt • For slower objects, a good enough model has air resistance proportional to velocity. Realistic models may also include the fact that gravity diminishes as you move away from the earth’s surface (Newton’s inverse square law). Example: A model with air resistance Consider the motion of a ball subject to air resistance 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. Drag force, FD Direction of motion, (ẋ, ẏ) θ Gravity force, mg 146 4. ORDINARY DIFFERENTIAL EQUATIONS Note that sin θ = p ẏ ẋ2 + and ẏ 2 ẋ cos θ = p . 2 ẋ + ẏ 2 Hence F = ma in the y direction gives mÿ = −mg − FD sin θ ⇒ ⇒ ⇒ mÿ = −mg − kv 2 sin θ ẏ mÿ = −mg − k(ẋ2 + ẏ 2 ) × p ẋ2 + ẏ 2 k p ÿ = −g − ẏ ẋ2 + ẏ 2 . m In much the same way ẍ = − k p 2 ẋ ẋ + ẏ 2 . m This is a coupled system of ODEs which is extremely difficult to solve analytically. However, numerical solutions are easy to obtain, and you can undertake this a little later in the semester. A sample trajectory in the xy-plane is shown here. !"#$&%'!"()(*%(+ 18 No Drag Drag 16 14 12 10 8 6 4 2 0 0 5 10 15 20 25 30 35 4.1. INTRODUCTION 4.1.9 147 Analytical and numerical solutions To solve an ODE (or IVP) analytically means to give a solution curve in terms of continuous functions defined over a specified interval, where the solution is obtained exactly by analytic means (e.g., by integration). The solution satisfies the ODE (and initial conditions) on direct substitution. To solve an ODE (or IVP) numerically means to use an algorithm to generate a sequence of points which approximates a solution curve. Important remark: As we have already seen, the ODE dy = f (t) dt can be solved analytically. The solution simply is Z y = f (t)dt. Now this may seem like a cop-out because all we are saying is that the solution is given by the anti-derivative of f (t), and the above solution is as informative as the actual ODE. In practice one hopes to be able perform the above integral to get a more explicit form of the solution. Depending on the form of f this may either be done exactly or by numerical means. 4.1.10 Main points • You should understand what a differential equation (DE) is and how they can arise. • You should understand what it means for a function to be a solution to an ODE. • You should know what a general solution is. • You should know what an initial value problem (IVP) is. • You should be able to solve simple differential equations of the form dy = f (x) dx d2 y or = g(x). dx2 • You should understand what is meant by the order of a differential equation. • You should be able to derive, using Newton’s second law, equations of motion in a force field (such as gravity). • You should understand the difference between an analytical and numerical solution to an ODE. 148 4. ORDINARY DIFFERENTIAL EQUATIONS 4.2 Slope Fields and Equilibrium Solutions This material is covered in Stewart, Section 9.2 (Section 10.2). We have seen that in order to solve dy = f (t) dt we only need to integrate. However, for the more general first-order ODE dy = f (t, y) dt this no longer works. Nonetheless, the ODE gives a qualitative picture of the solution by noting that at (y, t) = (a, b) the slope of y(t) is f (a, b). So what one can do is as follows: • In the ty-plane, at (t, y) = (a, b) draw a small straight line with slope f (a, b). • Repeat the process for many different values of (a, b). • The resulting diagram is called the slope field. Note the slope field can be generated without having to solve the ODE. 4.2. SLOPE FIELDS AND EQUILIBRIUM SOLUTIONS 149 Example: the slope field of y 0 = 2y 4.2.1 1 0.8 0.6 0.4 0.2 0 −0.2 −0.4 −0.6 −0.8 −1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 From the slope field of y 0 = 2y we can see that y = 0 is one of the solution curves. It is a constant or equilibrium solution. 4.2.2 Equilibrium solutions An equilibrium solution is a constant solution y(t) = c to the ODE dy = f (t, y). dt The graph of an equilibrium solution is a horizontal line. Such a line has a slope of zero, i.e., y 0 = 0. This can only happen if f (t, y) = 0 has a solution y = c for some real constant c. 150 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Find the equilibrium solutions of y 0 (t) = −3(y − 1). !"#%$ 3 2 1 0 −1 −2 −3 −3 −2 −1 0 1 2 3 4.2. SLOPE FIELDS AND EQUILIBRIUM SOLUTIONS 151 Example: Find the equilibrium solutions of y 0 (t) = 2t + 1. "! 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 152 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Find the equilibrium solutions of y 0 = 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 4.2. SLOPE FIELDS AND EQUILIBRIUM SOLUTIONS 4.2.3 153 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 by looking at whether solution curves will tend toward the equilibrium solution or away from it as time increases. Formally, an equilibrium solution y(t) = y0 to the differential equation y 0 = f (t, y) 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. 154 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Here is the slope field for y 0 = 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 For this ODE, which equilibrium solutions are stable and which are unstable? 4.2. SLOPE FIELDS AND EQUILIBRIUM SOLUTIONS 4.2.4 155 Existence and uniqueness of solutions Consider the IVP dy = f (t, y), y(t0 ) = y0 . dt Then, if f (t, y) is “smooth” in some rectangle about (t0 , y0 ), there exists a unique solution y = y1 (t) at least in some small neighbourhood of (t0 , y0 ). y y0 t0 t It is an important point that equilibrium solutions cannot be crossed by other solution curves. In fact, no solutions curves can cross each other because this would mean that in some point(s) y 0 (t) has more than one value. Equilibrium solutions therefore partition the solution space. 4.2.5 Long-term behaviour Exercise: True or False? Assume the ODE dy = −3(y − 1) dt has initial condition y(t0 ) = c with c < 1. Then y(t) < 1 for all t > t0 . 156 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Consider the ODE y 0 = y 2 . It has one equilibrium solution y = 0. For y > 0, y 0 > 0 so that y(t) is increasing and hence any solution with y > 0 tends away from y = 0. For y < 0, 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.2.6 Main points • You should understand how to generate and interpret slope fields. • You should understand what is meant by an equilibrium solution and how to find one. • You should be able to determine the stability of an equilibrium solution. • You should understand that uniqueness of solutions to an IVP implies that solutions cannot cross. 4.3. EULER’S METHOD FOR SOLVING ODES NUMERICALLY 4.3 157 Euler’s Method for Solving ODEs Numerically Stewart, Section 9.2 (Section 10.2) covers Euler’s method. The method gives a simple approximate solution to an ODE and is closely related to the notion of a slope field. 4.3.1 Euler’s method using tangent lines What is the equation of the straight line with slope m that passes through the point (t, y) = (a, b)? Euler’s method uses tangent lines as approximations to solution curves. The tangent line to a solution curve of y 0 = f (t, y) at (t0 , y0 ) is y = y0 + f (t0 , y0 )(t − t0 ). This 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 our current point. This is Euler’s method. Using ∆t as the step size for our algorithm, 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 equations in sequence: y1 = y0 + f (t0 , y0 )∆t y2 = y1 + f (t1 , y1 )∆t y3 = y2 + f (t2 , y2 )∆t, and so on. 158 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Use Euler’s method with ∆t = 0.2, to find an approximate solution to y(0.6) for the IVP dy = 2t, y(0) = 0. dt Compare your answer with the actual value. 4.3. EULER’S METHOD FOR SOLVING ODES NUMERICALLY 159 The method is only accurate if you make ∆t small, which means a large number of steps is usually required. For this you might want to 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 160 4.3.2 4. ORDINARY DIFFERENTIAL EQUATIONS Euler’s method using Matlab; an example Consider the IVP dy = sin(xy), y(0) = 1. dx 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.3. EULER’S METHOD FOR SOLVING ODES NUMERICALLY 4.3.3 161 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? !"# 2 1.5 1 0.5 0 −0.5 −1 −1.5 −2 −2 4.3.4 −1.5 −1 −0.5 0 0.5 1 1.5 2 Main points • You should understand how slope fields lead to Euler’s method. • You should know how to approximate a solution to an IVP using Euler’s method by hand. • You should know how to implement Euler’s method in Matlab. • You should understand that errors can be a problem in the approximate solution, especially if a large step size is used. 162 4. ORDINARY DIFFERENTIAL EQUATIONS 4.4 Separable ODEs 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 this type of ODE. See also Stewart, Section 9.3 (Section 10.3). 4.4.1 Definition A first-order ODE is called separable if it can be written in the form dy = f (x)g(y). dx Exercise: Which of the following ODEs are separable? • dy = y(1 − y), dt • dy = ex+y , dx 2 • y 0 = e(x+y) , • ẏ = • ty + y , t2 dy = ty + y 2 . dt 4.4. SEPARABLE ODES 4.4.2 163 Solving separable ODEs 1 dy = f (x). g(y) dx Z 2. Integrate both sides with respect to x: 1. Rewrite the equation as 1 dy dx = g(y) dx Z f (x)dx. 3. Note the integral Zon the leftZ is a “substitution”, so that we can replace the dy = f (x)dx. last equation by g(y) 4. If one is lucky (or very smart, or both) one or both of the integrals can actually be evaluated. 5. If one is even more lucky (or even smarter) one can then finally explicitly express y as a function of x. Important remark: The final two steps will not always be possible. Whenever you are asked to solve a separable ODE or IVP in this course you are supposed to always “go as far as possible”. For good reasons we assume you are infinitely smart; not being able to compute simple integrals is never a valid excuse. For the IVP y 0 (x) = f (x)g(y), y(x0 ) = y0 , we again might have the problem that we can’t evaluate one of both of the integrals needed to solve the separable ODE. In this case, we can still write down an actual solution, be it a rather implicit one. Indeed, the solution to the IVP is given by Z y Z x dt = f (t)dt. y0 g(t) x0 164 Example: Solve the ODE 4. ORDINARY DIFFERENTIAL EQUATIONS dy x = . dx y The solution we found for the above example contains an arbitrary constant so is the general solution. In the case of an IVP this constant will be fixed to find one, particular solution. dy x Example: Solve the IVP = , y(0) = 3. dx y 4.4. SEPARABLE ODES Example: Solve the IVP 165 dy sin x = , dx y y(0) = 1. 166 4.4.3 4. ORDINARY DIFFERENTIAL EQUATIONS Singular solutions In step 1 of our recipe for solving separable ODEs we rewrote y 0 (x) = f (x)g(y) as 1 dy = f (x). g(y) dx Since we can’t divide by zero, this means that our method is only valid provided g(y) 6= 0. If there is an a such that g(a) = 0 then the ODE will have the equilibrium solution y(x) = a. This is easy to check: Such a solution is also known as a singular solution because it won’t generally arise from our earlier recipe. Whenever you are asked to solve a separable ODE you will have to check for singular/equilibrium solutions. dy = y(1 − y), Example: Solve the IVP dt logistic equation. y(0) = y0 . This corresponds to the 4.4. SEPARABLE ODES 167 The logistic equation has many uses in population dynamics, physical science and economics. 168 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Solve 4.4.4 dy = y2. dx Main points • You should be able to identify a first-order separable ODE. • You should know how to solve a separable ODE. • You should understand that equilibrium and singular solutions are equivalent and must be checked for when solving a separable ODE. 4.5. APPLICATIONS: LAW OF COOLING, POPULATION GROWTH 4.5 169 Applications: Law of Cooling, Population Growth Here we look at using some simple ODEs to model cooling and population dynamics. See Stewart Section 9.4 (Section 10.4). 4.5.1 Newton’s law of cooling Newton’s law of cooling states that the rate at which a “body” cools is proportional to the temperature difference between the body and its surrounding medium. If T is the temperature of the body and Tm the temperature of the surrounding medium then, according to Newton, dT = −k(T − Tm ), dt k > 0. Here the constant k is chosen such that if T > Tm , T 0 (t) is negative, describing cooling. Example: Solve this ODE with the intial condition T (0) = T0 . 170 4. ORDINARY DIFFERENTIAL EQUATIONS In real-life problems the cooling constant k is usually not known and you will have to infer it from measurements. Example: CSI Victoria Carl W.’s body was discovered in his cell at Barwon Prison, Victoria on the 19th of April 2010. The coroner’s report stated that: • C.W.’s body discovered at 11am on 19 April 2010. • Body temperature at 11am: 34.8◦ C. • Body temperature at 12.30pm: 34.1◦ C. • Temperature in C.W.’s cell: 21.1◦ C. Use the above information to determine the time of Carl’s death, assuming his body temperature at the time of death was 37◦ C. 4.5. APPLICATIONS: LAW OF COOLING, POPULATION GROWTH 171 172 4.5.2 4. ORDINARY DIFFERENTIAL EQUATIONS Malthus: “. . . doomed to misery and vice” Without inhibiting factors the rate at which the human population grows is proportional to its existing population; if P is the population at time t then dP = rP, dt where r is the growth rate (per human per year, say). The world population is currently growing at a rate of approximately r = 0.0125. If we take as initial condition the year 2000 with roughly 6 billion people on the planet, and ignore global warming so that we may take the total area of land on Earth to be constant at 150 million square kilometres, find in which year each human will have, on average, one square metre of land left to stand on. 4.5. APPLICATIONS: LAW OF COOLING, POPULATION GROWTH 173 174 4.5.3 4. ORDINARY DIFFERENTIAL EQUATIONS Verhulst to the rescue; down with the Malthusian law The problem with the Malhusian population model is that it does not contain a “damping” factor reflecting issues such as over-population, limited food or water supply, etc. The logistic model of Verhulst is the simplest model that has such built-in damping: dP P = rP 1 − , dt θ where θ is some very large constant, known as the carrying capacity. Note that when P is small(ish) then P/θ is small and we approximately have dP ≈ rP dt as before. When the population becomes close to the carrying capacity (i.e., P/θ very close to 1 so that not just 1 − P/θ ≈ 0 but also P (1 − P/θ) ≈ 0) then growth comes almost to a standstill. If we introduce new variables y = P/θ and τ = rt then the ODE becomes dy = y(1 − y). dτ We have already seen that if y(0) = y0 then this has solution y= y0 eτ . 1 − y 0 + y 0 eτ If P (0) = P0 in our original variables then we have θP0 ert P = . θ − P0 + P0 ert 4.5. APPLICATIONS: LAW OF COOLING, POPULATION GROWTH 175 Example: Identify the equilibrium (or singular) solutions to the logistic population model, and interpret these solutions in terms of population growth. Include a diagram. 176 4. ORDINARY DIFFERENTIAL EQUATIONS Example: 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. If E is the constant of proportionality for harvesting, their model is dP P = rP 1 − − EP. dt θ What restriction must be put on E so that the population will not die out? 4.5. APPLICATIONS: LAW OF COOLING, POPULATION GROWTH 4.5.4 177 Extra reading: numerically solving ODEs Matlab has some more sophisticated numerical methods for solving ODEs than Euler’s method. Matlab’s ode45 uses a Runge–Kutta fourth-order integration technique. To use it you need to write two little programs. The first one, say logistic.m, defines the differential equation function dy=logistic(t,y) dy=y*(1-y); The second program will numerically solve the ODE and plot your solution. y0=0.1; tfinal =6; [t,y]= ode45 (‘logistic’, tfinal, y0); plot (t,y) 4.5.5 Main points • You should be able to apply Newton’s law of cooling and solve the resulting IVP. • You should know how to model population growth and solve the resulting IVP. • You should be able to interpret the obtained solutions. 178 4. ORDINARY DIFFERENTIAL EQUATIONS 4.6 Solving Linear First Order Equations 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 exposition and examples are given in Stewart Section 9.5 (Section 10.5). 4.6.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 = log x • 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 4.6. SOLVING LINEAR FIRST ORDER EQUATIONS Recall the product rule for differentiation (written here in reverse order): f dg df d + g = (f g). dt dt dt This formula is key to solving first-order linear ODEs. Example: Solve dy 2t + 2 y = t2 + 1. dt t +1 179 180 4. ORDINARY DIFFERENTIAL EQUATIONS The lucky thing (it wasn’t luck really) about the previous example was that after multiplication by t2 + 1, the equation exactly matched the product rule. This raises the following natural question. Question: Can we always multiply a first-order linear ODE by some function I(t), so that its left-hand side matches the left-hand side of the product rule? This question can be answered in the affirmative and any function I(t) that does the trick is known as an integrating factor. Given the ODE dy + p(t)y = q(t) dt we multiply by the yet-to-be-found integrating factor I(t): dy + I(t)p(t)y = I(t)q(t). (∗) dt We think of I as g and y as f . Then the first term on the left is f 0 g and consequently we want the second term on the left to be f g 0 = yI 0 . Given that this second term is in fact Ipy, we infer that I must satisfy the ODE I(t) I 0 (t) = I(t)p(t). This is good news since this is a separable ODE which we already know how to solve: Z I(t) = exp p(t)dt . Important remark: Any integrating factor will do. Hence when you try to solve I 0 = Ip, i.e., when you try to compute the above integral (assuming you can explicitly do so), you do not need to worry about keeping track of constants. For example, if p(t) = 2t, you would get Z 2 2 2 I(t) = exp 2 tdt = et +C = eC et = B et , where B = exp(C) 6= 0. When you substitute this in (∗) the constant B would simply be an overall factor which may thus be divided out. It is therefore perfectly okay to write on your assignment: Because constants of integration do not matter, Z I(t) = exp 2 2 tdt = et . To confuse your tutor, you might even write: Because constants of integration do not matter, Z I(t) = exp 2 2 +65π 123 tdt = et . 4.6. SOLVING LINEAR FIRST ORDER EQUATIONS 181 Summary To solve a first-order linear ODE: dy + py = q. dt Z 2. Find an integrating factor I = exp pdt . 1. Write the equation in the form 3. Multiply the ODE by I and apply the product rule to get d (Iy) = Iq. dt 4. Integrate both sides with respect to t. (This time, don’t forget about constants of integration!). 182 Example: Solve the ODE 4. ORDINARY DIFFERENTIAL EQUATIONS dy = y + t. dt 4.6. SOLVING LINEAR FIRST ORDER EQUATIONS Example: Solve the IVP x2 dy + xy = 1, dx 183 y(1) = 2. 184 Example: Solve the ODE 4. ORDINARY DIFFERENTIAL EQUATIONS dy − x2 y = x2 . dx 4.6. SOLVING LINEAR FIRST ORDER EQUATIONS 4.6.2 185 Worked-out example: RL circuit The ODE describing the current in a circuit with a resistor and an inductor (known as an RL circuit) turns out to be linear. To obtain this 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 Amperes). R E L The voltage drop due to the inductor is L dJ where L is the inductance (in Henries). dt According to Kirchhoff’s Law the sum of the voltage drops is equal to the supplied voltage E(t) (in Volts). Hence L dJ + RJ = E(t). dt (∗) Example: Suppose in an RL circuit a battery supplies a constant voltage of 80V, the inductance is 2H and the resistance is 10Ω. (i) Find an expression for J(t), and (ii) determine the the current after 1 second if J(0) = 0. Solution: We first put (∗) into standard form: E(t) dJ RJ + = . dt L L An integrating factor for this ODE is Z I(t) = exp(R/L dt) = exp(Rt/L). Multiplying the ODE by this factor and using the product rule gives e Rt/L dJ dt Rt/L RJ +e E(t) Rt/L = e L L ⇒ d Rt/L E(t) Rt/L e J = e dt L Integrating the previous equation leads to Z Z 1 1 −Rt/L Rt/L Rt/L e J= E(t)e dt ⇒ J(t) = e E(t)eRt/L dt. L L 186 4. ORDINARY DIFFERENTIAL EQUATIONS So far we have treated everything completely generally. Now we use that the battery supplies constant voltage, i.e., E does not depend on t. This yields Z L E −Rt/L E J(t) = e eRt/L dt ⇒ J(t) = e−Rt/L eRt/L + C , L L R so that we finally obtain E + D e−Rt/L . R Since J(0) = 0, the constant D = −E/R: J(t) = J(t) = E 1 − e−Rt/L . R Since E = 80, R = 10 and L = 2: J(t) = 8 1 − e−5t . We can finally determine J(1) as J(1) = 8(1 − exp(−5)) Amperes. Remember that we derived a completely general solution to the ODE for any applied voltage E(t): Z 1 −Rt/L J(t) = e E(t)eRt/L dt. L So if we had a non-constant voltage such as E(t) = E0 sin(ωt), we would be able to find the solution for this case by evaluating the integral. 4.6.3 Main points • You need to be able to identify a first-order linear ODE. • You need to be able to solve a first-order linear ODE using an integrating factor. 4.7. LINEAR SECOND-ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS 4.7 187 Linear Second-Order Differential Equations with Constant Coefficients Second-order ODEs are covered in Stewart in Sections 17.1 and 17.2 (Sections 18.1 and 18.2). They often correspond with unforced (homogeneous) oscillators in our applications, but they arise in many other problems. 4.7.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. Exercise: Which of the following ODEs are linear? • y 00 + y 0 = y log x • 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 is an ODE that can be written in the form dy d2 y + p(t) + q(t)y = r(t). 2 dt dt The functions p(t) and q(t) are called the coefficients of the ODE and if r(t) = 0 the ODE is called homogeneous. 188 4.7.2 4. ORDINARY DIFFERENTIAL EQUATIONS The superposition principle If y1 (t) and y2 (t) are solutions of d2 y dy + q(t)y = 0, + p(t) 2 dt dt then so is y(t) = c1 y1 (t) + c2 y2 (t). Proof 4.7. LINEAR SECOND-ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS 189 Theorem: If y1 (t) and y2 (t) are two linearly independent solutions (i.e., y2 (t) 6= Cy1 (t)) of the homogeneous second-order linear ODE d2 y dy + q(t)y = 0 + p(t) 2 dt dt then the general solution is given by y(t) = c1 y1 (t) + c2 y2 (t). Important remark: For first-order ODEs we have seen that the general solution contains one arbitrary constant. The above result suggests that for second-order ODEs the general solution should contain two arbitrary constants, and this is in fact the case. 4.7.3 Reduction of order The previous theorem states that all we need to do in order to solve a homogeneous linear second-order ODE is to find two linearly independent solutions. Unfortunately, that is where the good news ends: no general method (such as the integrating factor method for linear first-order ODEs) is known to find two such solutions. There is however a method to find a second solution once a first solution has been found. So, if you are clever enough to guess one solution (and a guess it will have to be, given that we have no general method) you can obtain a second, linearly independent solution. This goes by the name of reduction of order. Assume your smart little sister has guessed that y1 (x) is a solution to dy d2 y + p(x) + q(x)y = 0. 2 dx dx To find a second solution y2 (x) you set y2 (x) = u(x)y1 (x). with u(x) a yet-to-be-determined function. Substituting this in the ODE gives u00 y1 + 2u0 y10 + uy100 + p(u0 y1 + uy10 ) + quy1 = 0. This may also be written as u00 y1 + u0 (y1 p + 2y10 ) + u(y100 + py10 + qy1 ) = 0. 190 4. ORDINARY DIFFERENTIAL EQUATIONS But y1 is a solution to the ODE so the third term actually vanishes! Thus y10 00 0 u +u p+2 = 0. y1 This is a first-order linear ODE in v = u0 which we known how to solve using an integrating factor. Example: One solution to the ODE x2 y 00 + 2xy 0 − 6y = 0 is y(x) = x2 . Use reduction of order to find the general solution. 4.7. LINEAR SECOND-ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS 191 192 4.7.4 4. ORDINARY DIFFERENTIAL EQUATIONS Homogeneous linear second-order ODEs with constant coefficients We next discuss how to solve a homogeneous linear second-order ODE with constant coefficients: ay 00 + by 0 + cy = 0, a 6= 0. The trick is to try to find a solution of the form y = eλt . Substituting this into the ODE gives aλ2 eλt + bλeλt + ceλt = 0. Since eλt 6= 0 we may divide both sides of the above equation by this term to be left with the quadratic equation aλ2 + bλ + c = 0. This equation is called the characteristic equation of the ODE. Important remark: When you are solving ODEs of this type on your assignments and exams, you don’t have to derive the characteristic equation. It is fine to simply start from the characteristic equation and solve the ODE from there. Various scenarios can now arise, which we discuss in more detail below. Case 1: Two distinct real roots If b2 − 4ac > 0 the characteristic equation aλ2 + bλ + c = 0 has two distinct real roots, say λ1 and λ2 . In this case the general solution to the ODE is y = c1 eλ1 t + c2 eλ2 t . Question: What will this solution look like for very large t? 4.7. LINEAR SECOND-ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS Example: Solve the ODE y 00 − 3y 0 + 2y = 0. 193 194 4. ORDINARY DIFFERENTIAL EQUATIONS Case 2: A single root of multiplicity 2 If b2 − 4ac = 0 the characteristic equation aλ2 + bλ + c = 0 has one root, say µ, of multiplicity 2. (Of course µ = −b/(2a) but it is more convenient to work with µ). This is somewhat of a problem because now we only have single solution to the ODE: y = eµt . Fortunately, we know that reduction of order will give us a a second linearly independent solution, so that we try y = u(t)eµt . By the product rule y 0 = u0 eµt + µueµt y 00 = u00 eµt + 2µu0 eµt + µ2 ueµt . Substituting this into ay 00 + by 0 + cy = 0 gives h i aµ2 + bµ + c u + 2aµ + b u0 + au00 eµt = 0. Since µ is a solution to the characteristic equation, aµ2 + bµ + c = 0. Moreover, since µ = −b/2a, 2aµ + b = 0. Hence we are left with just u00 = 0. This is easily solved as u(t) = (a + bt). Using this with a = 0 and b = 1 we now have two linearly independent solutions: y1 (t) = eµt and y2 (t) = teµt , where µ = −b/2a. Important remark: In actual problems you do not have to repeat the above derivation. It is enough for you to claim that if the characteristic equation has a single root of multiplicity 2 then the above are a pair of linearly independent solutions and y(t) = (c1 + c2 t)eµt is the general solution. Question: What will this solution look like for very large t? 4.7. LINEAR SECOND-ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS Example: Solve the IVP y 00 + 6y 0 + 9y = 0, y(0) = 2, y 0 (0) = 0. 195 196 4. ORDINARY DIFFERENTIAL EQUATIONS Case 3: Complex roots If b2 − 4ac < 0 the characteristic equation aλ2 + bλ + c = 0 has a pair of complex conjugate roots, say α ± iβ. This leads to the general solution i βt − i βt y(t) = c1 e + c2 e eαt . For those who want the solution to be manifestly real we note that e± i βt = cos(βt) ± i sin(βt), so that y(t) = (c1 + c2 ) cos(βt) + (c1 − c2 )i sin(βt) eαt . Introducing new constants b1 and b2 by b1 = c1 + c2 and b2 = (c1 − c2 )i we thus get y(t) = b1 cos(βt) + b2 sin(βt) eαt . Example: Solve y 00 + 9y = 0. 4.7. LINEAR SECOND-ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS Example: Solve the IVP y 00 − 4y 0 + 5y = 0, y(0) = 1, y 0 (0) = 0. 197 198 4. ORDINARY DIFFERENTIAL EQUATIONS Challenge problem: Solve the ODE y 0000 + y 000 − y 00 + y 0 − 2y = 0. 4.7. LINEAR SECOND-ORDER DIFFERENTIAL EQUATIONS WITH CONSTANT COEFFICIENTS 4.7.5 199 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) = 2. Then an Euler method 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) 4.7.6 Main points • You should be able to identify a linear second-order ODE. • You should understand the superposition principle and the general solution theorem for homogeneous linear second-order ODEs. • You should know how to solve homogeneous linear second-order ODEs with constant coefficients. 200 4.8 4. ORDINARY DIFFERENTIAL EQUATIONS Simple Harmonic Motion and Damped Oscillations This section deals with the modelling of oscillators by second-order ODEs. This material is covered in Stewart Section 17.3 (Section 18.3). 4.8.1 The undamped spring A spring of (natural) length L is stretched a distance s by a weight with mass m. In equilibrium we take the position of the weight (viewed as a point-particle) to be x = 0, with the positive x-axis pointing down. If we pull down the weight and then release it, it will start to oscillate. If we assume the weight-spring-system moves free of any resistance (no air-resistance and no internal friction in the spring) we say the spring is undamped. To derive an ODE for the undamped spring we must combine Newton’s second law and Hooke’s law. According to Hooke’s linear spring law the restoring force T exerted by the spring (“trying to unstretch”) is proportional to the distance d it is stretched: T = −kd. Here k > 0 is measured in N m−1 and is called the spring constant. Since k > 0, the tension in the spring acts to restore the spring to its natural length. The larger the spring constant, the more tightly-coiled the spring. Remark: As we all know from experience, Hooke’s law only works if we do not stretch a spring too much. 4.8. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 201 fixed support L T natural length L s s T +tve mg x(t) mg According to Hooke’s law the total force F is given by F = −k(s + x) + | {z } restoring force mg |{z} , gravitational force with x = x(t) the displacement out of equilibrium and g (≈ 9.8 m/s2 ) the acceleration on the Earth’s surface. Before we pulled down the weight the system was in equilibrium, so that ks = mg. We are thus left with F = −kx. (∗) According to Newton’s second law of motion the total force equals mass times acceleration: F = mx00 . (∗∗) Equating (∗) and (∗∗) leads to the equation of motion mx00 + kx = 0 (which holds throughout the Universe, not just on Earth). The equation of motion only depends on the ratio of k and m, and it is customary to introduce k ω2 = , m where ω > 0 is known as the angular frequency. Then the equation of motion for the undamped spring is thus given by x00 + ω 2 x = 0. 202 4. ORDINARY DIFFERENTIAL EQUATIONS This is a homogeneous second-order linear ODE with constant coefficients. The corresponding characteristic equation is given by λ2 + ω 2 = 0 with purely imaginary roots λ1 = iω and λ2 = −iω. Two linearly independent solutions are thus e i ωt and e− i ωt or cos(ωt) and sin(ωt). and the general solution is x(t) = c1 cos(ωt) + c2 sin(ωt). Although we could stop here it is standard to rewrite the above solution in the alternative form x(t) = A cos(ωt − φ), where the two constants c1 and c2 have been traded for two new constants A > 0 and φ ∈ (−π, π], known as the amplitude and phase shift. 4.8. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS Example: Find the relation between c1 , c2 and A, φ. 203 204 4. ORDINARY DIFFERENTIAL EQUATIONS Example: A spring with mass 9kg and spring constant 4N/m is pulled down 1m and given an initial upward kick of −0.5 m/s. Solve for position as a function of time and explicitly determine the amplitude of the oscillations. 4.8. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 4.8.2 205 The damped spring If you try the “spring-weight” system at home it is unlikely to display true simple harmonic motion. If that were possible you could build a perpetuum mobile. To make the model more realistic we need to take into account the effect of damping. This is caused by air-resistance and mechanical friction of the spring. The most common model of damping assumes that it corresponds to a force Fd , proportional to the velocity of the moving object, and opposite to the direction of movement. Hence Fd = −βx0 , where β > 0 is the damping constant measured in N s m−1 . The total force F for the damped spring is thus given by F = −βx0 | {z } damping force −k(s + x) + | {z } restoring force mg |{z} . gravitational force As before, ks = mg, so that F = −βx0 − kx. Thanks to Newton’s second law this yields the equation of motion mx00 + βx0 + kx = 0. This equation only depends on the ratios of k/m and β/m, and it is customary to not only set k ω2 = >0 m but also β 2p = > 0. m Then the equation of motion for the damped spring is given by x00 + 2p x0 + ω 2 x = 0. This is a homogeneous second-order linear ODE with constant coefficients. 206 4. ORDINARY DIFFERENTIAL EQUATIONS The corresponding characteristic equation is given by λ2 + 2pλ + ω 2 = 0, which has discriminant D = 4(p2 − ω 2 ). There are three different cases to consider. • D < 0 (i.e., p < ω). This is the underdamped or weakly-damped spring. • D = 0 (i.e., p = ω). This is the critically-damped spring. • D > 0 (i.e., p > ω). This is the overdamped or strongly-damped spring. 4.8.3 Underdamping For p < ω the characteristic equation λ2 + 2pλ + ω 2 = 0 has two complex conjugate roots p λ1 = −p + i ω 2 − p2 and λ2 = −p − i p ω 2 − p2 . Two linearly independent solutions are thus e−pt cos(ωp t) and e−pt sin(ωp t), where ωp = p p ω 2 − p2 = ω 1 − (p/ω)2 . The general solution is x(t) = e−pt c1 cos(ωp t) + c2 sin(ωp t) . 4.8. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 207 This figure shows an example of underdamped motion: "!$#%'&)(+*-,.0/1!$'&)(2*4356/178 1 0.8 0.6 0.4 B 0.2 ?A>@ 0 −0.2 −0.4 −0.6 −0.8 −1 0 10 20 9;30: #=< 40 50 60 It is best to write the general solution in terms of a single trigonometric function: x(t) = Ae−pt cos(ωp t − φ) = A(t) cos(ωp t − φ). Here A(t) is a time-dependent amplitude (as shown in the above figure with the dashed lines): A(t) = Ae−pt and ωp is the frequency of the underdamped spring. Since ωp p = 1 − (p/ω)2 < 1 ω the damping of the spring leads to a frequency red-shift and an increase in the period of the spring. When p approaches ω the period tends to infinity so that we may expect that the critically-damped spring does not display oscillatory motion. This is the case considered in the next section. 208 4. ORDINARY DIFFERENTIAL EQUATIONS Example: A mass of 1kg is attached to a spring hanging under gravity with damping constant 0.2 and spring constant 4. Find the position of the mass after time t if it is pulled down 1m from the equilibrium position and released without kick. 4.8. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 4.8.4 209 Critical damping For p = ω the characteristic equation λ2 + 2pλ + ω 2 = 0 has one root of multiplicity two: λ = −p. Two linearly independent solutions are thus e−pt and te−pt and the general solution is x(t) = e−pt (c1 + c2 t). Unlike the underdamped spring it is straightforward to express c1 and c2 in terms of the initial conditions x(0) and x0 (0): c1 = x(0) and c2 = x(0)p + x0 (0). Therefore x(t) = e−pt (x(0) + x(0)pt + x0 (0)t). The linear polynomial x(0) + x(0)pt + x0 (0)t vanishes for t=− x(0) . x(0)p + x0 (0) This corresponds to a positive time t if x0 (0) < −x(0)p. 210 4. ORDINARY DIFFERENTIAL EQUATIONS Depending on the initial conditions, the critically-damped spring behaves as shown in this figure: !"##$"%#&'($)*$+!#$#$ ,.-0/21 436587:9;-=<>[email protected]=<>DE FHGJIKL8MONQPSRUTWV FHGJIKL8MOKQPSRUTWV FHGJIKL8M*XYNZP[RUTWV 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 −0.2 0 1 2 3 4 5 6 7 8 9 10 • The top-most curve corresponds to x(0) and x0 (0) both positive, so that the weight is not just pulled down a distance x(0) but is also given a downward kick on release. • The middle curve corresponds to x0 (0) = 0 so that we only pull down the weight and then release it. • The third curve corresponds to x0 (0) < 0 so that upon release the weight is kicked upward. Since the spring overshoots the equilibrium we must also have that x0 (0) < −x(0)p. Example: A mass of 1kg is attached to a spring hanging under gravity, with spring constant 4. What value of the damping constant implies critical damping? 4.8. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 4.8.5 211 Overdamping For p > ω the characteristic equation λ2 + 2pλ + ω 2 = 0 has two distinct real roots λ1 = −p − p λ2 = −p + p p2 − ω 2 =: −β p2 − ω 2 =: −α with α > β > 0. Two linearly independent solutions are thus e−αt and e−βt and the general solution is x(t) = c1 e−αt + c2 e−βt . For generic initial conditions x(0) and x0 (0), the constants c1 and c2 are both nonzero. Assuming this, we have that for sufficiently large t x(t)critial damping ∼ c2 te−pt and x(t)overdamped ∼ c2 e−βt . √ Since β = p − . . . < p we see that the overdamped spring takes longer to return to equilibrium than the critically damped spring. The overdamped spring describes a door-closer that is too tight. It may take a long time for the door to actually close because the strong damping restricts (nearly) all movement. A tradeswoman will try to adjust a door-closer such that the damping will be as close as possible to critical damping. As always, Maths Rules! 212 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 down 1m from the equilibrium position and released without kick. 4.8. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 4.8.6 213 Summary of motion of a spring: The equation of motion of a mass m attached to a damped spring is described by the ODE x00 + 2p x0 + ω 2 x = 0, where k β > 0, 2p = >0 m m and k is the spring constant and β the damping constant. ω2 = The roots of the characteristic equation λ2 + 2pλ + ω 2 = 0 determine the type of motion: • p = 0; No damping — pure oscillatory or harmonic motion. The roots are ±iω and the general solution is given by x(t) = c1 cos(ωt) + c2 sin(ωt) = A cos(ωt − φ). • p < ω; Underdamped motion. p The roots are −p ± iωp , ωp = ω 1 − (p/ω)2 and the general solution is given by −pt x(t) = e c1 cos(ωp t) + c2 sin(ωp t) = Ae−pt cos(ωp t − φ). • p = ω; Critically damped motion. The single root (of multiplicity 2) is given by −p and the general solution is given by x(t) = e−pt (c1 + c2 t). • p > ω; Overdamped motion. p The two roots (denoted −α and −β) are −p ± p2 − ω 2 and the general solution is given by x(t) = c1 e−αt + c2 e−βt . 214 4.8.7 4. ORDINARY DIFFERENTIAL EQUATIONS The pendulum Some of you may have seen those beautiful old pendulum clocks. In such a clock a pendulum of mass m is suspended from a pivot by a long, thin metal rod. Now suppose that the pendulum moves in an arc of a circle with radius r. If the pendulum makes an angle (say in the anti-clockwise direction) θ with its equilibrium position, then it has travelled a distance rθ from this position. According to Newton’s second law of motion m d2 (rθ) = F, dt2 where F is the force in the direction of motion. Hence rm d2 θ = −mg sin θ. dt2 This is a nonlinear ODE which displays very interesting nonlinear behaviour. If we however assume that θ is very small (the pendulum does not swing very far out, unlike your little sister on the swing perhaps), then we have sin θ ' θ. Making this approximation we get d2 θ g + θ = 0. dt2 r 2 Setting g/r = ω as before we obtain the same ODE describing simple harmonic motion. Question: What is the period of the undamped pendulum? Remark: It is interesting to note that the ODE describing the undamped pendulum does not depend on the mass of the pendulum. 4.8. SIMPLE HARMONIC MOTION AND DAMPED OSCILLATIONS 215 As before one can make the model more realistic by assuming damping proportional to velocity. Example: If the damping constant is γ show that this leads to the ODE θ00 + 2p θ0 + ω 2 θ = 0, where ω2 = 4.8.8 g > 0, r 2p = γ > 0. m 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. 216 4.9 4.9.1 4. ORDINARY DIFFERENTIAL EQUATIONS Inhomogeneous Linear Second-Order ODEs Inhomogeneous linear second-order ODEs with constant coefficients Inhomogeneous second-order ODEs, also known as ODEs with forcing, are covered in Stewart in Section 17.2 (Section 18.2). They are very common in applications and often they require a bit of ingenuity to solve. We will only consider inhomogeneous linear second-order ODEs with constant coefficients, i.e., equations of the form ay 00 + by 0 + cy = r(x), where r is a continuous function. First we outline the general strategy of solving such equations. 1. First set r(x) = 0 and solve the corresponding homogeneous ODE. Its solution, yH (x) = c1 y1 (x) + c2 y2 (x), is known as the complementary function. 2. Find a solution, yP (x) to the inhomogeneous equation. Any solution will do and goes by the name of a particular solution. 3. The general solution to the inhomogeneous equation is then given by y(x) = yH (x) + yP (x). Clearly the remaining question is: How does one find a particular solution? Below we will describe two methods. The first is again reduction of order and is the more cumbersome, albeit most general method. We will illustrate it in the following example. 4.9. INHOMOGENEOUS LINEAR SECOND-ORDER ODES 217 Reduction of order partially-worked-out example Solve the ODE y 00 + y 0 − 2y = x2 − 2x + 3. • First we need to find the complementary function. The characteristic equation for the homogeneous ODE is λ2 + λ − 2 = 0. This has solutions λ = 1 and λ = −2, meaning that the complementary function is given by yH (x) = c1 ex + c2 e−2x . • Next, set either y(x) = ex u(x) or y(x) = e−2x u(x). We will take the first option but the second option works in the same way. Since y = uex , y 0 = ex (u0 + u) and y 00 = ex (u00 + 2u0 + u). Substituting this into the ODE and dividing by ex gives u00 + 3u0 = e−x (x2 − 2x + 3). This is a linear first-order ODE for u0 . • We can use the integrating factor I = e3x to solve this linear first-order ODE. The solution turns out to be 1 u(x) = (−2x2 + 2x − 7)e−x + c1 + c2 e−3x . 4 • Finally, we use y(x) = ex u(x) to find the general solution to the inhomogeneous equation as 1 y(x) = (−2x2 + 2x − 7) + c1 ex + c2 e−2x . 4 This is y(x) = yP (x) + yH (x) with yP (x) = 14 (−2x2 + 2x − 7). • By direct substitution you can check that the above yP (x) is indeed a particular solution of the ODE. Important remark: The previous method produced the general solution for the inhomogeneous equation given the complementary solution. It did not produce just a particular solution, which is all that is needed once the complementary solution is known. Below we will show that in many simple examples, you can save yourself a lot of work by only going for a particular solution. 218 4. ORDINARY DIFFERENTIAL EQUATIONS Simpler method We will consider the following cases for the inhomogeneous term r(x): • r(x) is a polynomial. • r(x) is a polynomial times an exponential. • r(x) is a combination of the above. Case 1: r(x) is a polynomial. The method is best illustrated through an example, and again we take y 00 + y 0 − 2y = x2 − 2x + 3. Because the polynomial on the right is of degree 2 we differentiate the ODE 2 times to get y 000 + y 00 − 2y 0 = 2x − 2 y 0000 + y 000 − 2y 00 = 2 Remembering that we are only after a particular solution, not the general solution, we can take yP00 = −1 as solution of the last equation since it correctly gives 0 + 0 − 2(−1) = 2. If yP00 = −1 then yP0 = −x + C. Substituting this into y 000 + y 00 − 2y 0 = 2x − 2 gives 0 + (−1) − 2(−x + C) = 2x − 2 so that C = 1/2, i.e., yP0 = −x + 1/2, or yP = (−x2 + x + D)/2. Substituting this in the original ODE gives (−1) + (−x + 1/2) − (−x2 + x + D) = x2 − 2x + 3, so that D = −7/2 and 1 yP = (−2x2 + 2x − 7). 4 4.9. INHOMOGENEOUS LINEAR SECOND-ORDER ODES 219 Important remark: In the above computations we twice had to fix a constant. Because “constants are constants” we could have ignored all x-stuff and streamlined the computations as follows: . . . then yP0 = −x + C. Substituting this into y 000 + y 00 − 2y 0 = 2x − 2 and ignoring any x’s gives 0 + (−1) − 2(C) = −2 so that C = 1/2, i.e., yP0 = −x + 1/2, or yP = (−x2 + x + D)/2. Substituting this in the original ODE again ignoring any x’s gives (−1) + (1/2) − (D) = 3, so that D = −7/2 and 1 yP = (−2x2 + 2x − 7). 4 Example: Find a particular solution of 3y 00 − 2y 0 + 4y = 2x3 + 1. 220 4. ORDINARY DIFFERENTIAL EQUATIONS Case 2: r(x) is a polynomial times an exponential Example: Find a particular solution to y 00 − y = 3xe2x . Partially worked-out solution: In this case r(x) = 3xe2x is not a polynomial so no amount of differentiating will reduce it to a constant. We thus have to get rid of the exponential first. The trick is to put y(x) = u(x)e2x with u(x) an unknown function. Exercise: Show that u must satisfy the ODE u00 + 4u0 + 3u = 3x. We do not need to find the general solution of the ODE satisfied by u, but again only need a particular solution. Exercise: Find a particular solution uP . Exercise: Use uP to find a particular solution yP . 4.9. INHOMOGENEOUS LINEAR SECOND-ORDER ODES 221 Important remark: The above method also works in the case that r(x) is a polynomial times a simple trigonometric function. For this case, you use the fact that e i x = cos x + i sin x to write your trigonometric function as either the real or imaginary part of an exponential. This allows you to use the above method, and then you need to take either the real or imaginary part (depending on if you had a cos or a sin term) of your particular solution at the end of the calculation. Case 3: r(x) is a sum of the previous two cases. The above two cases are enough to also deal with linear combinations such as r(x) = x2 + 2x + 1 + e2x (x + 1) or r(x) = x3 + xex + e3x . Theorem: If yP1 is a particular solution of ay 00 + by 0 + cy = r1 (x) and yP2 is a particular solution of ay 00 + by 0 + cy = r2 (x), then yP = yP1 + yP2 is a particular solution of ay 00 + by 0 + cy = r1 (x) + r2 (x). 222 4. ORDINARY DIFFERENTIAL EQUATIONS Example: Find a particular solution of y 00 − 2y 0 + y = x2 + 1 + e−2x . 4.9. INHOMOGENEOUS LINEAR SECOND-ORDER ODES 223 Example: Finding a general solution Find the general solution to y 00 + 4y 0 + 5y = 5x + 1. 4.9.2 Main points • You should be able to find a particular solution for the cases we have talked about. • You should know how to find the general solution as the sum of a particular solution and the complementary function. 224 5 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Parametrisation of Curves and Line Integrals Stewart covers parametrisation in Chapters 10 and (some of) 12 (Chapters 11 and (some of) 13). There are some features of Matlab, especially plotting, that will work with parametric forms. 5.1 Parametrisation of Curves Imagine some curve in the xy-plane, for example, x2 + 2y 2 = 1 or 3x + y = 2. One way to think of the curve is as the trajectory of a moving particle, such that at time t the particle is at the point (x(t), y(t)) on the curve. In other words, we describe the curve by ( x = f (t) y = g(t). Such equations for the curve are called parametric equations, and the auxiliary variable t, is called a parameter. The functions f and g are said to provide a parametrisation of the curve. y t2 > t1 (f (t2 ), g(t2 )) (f (t1 ), g(t1 )) x Before we look at some examples, it should be clear from our mental picture of a moving particle that a parametrisation of a curve is not unique. For example, ( x = F (t) y = G(t), where, say F (t) = f (3 − t) and G(t) = g(3 − t), is another parametrisation of the same curve; we simply have changed the velocity and initial (i.e., t = 0) position of the imaginary particle. 5.1. PARAMETRISATION OF CURVES 5.1.1 225 Sketching parametric curves Example: Sketch the curve parametrised as ( x = t2 − 2t y = t + 1. Give an equation for the curve in terms of just the Cartesian coordinates x and y and give two alternative parametrisations of the curve. 226 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Sometimes one has reasons to restrict t to a finite interval. In general, the curve with parametric equations ( x = f (t) a≤t≤b y = g(t) starts at the point (f (a), g(a)) and finishes at the point (f (b), g(b)). One has to be careful though, since even a finite time-interval can still give rise to an infinitely-long curve. ( x = t2 − 2t Example: Sketch the curve 0 ≤ t ≤ 4. y =t+1 5.1. PARAMETRISATION OF CURVES 227 Example: Sketch the Archimedean spiral given by ( x = t cos t y = t sin t and verify that it may also be described by x + y = arctan2 (y/x). 2 If you like spirals you could cos t x = t t > 0; sin t y = t also try to sketch cos t x = √ t t > 0; sin t y = √ t 0≤t≤ π 2 2 ( x = e−t cos t y = e−t sin t t ∈ R. For the true connoisseurs there even is the amazing Cornu spiral; who said maths couldn’t be fun? Z t cos(s2 )ds x = 0Z t > 0. t 2 y = − sin(s )ds, 0 228 5.1.2 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Parametrising circles and ellipses Example: Parametrise the circle of radius r centered at (a, b). 5.1. PARAMETRISATION OF CURVES 229 Example: Parametrise the circle with radius 2 centered at (2, 3), such that the imaginary particle tracing this circle is travelling in clockwise direction and at time t = 0 is at (0, 3). y (x − 2)2 + (y − 3)2 = 4 3 2 x 230 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: Parametrise the ellipse x2 y 2 + 2 = 1. a2 b A much more fancy parametrisation may be given using Jacobi’s elliptic functions sn, cn and dn. You will hopefully learn about these functions in a more advanced mathematics course. 5.1. PARAMETRISATION OF CURVES 5.1.3 231 Parametric curves in R3 In general, a curve in three dimensions can be parametrised in the form x = f (t) y = g(t) z = h(t). A helix is a three-dimensional curve that has shape of a wound spring, or a piece of string wrapped around a cylinder. One possible parametrisation of the helix is x = r cos(πt) y = r sin(πt) z = t for t ∈ R. Example: Sketch the helix for t ∈ [−2, 2]. 232 5.1.4 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Piecewise linear paths Example: Sketch the curve parametrised by 5.1.5 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. Plotting parametrised equations in Matlab The ezplot function handles parametric plots. To plot an ellipse, the command is simply ezplot(’2*cos(t)’,’1/3*sin(t)’) To plot a spiral, simply use ezplot(’exp(-t/100)*cos(t)’,’exp(-t/100)*sin(t)’) Executing this command Matlab uses a default range for the t values. To get a longer spiral, 0 ≤ t ≤ 100, run ezplot(’exp(-t/100)*cos(t)’,’exp(-t/100)*sin(t)’,[0,100]) 5.1. PARAMETRISATION OF CURVES 5.1.6 233 Main points • You should understand what a parametric representation of a curve is. • You should be able to parametrise circles and ellipses. • You should be able to sketch curves from their parametric representation. This includes being able to sketch parametric representations of parabolas, circles, ellipses, spirals, helices and piecewise linear paths. • You should be able to plot parametric forms in Matlab. 234 5.2 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Position Vectors, Velocity and Acceleration Stewart covers the following material in Section 13.4 (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)). y t2 > t1 (x(t1 ), y(t1 )) (x(t2 ), y(t2 )) r(t1 ) r(t2 ) x 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)). Example: Find the position vector of a car moving along the helical path x = cos 5t, y = sin 5t, z = t. 5.2. POSITION VECTORS, VELOCITY AND ACCELERATION 5.2.2 235 Parametrising straight lines We know from chapter 1 of this workbook that the position vector of any point on a straight line parallel to the vector v and passing through the point r 0 is given by r(t) = r 0 + tv. Example: Find the position vector of any point on the line passing through the 1 point (0, 1, 3) and parallel to the vector 2i − 3j + k. 2 236 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS 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 = t0 , and (c, d) corresponds to t = t1 , with t1 = t0 + ∆t and ∆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 a direction vector for the line that goes through (a, b) and (c, d). (c, d) (a, b) r 1 = r(t1 ) r 0 = r(t0 ) v 0 We know from chapter 1 that the position vector for points on the line through r 0 and r 1 is r = r 0 + λv = (1 − λ)r 0 + λr 1 , λ ∈ R. So all we need to do to parametrise the portion of the line from r 0 to r 1 is choose a relationship between λ and t that gives λ = 0 when t = t0 and λ = 1 when t = t1 . The linear relation between λ and t that does this is λ= t − t0 . ∆t A parametrisation for the line segment is therefore r(t) = r 0 + t − t0 v, ∆t t0 ≤ t ≤ t1 . 5.2. POSITION VECTORS, VELOCITY AND ACCELERATION 237 Example: Parametrise the path given in the following diagram, so that path C1 is given for 0 ≤ t ≤ 1 and the path C2 is given for 1 ≤ t ≤ 3. y −1 x C2 −1 C1 −2 238 5.2.3 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS 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. r(t) 0 ∆r r(t + ∆t) 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) = dx dy dz dr = 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, this means that it has constant speed and direction, i.e., it travels in a straight line. 5.2.4 Acceleration The acceleration vector is defined as a(t) = dv d2 x d2 y d2 z = 2 i + 2 j + 2 k. dt dt dt dt 5.2. POSITION VECTORS, VELOCITY AND ACCELERATION 239 Example: 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. Example: Find the velocity vector of a car moving along the helical path (e.g., the Indooroopilly Shopping Centre carpark) x = cos 5t, y = sin 5t, z = t. 240 5.2.5 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Quiz Can you match each position vector with one of the curves? Then locate the points corresponding to t = 0. (a) r(t) = (−t, 3/2 − t) (b) r(t) = (1, t − 1) 1 (c) r(t) = (− sin t, cos t) 2 (d) r(t) = 2(cos 3t, sin 3t). y y 1 2 1 x 1 2 x −1 y y 2 3 2 2 x 3 2 x 5.2. POSITION VECTORS, VELOCITY AND ACCELERATION 5.2.6 241 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 at a height of 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 that r(0) = i + 2k and v(0) = 2j. After the string breaks the stone moves under gravity ma(t) = −mgk ⇒ a(t) = −gk ⇒ dv = −gk. dt Hence v = −gtk + v(0) = 2j − gtk. Since dr = v this in turn implies that dt 1 r(t) = 2tj − gt2 k + r(0) 2 so that 5.2.7 1 2 r(t) = i + 2tj + 2 − gt k. 2 Main points • You should understand the relationship between a position vector and the parametric equations of a curve. • You should understand the relationship between position, velocity and acceleration vectors. • You should be able to parametrise lines and piecewise linear paths. 242 5.3 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Line Integrals and Work Done The content of this section is covered in Stewart over a few sections: arc length in Sections 8.1 and 10.4 (Sections 9.1 and 11.4) and general line integrals in 16.2 (17.2). 5.3.1 Length of a curve Say we have a curve in R3 parametrised by 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 can work out how to do this by first approximating the curve as a series of straight lines. We can draw a picture of what we are doing for a curve in R2 , but we can do the same thing for a curve in R3 . 5.3. LINE INTEGRALS AND WORK DONE 243 In this case, with ∆si representing the length of a small segment of the curve corresponding to a small change ∆t in the parameter from ti to ti + ∆t, and given r(t) = (x(t), y(t)) we have the approximation dr ∆si ≈ ∆t. dt t=ti The same formula applies in R3 and then the length of the curve is approximately the sum of the lengths of each line segment: 244 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS This approximation improves for smaller (and hence more) line segments. In the limit when the number of line segments tends to infinity, we obtain an exact result, giving the arc length formula s Z b 2 2 2 dx dy dz + + dt. arc length = dt dt dt a This formula is often written more succinctly using the notion of a line integral. If the function s(t) gives the length of the curve C between r(a) and r(t) — so that the arc length we are after is nothing but s(b) — then s 2 2 2 ds dx dy dz = + + . dt dt dt dt Then the length of the arc between r(a) and r(b) is given by the line integral Z ds. arc length = C In general, if f (x, y) is defined on a smooth curve C in R2 then the line integral of f along C is Z f (x, y)ds. (1) C If C is parametrised by r(t), a ≤ t ≤ b and f (x, y) > 0, then this line integral gives the area of a ribbon whose base is the curve C and height above the point (x, y) is f (x, y). z y r(a) C o f (x, y) x r(b) Line integrals are a generalisation of one-dimensional definite integrals. 5.3. LINE INTEGRALS AND WORK DONE Example: Find the length of the helix 245 x = cos 10t y = sin 10t z = t 0 ≤ t ≤ π. 246 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: Use the arc length formula to show that the length of a semicircular arc of radius 2 is 2π. 5.3. LINE INTEGRALS AND WORK DONE 247 Example: Find the length of the spiral x(t) = e−t/10 cos t for t ≥ 0. y(t) = e−t/10 sin t Solution: We compute r(t) = e−t/10 (cos t i + sin t j) 1 1 −t/10 −t/10 v(t) = e − cos t − sin t i + e − sin t + cos t j 10 10 r 1 1√ kv(t)k = 101e−t/10 . + 1 e−t/5 = 100 10 Hence Z ∞ arc length = 0 1√ kv(t)k dt = 101 10 The improper integral can be computed as Z ∞ Z −t/10 e dt = lim A→∞ 0 Z ∞ e−t/10 dt. 0 A e−t/10 dt 0 A = −10 lim e−t/10 0 A→∞ = −10 lim A→∞ e−A/10 − 1 = 10. Therefore the arc length of the spiral is √ 101. Example: Find the length of the parabola y = x2 for x between 0 and 2. Solution: First we parametrise the parabola as x = t, y = t2 for 0 ≤ t ≤ 2. Hence r(t) = (t, t2 ) ⇒ kv(t)k = p 1 + (2t)2 ⇒ ⇒ v(t) = (1, 2t) Z 2p arc length = 1 + (2t)2 dt. 0 (If you are really clever you can perhaps show that this gives √ 17 + arcsinh(4)/4.) 248 5.3.2 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Work done by a constant force In one dimension, the work done by a constant force F in moving an object along a straight line of length d is W = F d. In two or three dimensions, the work done by a constant force in moving a particle −→ along a straight line from P to Q is W = F · P Q. 5.3. LINE INTEGRALS AND WORK DONE 5.3.3 249 Work done over a curve We now consider the more general case of the work done by a force field F (x, y, z) = F1 (x, y, z)i + F2 (x, y, z)j + F3 (x, y, z)k 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 F · T ds. W = C This last integral, which is an integral over the curve C, is again known as a line integral. 250 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), y(t), z(t)), 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 ≈ kr 0 (ti )k∆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 . kr (ti )k 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 251 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 F (r(t)) · r 0 (t)dt. W = a It is common to express these integrals as Z F · dr. C This is merely a notational convenience. This notation means Z Z F · dr = work done = C where C is parametrised by r(t), a ≤ t ≤ b. a b F (r(t)) · r 0 (t)dt, 252 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS R Example: Evaluate C F · dr, where F = (xy, yz, zx) and C is parametrised as x=t 0 ≤ t ≤ 1. y = t2 3 z=t 5.3. LINE INTEGRALS AND WORK DONE 253 Example: Find the work done by moving a mass of 2 kg along an arc of the circle x2 + z 2 = h2 from (−h, 0, 0) to (0, 0, h). Does the direction we choose matter? 254 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS 5.3.4 Main points • You should know how to use a line integral to calculate the arc length of a curve. • You should know how to use a line integral to calculate the work done by a force field in moving a particle along a parametrised path. 5.4. GRADIENT FIELDS 5.4 255 Gradient Fields Gradient fields are a special type of force field which are conservative, and they give a relatively easy way to determine if the field is conservative. Stewart covers this in Sections 16.1 and 16.3 (Sections 17.1 and 17.3). 5.4.1 Z Conservative fields B F · dr is independent of the path taken, then F is called a conservative field. If A Z B F · dr will give the same result That is, if F is conservative then the line integral A for any path C that you choose between A and B. B C2 C1 A Example: 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. 256 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: Take the field F = xi + yj, which you can picture as a set of vectors F (x, y) at each point (x, y). y x Since F · dr = (xi + yj) · (dxi + dyj) = xdx + y dy, we get Z B Z B F · dr = A xdx + y dy A 1 = 2 Z B A iB 1h 2 2 d(x + y ) = x + y . 2 A 2 2 Once again F is a conservative field. But it was chosen in rather a special way. F is a gradient field, i.e., F = ∇f for some f . In this case f (x, y) = (x2 + y 2 )/2 since 2 x y2 F =∇ + = (x, y). 2 2 5.4. GRADIENT FIELDS 257 Z B F · dr is Theorem: Any gradient field F is a conservative and hence A path-independent. To show this, let F = ∇f = ∂f ∂f i+ j. Then using the chain rule, ∂x ∂y F (r(t)) · The work done is given by Z dr ∂f dx ∂f dy = + dt ∂x dt ∂y dt df . = dt B Z b F (r(t)) · F · dr = a A Z b = a Z dr dt dt df dt dt B = df A = f (B) − f (A). Important remark: The above means that finding the work done by a conservative field really comes down to finding a function f such that F = ∇f . This function f is called a potential function for the force field F . If you can find a potential function f such that ∇f = F then Z B F · dr = f (B) − f (A). A Important remark: This equation is by far the simplest way to evaluate the work RB done by a conservative field. When asked to evaluate A F · dr for a conservative field you are expected to use the above formula and not a parametrisation of the path. 258 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: Find the work done by the electric field E= xi + yj + zk x2 + y 2 + z 2 with potential 1 log(x2 + y 2 + z 2 ) 2 along any path from (1, 0, 0) to (1, 1, 1). f= 5.4. GRADIENT FIELDS 259 Example: Evaluate the work done by the force F from (0, 0) to (1, 2), where F = ∇f and f (x, y) = xy. Example: Show that F (x, y) = (x + y, x) is a gradient field. 260 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Example: Show that F (x, y) = y (x + y) i + j is not a gradient field. 2 2 It turns out that you don’t have to look for a potential function to check whether a force field F = (F1 , F2 ) is a gradient field. If we assume that F is a gradient field with potential function f then ∂f ∂f F = (F1 , F2 ) = , . ∂x ∂y ∂ 2f ∂ 2f We know that = when the second order partial derivatives are contin∂x ∂y ∂y ∂x uous, which means that the components of our force field F must satisfy ∂F1 ∂F2 = . ∂y ∂x In fact, the components of a vector field in R2 satisfy this condition if and only if F is a gradient field. This means that we can simply check this condition to determine whether a force field is a gradient field. You will look at this result in more detail in MATH2000. 5.4. GRADIENT FIELDS 261 Example: Determine whether the force field F = (y 2 − 2, 2xy) is conservative. Evaluate the work done by F in moving a particle along the path x = 2t, y = t2 , 0 ≤ t ≤ 2. 262 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS 5.4.2 Main points • You should understand the relation between a gradient field and its potential function. • You should understand that conservative fields and gradient fields are equivalent and that the work done by these fields in moving a particle between two points is independent of the path taken. • You should know that the potential function will give you an easy method to calculate line integrals for gradient fields. • You should know how to check whether a force field is a gradient field. • You should know how to find a potential for a conservative force field. 5.5. WORK DONE OR PATH INTEGRALS FOR NON-CONSERVATIVE FIELDS 5.5 263 Work Done or Path Integrals for Non-conservative Fields This material is covered in Stewart, Section 16.2 (Section 17.2). Line integrals for forces that are not conservative depend on the chosen path, which makes them harder to evaluate. To work them out we need to parametrise the path, by say t, and convert the integral to one over t. We have already seen how to do this in section 5.3. Let F (x, y) = F1 (x, y)i + F2 (x, y)j be a non-conservative field and let x(t) and y(t) provide a parametrisation of the path along which we wish to integrate. Then Z Z b dr dt. F · dr = F (r(t)) · dt C a Or, in component form, F · dr = F1 (x, y)dx + F2 (x, y)dy and Z b Z F · dr = C a dx dy F1 (x(t), y(t)) + F2 (x(t), y(t)) dt dt dt. There are some important properties of line integrals that can be useful when evaluating the work done by a non-conservative field: 264 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS Z F · dr, where F (x, y) = Example: Evaluate C as shown in the figure below. y 1 C2 C1 1 x x+y 2 i+ y j and the path is 2 5.5. WORK DONE OR PATH INTEGRALS FOR NON-CONSERVATIVE FIELDS 265 Z F · dr, were F = (x + y)i + yj and C is the arc of the Example: Evaluate C ellipse x2 + y 2 = 1 from (0, 1) to (2, 0) as shown in the figure below. 4 y 1 2 x 266 5. PARAMETRISATION OF CURVES AND LINE INTEGRALS 5.5.1 Main points • You should know how to recognise a non-conservative field. • You should know how to evaluate the work done by a non-conservative field by performing a parametrisation of the path. 267 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 or the MATH1051 notes. 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. 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 268 6. MATLAB GUIDE 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. 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 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 = 6.3. M-FILES 269 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 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. 270 6. MATLAB GUIDE 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 if 1st condition 1st commands elseif 2nd condition 6.3. M-FILES 271 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.2 For and while loops Loops allow MATLAB to perform repetitive operations automatically. There are two main types of loops: 272 6. MATLAB GUIDE • 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. 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; 6.4. VISUALISING FUNCTIONS 273 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]) 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 274 6. MATLAB GUIDE >> 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]) 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. 6.4. VISUALISING FUNCTIONS 275 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 !#"$ 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 You can alter the x and y axis ranges in the first few lines of the MATH1052quiver M-file: xmin xmax ymin ymax = = = = -1; 1; -1; 1; 276 6. MATLAB GUIDE 6.4.3 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 three 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. 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 6.5. VISUALISING DATA 277 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 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: 278 6. MATLAB GUIDE -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: • Write a loop which will vary t (or another independent variable) over some range; • For each value of t, produce a plot; 6.6. ANIMATIONS 279 • 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 t=(j-1)/5; for k=1:64 x(k)=(k-1)/10; for m=1:64 280 6. MATLAB GUIDE 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 % for simplicity x = input(1); y = input(2); output = (x-1)^2 + y^2 + 3; 6.7. OPTIMISATION 281 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: n X i=1 (mxi + c − yi )2 282 6. MATLAB GUIDE 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 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 i=1 (mxi + qyi + c − zi )2 6.8. DIFFERENTIAL EQUATIONS 6.8 6.8.1 283 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. !#" 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 284 6. MATLAB GUIDE A possible solution to the differential equation is shown below. "!#%$'&() 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 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. 6.8. DIFFERENTIAL EQUATIONS 6.8.2 285 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.3 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. 286 6. MATLAB GUIDE Higher-order methods can yield far more accurate solutions. One such is the Runge– Kutta 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) !#"$&%(')+* 0 −100 −200 −300 −400 −500 −600 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 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. DIFFERENTIAL EQUATIONS 6.8.4 287 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 dy (0) = 0 + ey = 0, y(0) = 1, 2 dt 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; 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). 288 6. MATLAB GUIDE >> 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)) 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 6.9. PARAMETRIC FUNCTIONS 289 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]) 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. 290 6. MATLAB GUIDE 6.10 Common commands and operations 6.10.1 Common numerical operations and 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 log(x) log10 (x) x × 10n 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 π √ −1 e ∞ −∞ MATLAB Code pi i exp(1) Inf -Inf 6.10. COMMON COMMANDS AND OPERATIONS 6.10.2 291 Vector and 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. 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 n × n identity matrix m × n matrix of ones m × n matrix of zeros MATLAB Code eye(n) ones(m,n) zeros(m,n)

Download PDF

advertisement