```
using CalculusWithJulia
using Plots
plotly()
using SymPy
using ForwardDiff
using LinearAlgebra
```

# 58 Functions \(R^n \rightarrow R^m\)

This section uses these add-on packages:

For a scalar function \(f: R^n \rightarrow R\), the gradient of \(f\), \(\nabla{f}\), is a function from \(R^n \rightarrow R^n\). Specializing to \(n=2\), a function that for each point, \((x,y)\), assigns a vector \(\vec{v}\). This is an example of vector field. More generally, we could have a function \(f: R^n \rightarrow R^m\), of which we have discussed many already:

Mapping | Name | Visualize with | Notation |
---|---|---|---|

\(f: R\rightarrow R\) | univariate | familiar graph of function | \(f\) |

\(f: R\rightarrow R^m\) | vector-valued | space curve when n=2 or 3 | \(\vec{r}\), \(\vec{N}\) |

\(f: R^n\rightarrow R\) | scalar | a surface when n=2 | \(f\) |

\(F: R^n\rightarrow R^n\) | vector field | a vector field when n=2 | \(F\) |

\(F: R^n\rightarrow R^m\) | multivariable | n=2,m=3 describes a surface | \(F\), \(\Phi\) |

After an example where the use of a multivariable function is of necessity, we discuss differentiation in general for a multivariable functions.

## 58.1 Vector fields

We have seen that the gradient of a scalar function, \(f:R^2 \rightarrow R\), takes a point in \(R^2\) and associates a vector in \(R^2\). As such \(\nabla{f}:R^2 \rightarrow R^2\) is a vector field. A vector field can be visualized by sampling a region and representing the field at those points. The details, as previously mentioned, are in the `vectorfieldplot`

function of `CalculusWithJulia`

.

```
F(u,v) = [-v, u]
vectorfieldplot(F, xlim=(-5,5), ylim=(-5,5), nx=10, ny=10)
```

The optional arguments `nx=10`

and `ny=10`

determine the number of points on the grid that a vector will be plotted. These vectors are scaled to not overlap.

Vector field plots are useful for visualizing velocity fields, where a velocity vector is associated to each point; or streamlines, curves whose tangents are follow the velocity vector of a flow. Vector fields are used in physics to model the electric field and the magnetic field. These are used to describe forces on objects within the field.

The three dimensional vector field is one way to illustrate a vector field, but there is an alternate using field lines. Like Euler’s method, imagine starting at some point, \(\vec{r}\) in \(R^3\). The field at that point is a vector indicating a direction of motion. Follow that vector for some infinitesimal amount, \(d\vec{r}\). From here repeat. The field curve would satisfy \(\vec{r}'(t) = F(\vec{r}(t))\). Field curves only show direction, to indicate magnitude at a point, the convention is to use denser lines when the field is stronger.

Vector fields are also useful for other purposes, such as transformations, examples of which are a rotation or the conversion from polar to rectangular coordinates.

For transformations, a useful visualization is to plot curves where one variables is fixed. Consider the transformation from polar coordinates to cartesian coordinates \(F(r, \theta) = r \langle\cos(\theta),\sin(\theta)\rangle\). The following plot will show in blue fixed values of \(r\) (circles) and in red fixed values of \(\theta\) (rays).

```
F(r,theta) = r*[cos(theta), sin(theta)]
F(v) = F(v...)
= range(0, 2, length=5)
rs = range(0, pi/2, length=9)
thetas
plot(legend=false, aspect_ratio=:equal)
plot!(unzip(F.(rs, thetas'))..., color=:red)
plot!(unzip(F.(rs', thetas))..., color=:blue)
= [1, pi/4]
pt = ForwardDiff.jacobian(F, pt)
J arrow!(F(pt...), J[:,1], linewidth=5, color=:red)
arrow!(F(pt...), J[:,2], linewidth=5, color=:blue)
```

To the plot, we added the partial derivatives with respect to \(r\) (in red) and with respect to \(\theta\) (in blue). These are found with the soon-to-be discussed Jacobian. From the graph, you can see that these vectors are tangent vectors to the drawn curves.

## 58.2 Parametrically defined surfaces

For a one-dimensional curve we have several descriptions. For example, as the graph of a function \(y=f(x)\); as a parametrically defined curve \(\vec{r}(t) = \langle x(t), y(t)\rangle\); or as a level curve of a scalar function \(f(x,y) = c\).

For two-dimensional surfaces in three dimensions, we have discussed describing these in terms of a function \(z = f(x,y)\) and as level curves of scalar functions: \(c = f(x,y,z)\). They can also be described parametrically.

We pick a familiar case, to make this concrete: the unit sphere in \(R^3\). We have

- It is described by two functions through \(f(x,y) = \pm \sqrt{1 - (x^2 + y^2)}\).
- It is described by \(f(x,y,z) = 1\), where \(f(x,y,z) = x^2 + y^2 + z^2\).
- It can be described in terms of spherical coordinates:

\[ \Phi(\theta, \phi) = \langle \sin(\phi)\cos(\theta), \sin(\phi)\sin(\theta), \cos(\phi) \rangle, \]

with \(\theta\) the *azimuthal* angle and \(\phi\) the polar angle (measured down from the \(z\) axis).

The function \(\Phi\) takes \(R^2\) into \(R^3\), so is a multivariable function.

When a surface is described by a function, \(z=f(x,y)\), then the gradient points (in the \(x-y\) plane) in the direction of greatest increase of \(f\). The vector \(\langle -f_x, -f_y, 1\rangle\) is a normal.

When a surface is described as a level curve, \(f(x,y,z) = c\), then the gradient is *normal* to the surface.

When a surface is described parametrically, there is no “gradient.” The *partial* derivatives are of interest, e.g., \(\partial{F}/\partial{\theta}\) and \(\partial{F}/\partial{\phi}\), vectors defined componentwise. These will be lie in the tangent plane of the surface, as they can be viewed as tangent vectors for parametrically defined curves on the surface. Their cross product will be *normal* to the surface. The magnitude of the cross product, which reflects the angle between the two partial derivatives, will be informative as to the surface area.

### 58.2.1 Plotting parametrized surfaces in `Julia`

Consider the parametrically described surface above. How would it be plotted? Using the `Plots`

package, the process is quite similar to how a surface described by a function is plotted, but the \(z\) values must be computed prior to plotting.

Here we define the parameterization using functions to represent each component:

```
X(theta,phi) = sin(phi) * cos(theta)
Y(theta,phi) = sin(phi) * sin(theta)
Z(theta,phi) = cos(phi)
```

`Z (generic function with 1 method)`

Then:

```
= range(0, stop=pi/2, length=50)
thetas = range(0, stop=pi, length=50)
phis
= [X(theta, phi) for theta in thetas, phi in phis]
xs = [Y(theta, phi) for theta in thetas, phi in phis]
ys = [Z(theta, phi) for theta in thetas, phi in phis]
zs
surface(xs, ys, zs) ## see note
```

PyPlot can be used directly to make these surface plots: `import PyPlot; PyPlot.plot_surface(xs,ys,zs).`

Instead of the comprehension, broadcasting can be used

`surface(X.(thetas, phis'), Y.(thetas, phis'), Z.(thetas, phis'))`

If the parameterization is presented as a function, broadcasting can be used to succinctly plot

```
Phi(theta, phi) = [X(theta, phi), Y(theta, phi), Z(theta, phi)]
surface(unzip(Phi.(thetas, phis'))...)
```