Вы находитесь на странице: 1из 17

Symbolic Computations

Using
MATLAB
Define variables as symbolic

The easiest method is to use the ‘syms’


command.
>> syms x y a b c f g

Alternately, the ‘sym’ command can be used, e.g.


>> x=sym('x');
>> y=sym('y');
>> a=sym('a');
>> b=syms(‘b’) etc.
Define variables as symbolic

Defining f = e-axx3bsin(cx)
>> f = exp(-a*x)*x^(3*b)*sin(c*x)
f=
exp(-a*x)*x^(3*b)*sin(c*x)

Defining f = a*sin(xy) + b*cos(yx)


>> f = a*sin(x^y) + b*cos(y^x)
f=
a*sin(x^y)+b*cos(y^x)
Simplifications
Pretty (the expression looks prettier)
>> f = exp(-a*x)*x^(3*b)*sin(c*x);
>> pretty(f)
(3 b)
exp(-a x) x sin(c x)

Collect (collects all the coefficients with the


same power of x)
>> f = (x-1)*(x-2)*(x-3);
>> collect(f)
ans =
x^3-6*x^2+11*x-6
Simplifications
Expand (expands the symbolic expression)
>> f = sin(x+y);
>> expand(f)
ans =
sin(x)*cos(y)+cos(x)*sin(y)

Factor (expresses f as a product of polynomials


of lower degree with rational coefficients)
>> f = x^3-6*x^2+11*x-6
>> factor(f)
ans =
(x-1)*(x-2)*(x-3)
Simplifications

Simplify:
>> f = (1-x^2)/(1-x);
>> simplify(f)
ans =
x+1

>> f = (cos(x)^2-
sin(x)^2)/(cos(x)^2+sin(x)^2);
>> simplify(f)
ans =
2*cos(x)^2-1
Substitution
The following statement substitutes value1,
value2 etc. in var1, var2, etc. in the ‘expression’

subs (expression,{var1,var2 ...},{value 1, value 2,


..})

Example: Determine the value of


f = a*sin(x) + b*x^2 at x = 2, given a = 1.5, b = 5
>> subs(f,{x, a, b},{2, 1.5, 5})
ans =
21.3639
Differentiation
diff (f, y, n) finds the nth derivative of ‘f’ w.r.t ‘y’

Example: f = ax3 + ax3 + sin(ax)

df
1) Find diff_f =
dx
>> f = a*x^3 + b*x^2 + sin(a*x);
>> diff_f = diff(f)
diff_f =
3*a*x^2+2*b*x+cos(a*x)*a
Differentiation
d2 f
2) Find diff_f =
dx 2
>> diff_f = diff(f,2)
diff_f =
6*a*x+2*b-sin(a*x)*a^2

df
3) Find diff_f =
da
>> diff_f = diff(f,a)
diff_f =
x^3+cos(a*x)*x
Indefinite Integrals

int (f,v) finds the indefinite intrgral fdv

Example: Find the indefinite integral


int_g = gdx , where g = e-ax + sin(cx)

>> g = exp(-a*x) + sin(c*x);


>> int_g=int(g,x)
int_g =
-1/a*exp(-a*x)-1/c*cos(c*x)
Definite Integrals
b
int (f,v,a,b) finds the definite intrgral a gdv

Example: Find the indefinite integral

Int_f = 2
sin 2x dx
0

>> f = sin(2*x);
>> int (f, 0, pi/2)
ans =
1
Limits

Function limit in symbolic toolbox is used to


find the limit. The following example illustrates
its use:
sin(ax)
Let us find the limit, lim
x 0 x
>> value=limit (sin (a*x) /x, x, 0)
value =
a
Limits (cont.)
There are cases where the
limit depends on the direction
from which it is approached.
Consider the plot of ‘1/x’
versus x generated by
>> ezplot(‘1/x') lim
1
inf lim
1
inf
The left and the right limits x x
x 0 x 0

are computed as follows:


>> value_left = limit (1/x, x, 0, 'left')
value_left =
-inf
>> value_right = limit (1/x, x, 0, 'right')
value_right =
inf
Solution of Symbolic Equations
The following example illustrates the basic steps required to
solve the quadratic equation, ax2 + bx + c = 0
>> syms a b c x % Define variables as symbolic
>> eq = ‘a*x^2 + b*x + c = 0’; % Define the equation
>> [x] = solve (eq, x) % Solve the equation
>> pretty( x(1) )
2 1/2
-b + (b - 4 a c)
1/2 --------------------
a
>> pretty( x(2) )
2 1/2
-b - (b - 4 a c)
1/2 --------------------
a
Solution of Simultaneous Linear Equations
Let us try to solve the following set of simultaneous linear
equations:
2x-3y+4z=5, y+4z+x=10, -2z+3x+4y=0

>> syms x y z % Define the variables and functions


>> eq1='x-y+2*z=5'; eq2='x+y+4*z=1'; eq3='3*x+2*y-z=1';
>> [x, y, z]=solve (eq1, eq2, eq3, x, y, z) % Solve equations
x = 2, y = -7/3, z = 1/3

Alternatively, the equations may be solved as follows:


>> clear, syms x y z
>> eqns = 'x-y+2*z=5, x+y+4*z=1, 3*x+2*y-z=1';
>> [x,y,z]=solve(eqns)
x = 2, y = -7/3, z = 1/3
Differential Equations
The function dsolve computes symbolic solutions to ordinary
differential equations. symbols D2, D3, ... DN, correspond to the
second, third, ..., Nth derivative, respectively.

The following code solves the differential equation,


dy
y xe x y
dx
>> dsolve('Dy + y = x*exp(x)*y', 'x' )
ans = Specifies the
independent variable,
C1*exp(-x+x*exp(x)-exp(x)) the default is ‘t’
constant
of Subjected to the initial condition y(0)=1
integratio
n >> dsolve('Dy + y = x*exp(x)*y', 'y(0)=1', 'x')
ans =
Initial condition
1/exp(-1)*exp(-x+x*exp(x)-exp(x))
Symbolic Summation
You can compute symbolic summations, when they exist, by
using the symsum command.
Example: Let us determine the series sum of the geometric
2
series: S 1 x x x3 xn
Series sum up to ‘n’ terms
>> syms x k n
>> s = symsum (x^k, k, 0, n); pretty (simplify (s))
(n + 1)
x -1
------------
x–1
Series sum up to ‘inf’
>> s = symsum (x^k, k, 0, inf); pretty(s)
1
- -----
x-1

Вам также может понравиться