# Tangent Plane and Normal Line of Implicit Surface

Since R2021b

This example shows how to find the tangent plane and the normal line of an implicit surface. This example uses symbolic matrix variables (with the `symmatrix` data type) for compact mathematical notation.

A surface can be defined implicitly, such as the sphere ${x}^{2}+{y}^{2}+{z}^{2}={R}^{2}$. In general, an implicitly defined surface is expressed by the equation $f\left(x,y,z\right)=k$. This example finds the tangent plane and the normal line of a sphere with radius $R=\sqrt{14}$.

Create a symbolic matrix variable $r$ to represent the $⟨x,y,z⟩$ coordinates. Define the spherical function as $f\left(r\right)=r\cdot r$.

```clear; close all; clc syms r [1 3] matrix f = r*r.'```
`f = $r {r}^{\mathrm{T}}$`

The implicit equation $f\left(r\right)=14$ represents a sphere. Convert the equation to the `syms` data type using `symmatrix2sym`. Plot the equation by using the `fimplicit3` function.

`feqn = symmatrix2sym(f == 14)`
`feqn = ${{r}_{1,1}}^{2}+{{r}_{1,2}}^{2}+{{r}_{1,3}}^{2}=14$`
```fimplicit3(feqn) axis equal axis([-6 6 -6 6 -6 6])```

Next, find the tangent plane and normal line at the point ${r}_{0}=⟨{x}_{0},{y}_{0},{z}_{0}⟩$.

Recall that the gradient vector of $f$ is $\nabla f\left(r\right)=⟨{f}_{x}\left(r\right),{f}_{y}\left(r\right),{f}_{z}\left(r\right)⟩$. The equation for the tangent plane at the point ${r}_{0}$ is then given by ${f}_{x}\left({r}_{0}\right)\left(x-{x}_{0}\right)+{f}_{y}\left({r}_{0}\right)\left(y-{y}_{0}\right)+{f}_{z}\left({r}_{0}\right)\left(z-{z}_{0}\right)=0$. In compact mathematical notation, the tangent plane equation can be written as $\nabla f\left({r}_{0}\right)\cdot \left(r-{r}_{0}\right)=0$.

Find the gradient of $f\left(r\right)$ using the `gradient` function. Note that the result is a 3-by-1 symbolic matrix variable.

`fgrad = gradient(f,r)`
`fgrad = $2 {r}^{\mathrm{T}}$`
`size(fgrad)`
```ans = 1×2 3 1 ```

Define the equation for the tangent plane. Use the `subs` function to evaluate the gradient at the point ${r}_{0}=⟨1,-2,3⟩$.

```r0 = [-2,1,3]; fplane = (r-r0)*subs(fgrad,r,r0)```
```fplane =  ```

Plot the point ${r}_{0}$ using `plot3`, and plot the tangent plane using `fimplicit3`.

```hold on plot3(r0(1),r0(2),r0(3),'ro',MarkerSize = 10,MarkerFaceColor = 'r') fimplicit3(symmatrix2sym(fplane == 0))```

The equation for the normal line at the point ${r}_{0}$ is given by $n\left(t\right)=⟨{x}_{0},{y}_{0},{z}_{0}⟩+t\phantom{\rule{0.2777777777777778em}{0ex}}⟨{f}_{x}\left({r}_{0}\right),{f}_{y}\left({r}_{0}\right),{f}_{z}\left({r}_{0}\right)⟩$. In compact mathematical notation, the equation can be written as $n\left(t\right)={r}_{0}+t\phantom{\rule{0.2777777777777778em}{0ex}}\nabla f\left({r}_{0}\right)$.

Define the equation for the normal line.

```syms t n = r0 + t*subs(fgrad,r,r0).'```
```n =  ```

Convert the normal line equation to the `syms` data type using `symmatrix2sym`. Extract the parametric curves $x\left(t\right)$, $y\left(t\right)$, and $z\left(t\right)$ for the normal line by indexing into `n`. Plot the normal line using `fplot3`.

`n = symmatrix2sym(n)`
`n = $\left(\begin{array}{ccc}-4 t-2& 2 t+1& 6 t+3\end{array}\right)$`
`fplot3(n(1),n(2),n(3),[0 1],'r->')`