Calculus IV Lab 1: Functions, Graphs, and Integrals

Functions, operators, and graphs

Composition of functions

There are different ways to define a function of more than one variable in Mathematica.

f[x_,y_,z_,t_]:=((x y z Sin[2π t])/((x^2)+(y^2)+(z^2)))

g[x_,y_]:={y,(x^2)}

h[t_]:={Sin[t],Cos[t],t,2t}

Taking the composition of two functions is relatively easy (don't forget to evaluate the functions above first):

h[f[x,y,z,t]]

However, try:

g[g[x,y]]

This doesn't work.  It SHOULD work, but it doesn't. To get around this problem, we will often define functions of more than one variable in the following way:

g[{x_,y_}]:={y,(x^2)}

Now:

g[g[x,y]]

Will f[h[t]] work with the above definitions?  Should it?  If so, modify the definitions so it does.

Functions as arguments (Operators)

Notice that if you have a function of a single variable, Mathematica knows what you mean by:

h'[t]

Partial derivatives are also pretty straightforward:

(∂_x) g[x,y]

Sometimes, it is useful to define a function that operates on other functions.  So, for example, I could define a function to find the derivative matrix for functions from (ℝ^2)->(ℝ^2):

deriv2By2[expression_]:={(∂_x) expression,(∂_y) expression}//Transpose//MatrixForm

deriv2By2[{x Sin[y],x Cos[y]}]

deriv2By2[g[x,y]]

(Notice that I needed to transpose the matrix to get the rows/columns to work out like we want.  I used MatrixForm just because it looks cool.)  However, you have to be really careful with these.  For example, if I just make one minor little change when calling it:

deriv2By2[{u Cos[v],u Sin[v]}]

The problem is that, when we defined our function, we assumed that our variables would be called x and y, so any other choice of variables gets interpreted as a constant.  One way to fix this would be to explicitly specify your independent variables in the definition:

deriv2By2b[expression_,var1_,var2_]:={(∂_var1) expression,(∂_var2) expression}//Transpose//MatrixForm

deriv2By2b[g[u,v],u,v]

deriv2By2b[{u Cos[v],u Sin[v]},u,v]

This now works, though at the cost of a bit of redundancy in calling the function.  (There are fancier tricks we could use to generalize this, but this is a good starting point.)  

Actually, there is a built-in extension of the standard D (derivative) function in Mathematica that will take care of this particular problem much more easily:

D[g[{x,y}],{{x,y}}]//MatrixForm

D[{u Cos[v],u Sin[v]},{{u,v}}]//MatrixForm

Be sure you use the double curly brackets {{}} for the "with respect to" variables; if you only used a single pair, it would try to take a second derivative, once with respect to the first and the second time with respect to the other variable.  

If you need to access one of the components of a vector, you can use the notation:

g[x,y][[1]]

g[x,y][[2]]

Define your own functions to compute the gradient (call it myGrad), divergence (call it myDiv), and curl (myCurl) of an arbitrary function of 3 variables.  (You may not use any of the built-in functions that handle these.)  Demonstrate that each of these actually work for a few different functions, using different independent variable names.

ParametricPlot3D with spherical and cylindrical coordinates

Mathematica has special built-in functions for graphing cylindrical and polar coordinates (SphericalPlot3D).  Unfortunately, these aren't really as versatile as we might wish.  For example, they wouldn't be capable of plotting something as simple as ϕ=(π/6).  These commands are based on a much more flexible command, ParametricPlot3D.

ParametricPlot3D requires a function from either ℝ->(ℝ^3) or (ℝ^2)->(ℝ^3) as an input.  It will then graph the range of the function.  Thus:

curve[t_]:={t,Sin[t],Cos[2t]}

ParametricPlot3D[curve[t],{t,0,3π}]

If you like, you can use ParametricPlot3D to draw a surface:

surface[u_,v_]:={u Sin[v],u Cos[v],(u^2)}

ParametricPlot3D[surface[u,v],{u,-1,1},{v,0,2π}]

You can even use ParametricPlot3D to graph a function of 2 variables:

f[x_,y_]:=(x^2)+(y^2)

ParametricPlot3D[{x,y,f[x,y]},{x,-1,1},{y,-1,1}]

(If you had used the Plot3D function instead, it would have looked a little nicer...)

These two graphs look suspiciously alike.  Show that they are really the same surface.  Why do they look so different?

In order to use ParametricPlot3D to graph things in spherical and cylindrical coordinates, you must first develop a "change of variable" function. For example, a change of variable function for polar coordinates might look something like:

polarToRect[{r_,θ_}]:={r Cos[θ],r Sin[θ]}

rectToPolar[{x_,y_}]:={(Sqrt[(x^2)+(y^2)]),ArcTan[(y/x)]}

polarToRect[{2,(π/6)}]

rectToPolar[{(Sqrt[3]),1}]

rectToPolar[polarToRect[{2,(π/6)}]]

Warning:

rectToPolar[{-(Sqrt[3]),1}]

What is wrong with this and why?

Fortunately, we are mainly interested in polarToRect (actually the equivalents for cylindrical and spherical coordinates).

Create 2 new functions: cylinderToRect (accepts points in the form {r,θ,z}) and sphereToRect (accepts points in the form {ρ,θ,ϕ}) to make the appropriate conversions.  Notice that both of these functions are from (ℝ^3)->(ℝ^3).

You can use these new functions in conjunction with ParametricPlot3D to graph most equations in cylindrical and spherical coordinates. (You can even use it to graph cylindrical or spherical "parametric equations," something you don't see in many textbooks...)

For example, to graph something like ρ=Sin[θ]Cos[ϕ], you would do something like:

ρ=Sin[θ]Cos[ϕ]

ParametricPlot3D[sphereToRect[{ρ,θ,ϕ}],{θ,0,2π},{ϕ,0,2π}]

(Notice that you can only have 2 independent variables inside ParametricPlot3D, θ and ϕ in this case.)

Of course, the built-in SphericalPlot3D function could have done this (with less effort). However, you can also use your new functions to plot θ=(π/4):

ParametricPlot3D[sphereToRect[{ρ,(π/4),ϕ}],{ρ,0,5},{ϕ,0,π}]

Sometimes, it is important to impose limitations on your domain or range.  If they are simple limitiations, you can just take care of it in the ParametricPlot domain indications.  (In the previous example, we know that ρ≥0 and 0≤ϕ≤π, so we assigned those domains.)  However, sometimes finding the appropriate region to graph is sometimes more complicated.  Let's say you want to plot the graph of ϕ=ρ +sin(θ).  Well, it is certainly possible to compute a domain that will work for this, but it can get a bit tricky.  Mathematica has a built-in method to handle this, called RegionFunction.  You want to ensure that 0 ≤ ϕ ≤ π, so that means 0≤ρ +sin(θ)≤π.  You can graph this (using your sphereToRect function) as follows:

ParametricPlot3D[sphereToRect[{ρ,θ,ρ +Sin[θ]}],{ρ,0,5},{θ,0,2π}]

However, that doesn't satisfy the restrictions on ϕ in spherical coordinates.  To have Mathematica take that into account for you, try:

ParametricPlot3D[sphereToRect[{ρ,θ,ρ +Sin[θ]}],{ρ,0,5},{θ,0,2π},RegionFunction->Function[{θ,ρ},0≤ρ +Sin[θ]≤π]]

This looks weirder.  The reason is that I am using such a large domain for ρ (I get some disconnected sections).

ParametricPlot3D[sphereToRect[{ρ,θ,ρ +Sin[θ]}],{ρ,0,π},{θ,0,2π},RegionFunction->Function[{θ,ρ},0≤ρ +Sin[θ]≤π]]

Of course, it really depends on what you are doing whether this sort of restriction is necessary.

Use the functions you developed above to graph the following over domains appropriate to the coordinate system:

1. r=z cos(2θ)

2. ϕ=ρ sin((θ/2))

3. (ρ^4)(sin^2)(θ)=(ϕ^3)