# Analytical Plotting with Symbolic Math Toolbox

The `fplot` family accepts symbolic expressions and equations as inputs enabling easy analytical plotting without explicitly generating numerical data.

This example features the following functions

• `fplot`

• `fplot3`

• `fsurf`

• `fcontour`

• `fmesh`

• `fimplicit`

• `fimplicit3`

### Interactively plot functions of one variable

```syms x fplot(sin(exp(x)))```

`fplot([sin(x),cos(x),tan(x)])`

### Generate an implicit plot of a symbolic expression

```syms x y r = 1:10; fimplicit(x^2+y^2 == r)```

### Parametrically explore multiple functions with `subs`

```syms x a expression = sin(exp(x/a))```
`expression = $\mathrm{sin}\left({\mathrm{e}}^{x/a}\right)$`
```fplot(subs(expression,a,[1,2,4])) hold off legend show```

### Mix Symbolic and Numeric techniques to develop mathematical models

Explore the spline approximation to $f\left(x\right)=x*exp\left(-x\right)*sin\left(5*x\right)-2$

```syms f(x) f(x) = x*exp(-x)*sin(5*x) -2; xs = 0:1/3:3; ys = double(subs(f,xs)); fplot(f,[0,3]) hold on plot(xs,ys,'*k','DisplayName','Data Points') fplot(@(x) spline(xs,ys,x),[0 3],'DisplayName','Spline interpolant') hold off grid on legend show```

### Explore Gibbs Phenomenon

```syms x n = 5; approx = cumsum(sin((1:2:2*n-1)*x)./(1:2:2*n-1)); fplot(approx,'LineWidth',1)```

### Plotting the results of computations

With symbolic input, we can perform computations and plot the results.

```syms f(a,x) assume(a>0); f(a,x) = a*x^2+a^2*x+2*sqrt(a)```
`f(a, x) = $a {x}^{2}+{a}^{2} x+2 \sqrt{a}$`
`x_min = solve(diff(f,x), x)`
```x_min =  $-\frac{a}{2}$```
```fplot(f(a,x_min),[0 5]) xlabel 'a' title 'Minimum value of f depending on a'```

`assume(a,'clear')`

### Visualize Series and Summations

```syms x t6 = taylor(cos(x),x,'Order',6)```
```t6 =  $\frac{{x}^{4}}{24}-\frac{{x}^{2}}{2}+1$```
`t8 = taylor(cos(x),x,'Order',8)`
```t8 =  $-\frac{{x}^{6}}{720}+\frac{{x}^{4}}{24}-\frac{{x}^{2}}{2}+1$```
```fplot([cos(x) t6 t8]) xlim([-4 4]) ylim([-1.5, 1.5]) title '6th and 8th Order Taylor Series approximations to cos(x)' legend show```

### Explore functions along with their integrals and derivatives

Some symbolic expressions cannot be converted to MATLAB functions.

```syms x f = x^x```
`f = ${x}^{x}$`
`int(f,x)`
```ans =  $\int {x}^{x}\mathrm{d}x$```
`diff(f,x)`
`ans = $x {x}^{x-1}+{x}^{x} \mathrm{log}\left(x\right)$`
```fplot([f, int(f,x), diff(f,x)],[0 2]) legend show```

### Generate parametric curves without explicit numerical data

Curves $\left(x\left(t\right),y\left(t\right)\right)$or $\left(x\left(t\right),y\left(t\right),z\left(t\right)\right)$ can be drawn by `fplot` or `fplot3` (just like with `plot` or `plot3` for numerical data) :

```syms t fplot3(sin(t)-t/2,cos(t),t^3,'--','LineWidth',2.5) view([-45 45])```

### Generate surfaces $z=f\left(x,y\right)$ without meshgrid

```syms x y fsurf(sin(x)+sin(y)-(x^2+y^2)/20,'ShowContours','on') set(camlight,'Color',[0.5 0.5 1]); set(camlight('left'),'Color', [1 0.6 0.6]); set(camlight('left'),'Color', [1 0.6 0.6]); set(camlight('right'),'Color', [0.8 0.8 0.6]); material shiny view(-19,56)```

### Generate numeric streamlines from analytical derivatives using `meshgrid`

```syms x y u = diff(diff(sin(x^2+y^2)))```
`u = $2 \mathrm{cos}\left({x}^{2}+{y}^{2}\right)-4 {x}^{2} \mathrm{sin}\left({x}^{2}+{y}^{2}\right)$`
`v = diff(diff(cos(x^2+y^2)))`
`v = $-2 \mathrm{sin}\left({x}^{2}+{y}^{2}\right)-4 {x}^{2} \mathrm{cos}\left({x}^{2}+{y}^{2}\right)$`
```[X, Y] = meshgrid(-3:.1:3,-2:.1:2); U = subs(u, [x y], {X,Y}); V = subs(v, [x y], {X,Y}); startx = -3:0.1:3; starty = zeros(size(startx)); h = streamline(X,Y,U,V,X,Y); for i=1:length(h)-1 h(i).Color = [rand() rand() rand()]; end```

Like `fplot`, `fsurf` evaluates your symbolic expression more densely where needed, to more accurately show curved areas and asymptotic regions.

`fsurf(log(x) + exp(y), [-2 2])`

### Create Implicit surfaces

Plot the implicit surface $1/{x}^{2}-1/{y}^{2}+1/{z}^{2}=0$. Specify an output to make fimplicit3 return the plot object.

```syms x y z f = 1/x^2 - 1/y^2 + 1/z^2; fimplicit3(f)```

### Visualize multivariate surfaces

Unlike pure symbolic functions (like `int`, `diff`, `solve`), `fsurf` does not allow specifying the order of variables. To set the order, use symbolic functions:

```syms f(t) x(u,v) y(u,v) z(u,v) f(t) = sin(t)*exp(-t^2/3)+1.5; x(u,v) = u```
`x(u, v) = $u$`
`y(u,v) = f(u)*sin(v)`
```y(u, v) =  $\mathrm{sin}\left(v\right) \left({\mathrm{e}}^{-\frac{{u}^{2}}{3}} \mathrm{sin}\left(u\right)+\frac{3}{2}\right)$```
`z(u,v) = f(u)*cos(v)`
```z(u, v) =  $\mathrm{cos}\left(v\right) \left({\mathrm{e}}^{-\frac{{u}^{2}}{3}} \mathrm{sin}\left(u\right)+\frac{3}{2}\right)$```
`fsurf(x,y,z,[-5 5.1 0 2*pi])`

### Use `fmesh` for 3D mesh plots

Plot the parameterized mesh

`$\begin{array}{l}x=r*cos\left(s\right)*sin\left(t\right)\\ y=r*sin\left(s\right)*sin\left(t\right)\\ z=r*cos\left(t\right)\end{array}$`

where $r=8+sin\left(7*s+5*t\right)$

```syms s t r = 8 + sin(7*s + 5*t); x = r*cos(s)*sin(t); y = r*sin(s)*sin(t); z = r*cos(t); fmesh(x, y, z, [0 2*pi 0 pi], 'Linewidth', 2) axis equal```

### Generate a contour plot of a symbolic expression or equation

```syms x y g(x,y) g(x,y) = x^3-4*x-y^2```
`g(x, y) = ${x}^{3}-4 x-{y}^{2}$`
```fcontour(g,[-3 3 -4 4],'LevelList',-6:6) title 'Some Elliptic Curves'```

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos