Skip to main content

Section 1.7 Symbolic Computations

The focus of this course is on numerical computations, i.e. calculations, usually approximations, with floating point numbers. However, MATLAB can also do symbolic computations, which means exact calculations using symbols as in Algebra or Calculus.
Note: To do symbolic computations in MATLAB one must have the Symbolic Toolbox.

Subsection 1.7.1 Defining functions and basic operations

Before doing any symbolic computation, one must declare the variables used to be symbolic:
>> syms x y
An expression representing a function may be defined by simply typing the formula:
>> f = cos(x) + 3*x^2
Note that coefficients must be multiplied using *. To find specific values, you must use the command subs:
>> subs(f,pi)
This command stands for substitute, it substitutes \(\pi\) for \(x\) in the formula for \(f\text{.}\)If we define another function:
>> g = exp(-y^2)
then we can compose the functions:
>> h = compose(g,f)
i.e. \(h(x) = g(f(x))\text{.}\) We can also form new functions by multiplying:
>> yg = y*g
We can do simple calculus operations, like differentiation:
>> f1 = diff(f)
>> h1 = diff(h)
indefinite integrals (antiderivatives):
>> F = int(f)
and definite integrals:
>> int(f,0,2*pi)
To change a symbolic answer into a numerical answer, use the double command, which stands for double precision (not times 2):
>> double(ans)
Note that some antiderivatives cannot be found in terms of elementary functions; for some of these the antiderivative can be expressed in terms of special functions:
>> G = int(g)
and for others MATLAB does the best it can:
>> int(h)
For definite integrals that cannot be evaluated exactly, MATLAB does nothing and prints a warning:
>> int(h,0,1)
We will see later that even functions that don’t have an antiderivative can be integrated numerically. You can change the last answer to a numerical answer using:
>> double(ans)
Plotting a symbolic function can be done as follows:
>> fplot(f)
or the domain can be specified:
>> fplot(g,[-10,10])
>> fplot(g,[-2,2])
It is important to keep in mind that even though we have defined our variables to be symbolic variables, plotting can only plot a finite set of points. For intance:
>> fplot(cos(x^5))
will produce a plot with some small mistakes, because it does not plot enough points.

Subsection 1.7.2 Functions of Two Variables and Partial Derivatives

Since \(f = \cos(x) + 3x^{2}\) and \(g = \exp(-y^{2})\) are functions of different variables, their product is a function of two variables:
\begin{equation*} k(x,y) = f(x) g(y) = ( \cos(x) + 3 x^{2}) \exp(-y^{2}) \end{equation*}
In MATLAB, set
>> k = f*g
To evaluate, we need to specify which value goes in which variable:
>> subs(k,[x,y],[0,1])
To plot a symbolic function of two variables use:
>> ezsurf(k)
If we want to differentiate a function with more than one variable in MATLAB, we must specify which variable we are differentiating with respect to:
>> k1x = diff(k,x)
Here we have differentiated with respect to \(x\text{.}\) This is called the partial derivative with respect to \(x\) and we use the symbol \(\frac{\partial k}{\partial x}\) to denote it. There is also a partial derivative with respect to \(y\text{,}\) i.e. \(\frac{\partial k}{\partial y}\text{:}\)
>> k1y = diff(k,y)
To calculate a partial derivative by hand you differentiate as normal with respect to one variable, but treat the other variables as if they were constants. For example if \(f(x,y) = x y^{2}\text{,}\) then
\begin{equation*} \frac{\partial f}{ \partial x}= y^{2} \quad \text{ and }\quad \frac{\partial f}{ \partial y}= 2 xy. \end{equation*}

Subsection 1.7.3 Other useful symbolic operations

MATLAB allows you to do simple algebra. For instance:
>> poly = (x - 3)^5
>> polyex = expand(poly)
>> polysi = simplify(polyex)
To find the symbolic solutions of an equation, \(f(x) = 0\text{,}\) use:
>> solve(f)
>> solve(g)
>> solve(polyex)
Another useful property of symbolic functions is that you can substitute numerical vectors for the variables:
>> X = 2:0.1:4;
>> Y = subs(polyex,X);
>> plot(X,Y)

Subsection 1.7.4 New Notation

MATLAB recently expanded it notation for symbolic computations:
>> syms x
>> f(x) = x^2;
>> f(2)
The function must be given as \(f(x)\text{.}\) If given as \(f = x^{2}\) this will not work. Then we have to use
>> subs(f,2)
It works for multivariable functions also:
>> syms x y a b
>> f(x,y) = x + 2*y;
>> f(a,b)

Exercises 1.7.5 Exercises

1.

Starting from mynewton (Program 1.3.1) write a well-commented function program mysymnewton that takes as its input a symbolic function \(f\) and the ordinary variables \(x_{0}\) and \(n\text{.}\) Let the program take the symbolic derivative \(f'\text{.}\) You will need to use the command subs to obtain values of \(f(x)\) and \(f'(x)\text{.}\) Test it on \(f(x)=x^{3}-4\) starting with \(x_{0}=2\text{.}\) Turn in the program and a brief summary of the results.

2.

Find a complicated function in an engineering or science textbook or website. Make a well-commented script program that defines a symbolic version of this function and takes its derivative and indefinite integral symbolically (if possible). Plot the function on the domain that is relevant for the application. In the comments of the script describe what the function is and properly reference where you got it. Turn in your script and the plot.

3.

Calculate all the partial derivative for the functions below. Do them by hand, then check them using the symbolic toolbox.
  1. \(\displaystyle f(x,y) = \sqrt{xy} + x^2 \sin^2{y}\)
  2. \(\displaystyle g(u,v) = \frac{uv}{u+v}\)