Multivariate Calculus Partial Derivatives 1 Theory Recall the definition of the partial derivatives of a function of two variables, z = f(x, y): f x = lim f(x + x, y) f(x, y) x 0 x f y f(x, y + y) f(x, y) = lim y 0 y The notion of a limit is required to define the instantaneous rate of change of a function because when we take x = 0 or ( y = 0) the numerator and the denominator of the difference quotient both evaluate to 0. However, for many functions if you fix a point, (x, y ) in the domain, the slopes of the secant lines (defined between x and x + x) get arbitrary close to a unique number, provided you choose x small enough. This is the intuitive definition of the limit existing as x goes to 0 and is how we define the derivative of a function at the point x. Recall that a plane passing through the point (p x, p y, p z ) can be expressed with an equation of the form or equivalently, if c 0, a(x p x ) + b(y p y ) + c(z p z ) = 0, z = p(x, y) = [ a c (x p x) + b ] c (y p y) + p z. If we fix a value of y = p y then this equation becomes z = p(x, p y ) = a c (x p x) + p z, which is an equation for a line with slope a/c. If z = p(x, y) is the plane tangent to a surface given by z = f(x, y), passing through the point (p x, p y, p z ), then the slope a/c is also equal to the partial derivative f (p x x, p y ). Similarly if we hold x = p x fixed we have 1
z = p(p x, y) = b c (y p y) + p z, and the slope of this line ( b/c) is the partial derivative f y (p x, p y ). Finally, the value p z must be equal to f(p x, p y ) since the tangent plane touches the surface exactly at the point of tangency (p x, p y, p z ) = (p x, p y, f(p x, p y )). Putting this all together we derive a formula for the tangent plane to a differentiable function f : R 2 R at the point (p x, p y ) in the domain of f: z = p(x, y) = f x (p x, p y ) (x p x ) + f y (p x, p y )(y p y ) + f(p x, p y ). If a function attains a maximum, or a minimum in the interior of its domain and the derivative is defined there, then the derivative (in any direction) must be 0. But the derivative at a point might be 0 even if the function does not attain a max or a min there. As we have seen, this point could be a saddle point (where along certain directions the point is a maximum, while along others it is a minimum). This lab explores these concepts. 2 Matlab Useful Matlab Functions/Concepts: surf(x,y,z) Matlab has several tools for creating 3-dimensional plots of data in three dimensions. The one we will be utilizing most frequently in this course is the surf(x,y,z) command, which generates a surface from data. This command accepts inputs of several different types but we will primarily choose X and Y to be vectors. If X and Y are vectors, length(x) = n and length(y) = m, where [m,n] = size(z) then the vertices of the surface faces are (X(j),Y(i), Z(i,j)) triples. As with the plot() command, the surf() command only knows how to interpret a list of data points. Thus, to represent the graph of a function, you must create a list of inputs and corresponding outputs. For a surface there are two inputs (X and Y) and one output (Z). In order to compute the height of the surface Z(i,j) for each pair of inputs X(i), Y(j) you can use the meshgrid(x,y) function to create a grid of inputs 2
which can then be used to evaluate the height of the surface over each point in the XY-plane. Example: >> clear >> clc >> X = linspace(-4,1,50); >> Y = linspace(-2,3,70); >> [XX,YY] = meshgrid(x,y); >> Z = 2.*XX.^2-3.*YY.^2; >> surf(x,y,z) find(x) Often you will want to find elements of an array satisfying certain conditions. For example if you are only interested in positive values, only the 5ht, 6th, and 7th values in the list { 4, 3, 2, 0, 1, 2, 9} are of interest to you. The find(x) function takes in an array X and returns a list of the linear indices of the nonzero elements of X. For example if you enter the command >> find([-4,-3,-2,0,1,2,9]), Matlab will return the array [1,2,3,5,6,7], indicating that the numbers in positions 1, 2, 3, 5, 6, and 7 are nonzero. You can use the find function to identify elements which satisfy more complicated conditions than being nonzero when you pair it with logical indexing. Recall that relations like ==, >, < can be used to produce logical arrays of 0s and 1s. Then the find function can be used on the resulting logical array to find all 1s (i.e. all those indices which satisfy the condition which produced the logical array). Example: >> A = [-4,-3,-2,0,1,2,9] >> A > 0 >> find(a > 0) >> A(find(A>0)) The second command should produce the logical array [0,0,0,0,1,1,1] and the third command will produce the list of indices [5,6,7]. You 3
can use a list of indices (the output of the find function) to extract only those elements of A which satisfy the condition greater than 0, as is done with the fourth command which should return the list of the 5th, 6th and 7th elements of A (i.e. [1,2,9]). 3 Experimentation 3.1 Investigation 1 This investigation concerns the function of 2 variables z = f(x, y) = x 2 y 2. Step 1: Create a grid of input values using the meshgrid(x,y) function, for the range of inputs 1 x, y 1 and produce the output array according to the above formula. Step 2: Plot the surface z = f(x, y) using the surf(x,y,z) command. Step 3: The point in space (1,2,-3) is on the surface z = f(x, y). Use the scatter3(x,y,z) function to plot this point on the same figure window as the surface plot, and verify that the point appears to be on the graph of this function. Step 4: Create a list deltax = linspace(0,.1,50) and for each value, x, in the list compute the difference quotient where (x, y ) = (1, 2). f(x + x, y ) f(x, y ) x As x approaches 0, to what does the difference quotient converge? Is this consistent with what you expect f/ x at (1, 2) to be? 3.2 Investigation 2 This investigation concerns the function z = f(x, y) = x 2 y 2 + 3. We hope to compute the plane tangent to the point (p x.p y, p z ) = (1, 1, 1) on the surface. 4
Step 1: In a script define variables px = 1, py = 1 and pz = -px^2-py^2+3. Step 2: Compute by hand the partial derivatives f f and and define variables dfdx and dfdy to be functions of px, py, and pz according to the x y formulas you derive. Step 3: Create two grids of inputs using the meshgrid(x,y) function, one over the range 2 x, y 2 and one over the range px 1/2 x px + 1/2 and py 1/2 y py + 1/2. Compute and plot the surface z1 = f(x,y) over the first meshgrid, and z2 = p(x,y) = dfdx(x-px)+dfdy(y-py)+pz over the second. For px = py = 1 is the plane tangent to the surface? Change px and py and verify that the planes are tangent to the surface at the specified point. 3.3 Investigation 3 This investigation concerns the function z = f(x, y) = x 3 + 6xy 2 2y 3 12x. Step 1: Plot the surface z = f(x, y) over the range 3 x, y 3. Step 2: By hand compute the stationary points (where f/ x = f/ y = 0) of f(x, y) and plot them on the same figure as the surface. By looking at the points on the surface, classify each as either a min, max, or saddle point. Step 3: By hand compute formulas for the second derivatives f xx, f yy, and f xy. Use Matlab to compute f xx, f yy, and f xx f yy f 2 xy at each stationary point. If possible, use these quantities confirm that your classifcation of the stationary points is correct. Step 4: By hand compute f(x, y) at the point (x, y) = ( 2, 2). Then construct the variable deltax = linspace(0,.5,50), and for each x in deltax compute the point (x, y) = (0, 1) + ( f/ f )(δx). For each such point (x, y), scatter plot the points (x, y, 0) and (x, y, f(x, y)) on the same figure as the surface. 5
The points (x, y, 0) in the domain represent points along the gradient direction. The points (x, y, f(x, y)) on the surface are the correspoding function values. How are the function values changing as the inputs move away from the point (0,1)? If you were to change inputs along any other direction, describe how you think the function values will change relative to how the function values change along the gradient direction. 4 Application Create a new Matlab script called lastname partials.m which performs the following steps and use it to answer the questions in this lab. In a study on the Northern Blowfly, the crawling speed of the larvae was found to be a function of larvae body length l and ambient temperature T, v(l, T ) =.65lT e.315l.03t 1.2, measured in cm/min. This function was derived from data gathered from larvae whose lengths ranged from 1 to 10 mm over temperature ranges from 0 to 40 degrees Celsius. This result is very useful in forensic entomology and can be used to estimate the time that elapsed following a larvas departure from a corpse. 4.1 Coding: Step 1: Plot the function v(l, T ) as a function over the range 1 l 10 and 0 T 40. (Choose a mesh with 100 data points over each variable range). Step 2: Compute the partial derivatives of v(l, T ) by hand (see Question 1) and use these equations to plot the partial derivatives as surfaces over the same variable ranges as the original function. Each plot should be in a separate figure window. Step 3: Compute the values of the partial derivatives at the following length and temperature vector: (l, T ) = (5, 16) and store these values in the variables dvdl and dvdt. 6
Step 4: Fix a variable temp = 27 degrees Celsius and plot the graph of velocity as a function of just larvae length from 1 to 10 mm, for this fixed value of temperature. In other words plot the function v(l, 27) over the range 1 l 10. Step 5: Use the find(x) function to identify the range of larvae lengths which move at speeds greater than 3 cm/min, at 27 degrees Celsius. Store all lengths of larvae which can acheive at least 3 cm/sec in a list called fastlarvae. 4.2 Questions: Question 1: What are the partial derivatives of v(l, T )? Question 2: Does there appear to be a value for temperate and length which maximizes the speed of the larvae? Is so what is it approximately? Question 3: Will a longer larvae always be faster than a shorter larvae according to this model? Question 4: A larvae is 5 mm long in an ambient temperate of 16 degrees Celsius. If the ambient temperate rises just a little, will the larvae s speed increase or decrease? Question 5: What is the approximate length and speed of the fastest larvae at 27 degrees Celsius? Question 6: Approximately what is the longest and the shortest larvae which can move at least at 3 cm/min at 27 degrees Celsius? 7