Lab 08 - Gradient fields and level curves 
[Names...]

Consider a function f(x,y) of two variables.  The gradient of f is a vector field (OverVector[∇])f=(OverVector[F])(x,y)=(Subscript[f, x])(OverHat[i])+(Subscript[f, y])(OverHat[j]). The gradient at (x,y) is

a vector pointing in the direction of maximum increase of the function.

perpendicular to the contour line (level curve) of f which passes through (x,y), that is, perpendicular to f(x,y)=k.

Its magnitude is the slope (directional derivative) in that direction,

so, as the curves (contour lines) get further apart, ‖(OverVector[∇])f‖ should get smaller.

Check this out for a particular function:

({{({{f(x,y)=((x^2)/4)}})+((y^2)/9)}})

myf=x^2/4+y^2/9

The gradient of this function is...

myGradf=Grad[myf,{x,y}]

Overlaying Vector Fields/Gradient Fields

p1=VectorPlot[myGradf,{x,-4,4},{y,-4,4},VectorColorFunction->(Red&)]
p2=ContourPlot[myf,{x,-4,4},{y,-4,4},Contours->{1,2,3,4}]
Show[p2,p1]

1.) Hopefully at any point the vectors should appear to be perpendicular to the contour lines.  
But...this only holds when the size of one unit in the x-direction on screen measures the same as one unit in the y-direction.

You can control this in two ways:

If your x-range ((Subscript[x, max])-(Subscript[x, min])) is the same as your y-range, the aspect ratio is usually right in 2-D plots.

Using the AspectRatio->Automatic option. Execute the 2 examples below to see Mathematica’s interpretation of AspectRatio. What is the same in the horizontal and vertical direction when “AspectRatio->1” is set? (*not* the relative sizes of x- and y-units...)

Plot[Sin[x],{x,0,2Pi},AspectRatio->Automatic]

Plot[Sin[x],{x,0,2Pi},AspectRatio->1]

2.) Below, replot the vector field and contours from the same function above, but with x and y intervals which are *quite different* from each other.  Do the vectors still appear perpendicular to the contours?

When is a vector field a gradient vector field?

For a function f(x,y), we saw the order of differentiation for the second derivatives doesn’t matter (Clarault’s Theorem), that is:

({{({{(Subscript[f, xy])=(Subscript[f, yx])
(∂/(∂y))(Subscript[f, x])=(∂/(∂x))(Subscript[f, y])}})}})

Let P and Q be functions of x and y, and consider the vector field (OverVector[F])=P (OverHat[i])+Q (OverHat[j]).   

*If* (OverVector[F]) is the gradient of some function f, then it must be the case that P=(Subscript[f, x]) and Q=(Subscript[f, y]) and therefore it must be the case that

({{({{({{
(∂/(∂y))P=(∂/(∂x))Q}})}})}})

When this is true, we say that

(OverVector[F]) is conservative.

(OverVector[F]) is the gradient of a “potential” function f.

For each of the vector fields below:

Make a VectorPlot[ ]for each of the following vector fields.

Calculate (using Mathematica) (∂/(∂y))P and (∂/(∂x))Q for each vector field, and then, using the test above, say whether the vector field is conservative or not.

Just for the functions that you find are conservative ones, find (guess) the potential function, and make a contour plot of the potential function you found. 
Display the vector plot of the vector field and the contour plot of the potential function together (with appropriate x- and y-ranges), to verify that the vector field arrows line up with the contours of the potential function as they should.

3.) (OverVector[F])(x,y)=3x (y^2) (OverHat[i]) +3 (x^2)y (OverHat[j])

4.) (OverVector[F])(x,y)=3x (y^2) (OverHat[i]) -3 (x^2)y (OverHat[j])

5.) (OverVector[F])(x,y)=x sin(x y) (OverHat[i]) +y sin(x y) (OverHat[j])

6.) (OverVector[F])(x,y)=y sin(x y) (OverHat[i]) +x sin(x y) (OverHat[j])

7.) (OverVector[F])(x,y)=(2 x+y) (OverHat[i]) +(x+3 (y^2) (OverHat[j])

8.) Consider this vector function of three variables,

(OverVector[F])(x,y,z)=y z (e^(x y z)) (OverHat[i])+x z (e^(x y z)) (OverHat[j])+x y (e^(x y z)) (OverHat[k])

Make a conjecture about how to test such a 3-D vector field to see if it’s the gradient of a 3-D potential function. 

Try out your test.  

Find the potential function for this gradient field, if possible.