Lab 2 Tutorial

There are two files on the web server for this lab.   This file contains a tutorial and instructions.  You should do your work in a separate file -- I have provided a template called Lab2_report.nb.

We are going to get Mathematica to do three things for us:
1.  Draw graphs of functions of two variables illustrating tangent plane approximation for differentiable functions and failure of tangent plane approximation for non-differentiable functions.
2.  Calculate approximate change in a function, using the tangent plane approximation.
3. Calculuate directional derivatives.

We could do tasks 2 and 3 by hand, or with an ordinary calculator as well, but task 1could not be done as well with an ordinary calculator.

Part 1.  Tangent plane approximation and graphs.

We are going to 
1.  Define a function in Mathematica
2.  Plot the graph of the function near a chosen point.
3.  Calculate the tangent plane to the graph at the chosen point (using partial derivatives).
4.  Plot both the graph and the tangent plane together.
5.  Plot the graph and tangent plane together in smaller and smaller regions containing the chosen point.

\(\(\(
\)\(\(Clear[x, y, f];\)

    f[x_, y_] := 1 - \(x\^2\) y\^4/4

    \(Plot3D[f[x, y], {x, \(-2\), 2}, {y, \(-2\), 2}];\)\)\)\)

We plot the graph again in a neighborhood of a chosen point (x0, y0).  The "radius" of the region is a, which is initially set to be 2.

x0 = 2;

    y0 = 5/2;

    a  =  2;

    z0 = f[x0, y0];

    Plot3D[f[x0 + dx, y0 + dy] - z0, {dx, -a, a}, {dy, -a, a}, 
        AxesLabel -> {"x-x0", "y-y0", "z-z0"}];

We execute the same Mathematica code, with a set to be .2, and then .02

a  =   .5;
z0 = f[x0, y0];
\(\(Plot3D[f[x0 + dx, y0 + dy] - z0, {dx, \(-a\), a}, {dy, \(-a\), a}, 
        AxesLabel -> {"x-x0", "y-y0", "z-z0"}];\)
\)
a  =   .2;
z0 = f[x0, y0];
\(\(Plot3D[f[x0 + dx, y0 + dy] - z0, {dx, \(-a\), a}, {dy, \(-a\), a}, 
        AxesLabel -> {"x-x0", "y-y0", "z-z0"}];\)
\)
a  =   .02;
z0 = f[x0, y0];
Plot3D[f[x0 + dx, y0 + dy] - z0, {dx, -a, a}, {dy, -a, a}, 
        AxesLabel -> {"x-x0", "y-y0", "z-z0"}];

We calculate the partial derivatives of our function:

\(\(\(
\)\(\(Clear[fX, fY];\)

    fX[x_, y_] = D[f[x, y], x]

    fY[x_, y_] = D[f[x, y], y]\)\)\)

We calculate the gradient of the function at an arbitrary point (x,  y).

G[x_, y_]  :=  {fX[x, y],  
        fY[x, y]}

    MatrixForm[G[x, y]]

The formula for the tangent plane at (x0,  y0) is
λ[x,  y]  =  
        z0  +  G[x0, y0]  .  {x - x0,  y - y0}
as has been discussed in lecture.

λ[x_,  y_]  := 
      N[ z0  +  G[x0, y0] . {x - x0,  y - y0}]

    λ[x, y]

We plot the graph of the function together with the graph of the tangent plane in a region of radius a around (x0, y0), for several values of a.

Clear[a,  plot,  tangent]
Off[General::"spell1"]
\(\(plot[a_]\  := \ 
        Plot3D[f[x0 + dx, y0 + dy] - z0, {dx, \(-a\), a}, {dy, \(-a\), a}, 
          AxesLabel -> {"x-x0", "y-y0", "z-z0"}, 
DisplayFunction -> \ 
            Identity];\)\)
\(\(tangent[a_]\  := \ 
        Plot3D[λ[x0 + dx, y0 + dy] - z0, {dx, \(-a\), 
            a}, {dy, \(-a\), a}, 
          AxesLabel -> {"x-x0", "y-y0", "z-z0"}, 
DisplayFunction -> \ Identity];\)\)

a  =  2;
both  =  
      Show[{plot[a],  tangent[a]},  
        DisplayFunction ->  $DisplayFunction]

a  =   .5;
both  =  
      Show[{plot[a],  tangent[a]},  
        DisplayFunction ->  $DisplayFunction]

a  =   .01;
both  =  
      Show[{plot[a],  tangent[a]},  
        DisplayFunction ->  $DisplayFunction]

Part 2.  Calculate approximate change of a function, using the tangent plane approximation.

The change in a linear function λ[x, y] corresponding to a vector displacement ΔX is G . ΔX, where G is the gradient of the linear function.  The approximate change in a non-linear function f[x, y], using the tangent plane approximation, starting at some (x0,  y0), corresponding to a vector displacement ΔX is G . ΔX, where G is the gradient of the non-linear function evaluated at (x0,  y0).

Using another notation, the approximate change is ∇f[x0, y0] •ΔX.

Clear[x0,  y0,  ΔX,  f, gradf,  change,  x,  
      y] 
{x0,  y0}  =  {1, 2};
ΔX  =  {1.2,  1.99}  -  {1, 
          2}
f[x_,  y_]  :=  Cos[x  y]
gradf[x_,  
        y_]  =  {(∂_x) f[x, y],  (∂_y) 
          f[x,  y]}
N[gradf[x0,  y0]]
change  =  gradf[x0,  
          y0] . ΔX

Part 3.  Calculate Directional derivatives

The directional derivative of a non-linear function f[x, y]  at a point (x0, y0) in the direction u (specified by a unit vector) can be calculated as ∇f[x0,y0]•u.

The length of a vector is computed in Mathematica by the command Norm[ ].  A unit vector in the direction of ΔX is computed by u = ΔX/Norm[ΔX].

ΔX  =  {5, 6}
Norm[ΔX]
u  =  ΔX/
        Norm[ΔX]

Clear[x0,  y0,  ΔX,  f, grad,  x,  
      y] 
{x0,  y0}  =  {1, 
          2};

ΔX  =  {5, 6}
u  =  ΔX/
        Norm[ΔX]


f[x_,  y_]  :=  (x^3) (y^4) Cos[x y]
gradf[x_,  
        y_]  =  {(∂_x) f[x, y],  (∂_y) 
          f[x,  y]}
N[gradf[x0, y0]]

deriv  =  N[gradf[x0, y0] . u]

1.  Use Mathematica to graph the function 
f[x, y] = 
        3  Cos[π x y ] (e^(-((x^2) + (y^2)))) 
and its tangent plane together at (x0,  y0) = (1/5, 1/7). Plot the graphs on several different scales, i.e. in regions of different diameter.

2.Find the approximate change of the function
 f[x, y] = 
        3  Cos[π x y ] (e^(-((x^2) + (y^2)))) 
 as (x,  y)  changes from (x0, y0) = (1/2, 3/4)  to (.52,  .73) , using the tangent plane approximation,

3. Find the directional derivative of the function
 f[x, y] = 
        3  Cos[π x y ] (e^(-((x^2) + (y^2)))) 
 at (x0, y0) = (1/2, 3/4) in the direction of the increment  (4,  5).

Do this work in a separate notebook. I have provided you with a template notebook called Lab2_report.nb.