# Symbolic Variables

Matlab has a powerful symbolic math ability. Rather than making calculations on known numbers, we can make calculations on symbolic expressions. For example, what is the limit as x approaches inf of 1 + 1/2^1 + 1/2^2 + 1/2^3...+1/2^n ? Matlab can tell us. What is the integral of x^3 for any x? Matlab can tell us. Symbolic Math in Matlab

Matlab allows you to create symbolic math expressions. This is useful when you don't want to immediately compute an answer, or when you have a math "formula" to work on but don't know how to "process" it. Matlab allows symbolic operations several areas including:

* Calculus

* Linear Algebra

* Algebraic and Differential Equations

* Transforms (Fourier, Laplace, etc)

The key function in Matlab to create a symbolic representation of data is: sym() or syms if you have multiple symbols to make. Below is an example of creating some symbolic fractions and square roots:

>> sqrt(2)

ans =

1.4142

>> sqrt( sym(2) )

ans =

2^(1/2)

>> 2 / 5

ans =

0.4

>> 2/5 + 1/3

ans =

0.7333

>> sym(2) / sym(5)

ans =

2/5

>> sym(2) / sym(5) + sym(1) / sym(3) ans =

11/15

Defining Symbolic Expressions

We can define symbolic functions using the sym command and syms command. Here is an example of creating a symbolic function for (a*X^2) + (b*x) + c:

>> syms a b c x % define symbolic math variables

>> f = sym('a*x^2 + b*x + c');

From now on we can use the f symbol to represent the given function.

Evaluation of Symbolic Expressions

The keyfunction subs (which stands for substitute) is used to replace symbolic variables with either new symbolic variables or with acutal values. The syntax for the function is: subs( symbolic_function, list_of_symbols, list_of_values). Here is an example:

>> f = sym('a*x^2 + b*x + c');

>> subs(f,x,5)

ans =

25 * a + 5 * b + c

>> subs(f,[x a b c],[5 1 2 3])

ans =

38

Plotting Symbolic Function

In Matlab, we can plot a symbolic function over one variable by using the ezplot function. Here is an example:

>> y = sin(x)

y =

sin(x)

>> ezplot(y)

If you want to see something cool, try:

>> f = sin(x);

>> ezsurf(f);

Now try:

>> f = sin(x);

>> g = cos(y);

>> ezsurf(f+g);

Or really cool!

>> ezsurf( 'real(atan(x+i*y))' );

To set the bounding values of the variables, you can use:

>> ezplot(y, [ -5, 10 ]); % from -5 ezsurf(z,[[1 2] [5 7]); % x from 1 to 2, y from 5 to 7

Or plotting a polynomial equation:

>> f = sym('a*x^2 + b*x + c');

>> ezplot(subs(f,[a b c],[1 2 3]));

Integration and Derivation

Matlab can also compute many integrals and derivatives that you might find in Calculus or many advanced engineering courses. The key functions are int for integration and diff for derivation. Differentiation

>> syms x;

f = sin(5*x)

>> f =

sin(5*x)

>>diff(f)

ans =

5*cos(5*x)

2nd Derivative

To take the 2nd (or greater) derivative of an equation, we use:

>> f = x^3

f =

x^3

...

Please join StudyMode to read the full document