Introduction to symbolic expressions in Matlab (Symbolic Math Toolbox)

You should always start with this:
clearvars; % clear all variables in the work space
The Symbolic Math Toolbox allows to define symbolic expressions in terms of symbolic variables such as t,x,y. All results are exact symbolic results.
If you type
format long g % show 15 digits of numerical results
[ sin(pi), sin(pi/3), sin(pi/7) ]
ans = 1×3
1.22464679914735e-16 0.866025403784439 0.433883739117558
Matlab uses a numerical approximation with 16 digits accuracy for π, and then computes numerical results.
We can define
Pi = sym(pi) % gives symbolic version of pi
Pi = 
π
now
[ sin(Pi), sin(Pi/3), sin(Pi/7) ]
ans = 
gives symbolic results. We can then evaluate this using vpa (uses by default 32 digits):
vpa(ans)
ans = 
Note that we have to use sym(...) for integers to get symbolic results:
[ 1/3, exp(1) ]
ans = 1×2
0.333333333333333 2.71828182845905
[ 1/sym(3) , exp(sym(1)) ]
ans = 

Defining a symbolic expression g depending on x

Example: Define
syms x % define x as symbolic variable
v = x*sin(x);
Note that v is not a function, but just denotes the expression .
Find the derivative
diff(v,x)
ans = 
Find the fifth derivative
diff(v,x,5)
ans = 
Find an antiderivative:
int(v,x)
ans = 
Use int(v,x,a,b) to find the definite integral
int(v,x,0,Pi/3) % we defined Pi = sym(pi)
ans = 
Find , i.e., in v plug in
y = subs(v,x,3)
y = 
Find numerical value using vpa
vpa(y) finds a numerical value. By default this shows 32 digits, you can change this with digits(n)
vpa(y)
ans = 
0.42336002417960166630223440842433

Plot the symbolic expression v using fplot

Plot the function g for :
fplot(v,[0,20])
xlabel('x') % label x-axis
title('function g(x) = x\cdot sin x') % title on top of graph
grid on % show grid

Find a solution x of the nonlinear equation or

In our example there are infinitely many solutions.
solx = solve(v,x)
solx = 
0
This returns 0 which is definitely one solution.
We can try to find more solutions:
[solx,param,conditions] = solve(v,x,'ReturnConditions',true)
solx = 
param = 
k
conditions = 
This shows that 0 and are solutions, where can be arbitrary. (Showing 0 separately is redundant as it is included in with )
This only works in simple equations.
Let us try to solve :
sol = solve(v==5,x)
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
sol = 
This is indeed an approximation of a solution. But we may be interested in the solution near . With vpasolve we can specify an initial guess:
vpasolve(v==5,x,7)
ans = 
7.0688914033950668006875995619168

Using an expression v of two variables

We can also use functions of more than one variable.
Example: Define the function
syms x y % declare symbolic variables x,y
v = x^4+y^4-4*(x^2+y^2)+4;
We can find partial derivatives:
Find
diff(v,x)
ans = 
Find
diff(v,x,x,y)
ans = 
0
Evaluate , i.e. in v plug in
subs(v,[x,y],[1,2])
ans = 
1
Plot the graph of v for , as a surface over the -plane:
fsurf(v,[-2 2 -2 2])
xlabel('x'); ylabel('y'); title('function x^4+y^4-4(x^2+y^2)+4')
Make a contour plot of v for ,
fcontour(v,[-2 2 -2 2])
colorbar % show colorbar on the right which explains colors of contours
axis equal % use the same unit length for x and y
xlabel('x'); ylabel('y'); title('contour plot of x^4+y^4-4(x^2+y^2)+4')
Here Matlab chooses the contour levels automatically. If you want more or fewer contours you can specify the spacing of the contours by using fcontour(v,[x1,x2,y1,y2],'LevelStep',dz):
fcontour(v,[-2 2 -2 2],'LevelStep',0.5)
colorbar; axis equal
If you only want contours for a few specific z-values you can use
fcontour(v,[-2.1 2.1 -2.1 2.1],'LevelList',[0 1])
axis equal;
title('points with v=0 (blue) and G(x,y)=1 (yellow)')
The blue points satisfy , the yellow points satisfy
If you only want to see the points with you can use fcontour(v,[x1,x2,y1,y2],'LevelList',[0]) or fimplicit(v,[x1,x2,y1,y2]):
fimplicit(v,[-2 2 -2 2]);
axis equal; xlabel('x'); ylabel('y'); title('points satisfying x^4+y^4-4(x^2+y^2)+4=0')
NOTE: The plotting commands fplot, fsurf, fcontour, fimplicit work for both Matlab @-functions and symbolic expressions.