You are on page 1of 53

MATLAB Programming

(2014/01/15)

Symbolic Math Toolbox


(, symbolic expression )
, , , symbolic expression

MATLAB Programming

kmste2@kaist.ac.kr

MATLAB Programming

(polynomial)

MATLAB

= 1 + 2 1 + + + +1 ,
pi

= 1, 2 , , +1

y = polyval( P, X )
P X

= 3 2 2 + 1

evaluate () at = 5

>> p = [3 -2 1];
>> polyval(p,5)
ans =
66
3

MATLAB Programming

MATLAB

= 3 6 3 4 2

x 5, 3, 2, 1 ,
MATLAB 0


P = [3 0 -3 0 0 -2 0]
>> P = [3 0 -3 0 0 -2 0];
>> polyval(P, 0)
ans =
0
>> polyval(P, 1)
ans =
-2

MATLAB Programming

= 3 6 3 4 2

Y = polyval( P, X) , X , ,
Y X
P = [3 0 -3 0 0 -2 0]
>> polyval(P, magic(2))

>> polyval(P, [0 -3 2])

ans =

ans =
0

1950

140

-2
11512

1938
140

MATLAB Programming

Quiz

x = 0:2:6 .

= 3 2 7 2 + 2 + 23

= 4 3 0.53 4
= 3 2 4 2

MATLAB Programming

, ?

= 3 100 3 90 + 5 23 3 3 2

, 1001 P
P = [3 0 0 0 0 0 0 0 0 0 -3 0 0 0 0 0 0 -3 0 -2 0]
!
Symbolic Math Toolbox !!
P = sym2poly( F )

(symbolic polynomial) polynomial coefficient vector

>> syms x
>> f = 3*x^100 - 3*x^90 + 5*x^23 - 3*x^3 - 2*x;
>> P = sym2poly(f)
7

MATLAB Programming

Symbolic Math Toolbox !!


F = poly2sym( P )

polynomial coefficient vector

P = [3 1 -1 0 -5 0]
>> P = [3 1 -1 0 -5 0];
>> poly2sym(P)
ans =
3*x^5 + x^4 - x^3 - 5*x

MATLAB Programming

convolution

C = conv( A, B )
= 4 3 2 2 + 1

F = [4 0 -2 1]

= 3 3 + 5 2 3 2 G = [3 5 -3 -2]
1

-2

4
3

-3

-3

-2

-3

-2

-3

-2

-3

-2

-3

-2

-3

-2

-2

4 x 3 = 12
0 x 3 + 4 x 5 = 20
-2 x 3 + 0 x 5 + 4 x -3 = -18
1 x 3 + -2 x 5 + 0 x -3 + 4 x -2 = -15

>> F = [4 0 -2 1];
>> G = [3 5 -3 -2];
>> conv(F,G)
ans =

1 x 5 + -2 x -3 + 0 x -2 = 11
1 x -3 + -2 x -2 = 1

12

20 -18 -15

11

-2

1 x -2 = -2

MATLAB Programming

Convolution

1. symbolic polynomial

2. symbolic polynomial

3. sym2poly polynomial coefficient vector


>> F = [4 0 -2 1];
>> G = [3 5 -3 -2];
>> fn = expand( poly2sym(F) * poly2sym(G) )
fn =
12*x^6 + 20*x^5 - 18*x^4 - 15*x^3 + 11*x^2 + x 2
>> sym2poly(fn)
ans =
12 20 -18 -15

11

-2
10

MATLAB Programming

= 2 2 + 1

= 3 2 2 2
= + 1

>> A = poly2sym([2 -1 1]);


>> B = poly2sym([1 -2 0 -2]);
>> C = poly2sym([1 1]);
>> D = A*B*C

>> A = [2 -1 1];
>> B = [1 -2 0 -2];
>> C = [1 1];
>> D = conv(conv(A,B),C)

D=
-(x + 1)*(2*x^2 - x + 1)*(- x^3 + 2*x^2 + 2)

D=
>> sym2poly(D)
2

-3

-2

-3

-4

-2
ans =
2 -3

-2

-3

-4

-2
11

MATLAB Programming

g h

= + () () ()

[q, r] = deconv( g, h )
g h , q r (q, r )

= 3 6 2 + 12 8

= 2

>> g = [1 -6 12 -8];
>> h = [1 -2];
>> [q, r] = deconv(g,h)
q=
1

-4

r=
0

12

MATLAB Programming

Quiz

g h symbolic expression
.
= 3 6 2 + 12 8

= 2

symbolic expression
, .
= 3 6 2 + 12 8

= 1

: 1
13

MATLAB Programming

Symbolic

[Q, R] = quorem( F, G )

F G F/G ,
Q, R
= 3 6 2 + 12 8

= 1

>> g = [1 -6 12 -8];
>> h = [1 -1];
>> sg = poly2sym(g); sh = poly2sym(h);
>> [q, r] = quorem(sg, sh)
q=
x^2 - 5*x + 7
r=
-1
%
>> expand(q*sh+r)
ans =
x^3 - 6*x^2 + 12*x - 8

14

MATLAB Programming

sym/coeffs

[c, terms] = coeffs( P )


P
c , terms

>> syms x y
>> z = 3*x^2*y^2 + 5*x*y^3
>> [c, terms] = coeffs(z)
c=
[ 3, 5]
terms =
[ x^2*y^2, x*y^3]

>> syms x y
>> z = 3*x^2*y^2 + 5*x*y^3
>> [c, terms] = coeffs(z, x)

[c, terms] = coeffs( P, X )


P X
c , terms

c=
[ 3*y^2, 5*y^3]
terms =
[ x^2, x]
>> [c, terms] = coeffs(z, y)
c=
[ 5*x, 3*x^2]
terms =
[ y^3, y^2]

15

MATLAB Programming

[N, D] = numden( A )

A
N (numerator), D (denominator)
>> sym x y
>> r = 1 + x^2 / (3 + x^2/5);
r=
x^2/(x^2/5 + 3) + 1
>> [n, d] = numden( r )
n=
6*x^2 + 15
d=
x^2 + 15

>> [n,d] = numden(sym(4/5))


n=
4
d=
5
>> [n,d] = numden(x/y + y/x)
n=
x^2 + y^2
d=
x*y

16

MATLAB Programming

Taylor

F = taylor( A )

A , F
0 Taylor 5

>> syms x y z
>> f = taylor(log(1+x))
f=
x^5/5 - x^4/4 + x^3/3 - x^2/2 + x
>> ezplot( 'log(1+x)' )
>> hold on;
>> h = ezplot( f );
>> set(h, 'color', 'red');

17

MATLAB Programming

Taylor

F = taylor( A, x, a, order, n )

A x = a n
% sin(x) x=0
>> syms x;
>> f = taylor(sin(x), x, 0, 'order', 10)
f=
x^9/362880 - x^7/5040 + x^5/120 - x^3/6 + x
>> ezplot( 'sin(x)' );
>> hold on;
>> h = ezplot( f );
>> set( h, 'color', 'red' );

18

MATLAB Programming

Taylor

F = taylor( A, x, a, order, n )

A x = a n
% sin(x) x=2
>> syms x;
>> f = taylor(sin(x), x, 2, 'order', 10)
f=
x^9/362880 - x^7/5040 + x^5/120 - x^3/6 + x
>> ezplot( 'sin(x)' );
>> hold on;
>> h = ezplot( f );
>> set( h, 'color', 'red' );
>> ylim( [-5 5] )

19

MATLAB Programming

Multivariable Taylor

F = taylor( A, [x1, x2, , xk] , [a1, a2, , ak], order, n )


A (x1, x2, , xk) = (a1, a2, ..., ak) n

% 2 (x,y) = (0, 0)
>> syms x y;
>> f = x + y + 3*exp(-x^2-y^2);
>> ezsurf(f, [-1 2]);
>> g = taylor(f, [x,y], [0,0], 'order', 10);
>> hold on;
>> ezsurf(g, [-1 2]);

20

MATLAB Programming

Parametric Taylor

20

cos , =

20

sin , = ,

Taylor , x(t), y(t), z(t) t

syms x y t
x = exp(-t/20)*cos(t);
y = exp(-t/20)*sin(t);
z = t;

figure(1);
ezplot3(x,y,z, [-10 10]);
p = [x y z];
f = taylor(p, t, 0, 'order', 30);
figure(2);
ezplot3(f(1), f(2), f(3), [-10, 10]);

21

MATLAB Programming

22

MATLAB Programming

Symbolic Equation Solver

solve

solve( eqn1, eqn2, , eqnN)


solve( eqn1, eqn2, , eqnN, var1, var2, , varN)
var

2 + + = 0

>> syms a b c x
>> f = a*x^2+b*x+c;
>> solve(f)
f==0

ans =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)

2 4

2

23

MATLAB Programming

Symbolic Equation Solver

solve

eg) 2 2 4 = 0

solve , symbolic ,
.
>> syms x
>> f = x^2-2*x-4;
>> solve(f==0, x)
ans =
5^(1/2) + 1
1 - 5^(1/2)

>> solve('x^2-2*x-4==0')

ans =
5^(1/2) + 1
1 - 5^(1/2)

24

MATLAB Programming

Symbolic Equation Solver

= () ,

, f(x) = g(x) , symbolic


, f(x) g(x) == 0 .

eg) cos 2 = 1 sin()

>> syms x
>> f = cos(2*x) + sin(x) - 1

>> solve('cos(2*x) = 1 - sin(x)')


ans =
0
pi/6
(5*pi)/6

f=
cos(2*x) + sin(x) - 1
>> solve(f)
ans =
0
pi/6
(5*pi)/6
25

MATLAB Programming

, 2 log = 1 ,


>> syms x y
>> f = 2*x-log(y)-1

>> solve('2*x-log(y)=1', 'x')


f=
2*x - log(y) - 1

ans =

>> solve(f, x)

log(y)/2 + 1/2

ans =
log(y)/2 + 1/2

>> solve('2*x-log(y)=1', 'y')


ans =

>> solve(f, y)
exp(2*x - 1)
ans =
exp(2*x - 1)

26

MATLAB Programming

, 2 = 2 y 2 = 5

1. ,
(y )
= 2 2 , 2 = 2 + 5

2. ,
2 2 = 2 + 5

3. (x )
= 1 2 2

4. 3
= 7 + 4 2, = 7 4 2
27

MATLAB Programming

, 2 = 2 y 2 = 5

>> simplify(subs(y1, xsols))


>> syms x
>> y1 = x^2 - 2;
>> y2 = 2*x + 5;
>> xsols = solve(y1 == y2)

ans =

xsols =

>> simplify(subs(y2, xsols))

2*2^(1/2) + 1
1 - 2*2^(1/2)

ans =

4*2^(1/2) + 7
7 - 4*2^(1/2)

4*2^(1/2) + 7
7 - 4*2^(1/2)

28

MATLAB Programming

solve

2 = 2 y 2 = 5
%

>> sols.x(1)

>> sols.x(2)

>> syms x y
>> sols = solve( x^2 - y == 2, y - 2*x == 5 )

ans =

ans =

sols =

2*2^(1/2) + 1

1 - 2*2^(1/2)

>> sols.y(1)

>> sols.y(2)

ans =

ans =

4*2^(1/2) + 7

7 - 4*2^(1/2)

x: [2x1 sym]
y: [2x1 sym]

29

MATLAB Programming

Quiz

solve .

+ = 0
3 + 2 = 5
2 3 = 6

= 32
= 5 + 1
1 + 22 + 43 34 = 5
21 + 32 3 64 = 2
1 + 52 33 + 24 = 4
1 32 53 = 1

30

MATLAB Programming

Quiz

f(x,y) g(x,y) .

, = 4 2 + 3
2

, = 4 + 2 1

31

MATLAB Programming

Quiz

, x, y
.

= 0 cos()

= 0 sin 12 2

v0 100 m/s , g 9.8 m/s2 ,



.

32

MATLAB Programming

fzero

solve f(x) = sin(x) 0.5x 0

, 2

>> syms x
>> ezplot('sin(x) - 0.5*x')
>> grid on

fzero f (, f(x) = 0)

x0 ,
2 1016 x*

33

MATLAB Programming

fzero

solve f(x) = sin(x) 0.5x 0

, 2
>> solve('sin(x)-0.5*x==0')
ans =
0
>> fzero('sin(x)-0.5*x', [1 3])
ans =
1.8955
>> fzero('sin(x)-0.5*x', [-3 -1])
ans =
-1.8955
>> fzero('sin(x)-0.5*x', [-5 -3])
Error using fzero (line 274)
The function values at the interval endpoints must differ in sign.

34

MATLAB Programming

Quiz

fzero .

x tan(x) = 0
cos(x) x = 0

35

MATLAB Programming

roots

p
z = roots( p )

z p = 0

>> syms x
>> p = sym2poly(x^2 - x - 1)
p=
1

-1

-1

>> z = roots(p)
z=
-0.6180
1.6180

36

MATLAB Programming

polyval

p
v = polyval( p, z )

%
>> syms x
>> p = sym2poly(x^2 - x - 1)
p=
1

-1

-1

>> z = roots(p)
z=
-0.6180
1.6180

%
>> polyval(p, z)
ans =
1.0e-15 *
-0.1110
0.2220

37

MATLAB Programming

38

MATLAB Programming

MATLAB

D,

2
2

D2,

3
3

D3, ,

Dn

= 2 Dy = -2xy

= 2, y 1 = 1

>> dsolve('Dy = -2*x*y', 'y(1) = 1', 'x')


ans =
exp(1)*exp(-x^2)
39

MATLAB Programming

Symbolic Expression

Symbolic Math D

D,

2
2

D2,

3
3

D3, ,

diff(x)

Dn

= 2, y 1 = 1

>> syms x y(t)


>> dsolve(diff(y) == -2*x*y, y(1) == 1, x)
ans =
exp(1)*exp(-x^2)

40

MATLAB Programming

+ 6 + 9 = 0, y 0 = 4, y 0 = 14
y

2
2

D2y, y

Dy

>> Dsol = dsolve('D2y+6*Dy+9*y=0', 'y(0)=-4', 'Dy(0)=14', 'x')


Dsol =
2*x*exp(-3*x) - 4*exp(-3*x)
>> simple(Dsol)
ans =
2*exp(-3*x)*(x - 2)

41

MATLAB Programming

+ 6 + 9 = 0, y 0 = 4, y 0 = 14
y

2
2

D2y, y

Dy

symbolic expression
>> syms x y(t)
>> D2y = diff(diff(y));
>> Dy = diff(y);
>> Dsol = dsolve(D2y+6*Dy+9*y==0, y(0)==-4, Dy(0)==14, x)
Dsol =
2*x*exp(-3*x) - 4*exp(-3*x)

42

MATLAB Programming

(D =

2 3 = 2 2
2
1

,
x
0
=

,
y
0
=
3
3
+ 4 = 3 2

x
Dx, y
Dy

>> DSol2 = dsolve('Dx-2*x-3*y=2*exp(2*t)', '-x+Dy-4*y=3*exp(2*t)', 'x(0)=-2/3', 'y(0)=1/3')


DSol2 =
y: [1x1 sym]
x: [1x1 sym]
>> DSol2.x
ans =
- (3*exp(2*t))/4 - exp(5*t)*((11*exp(-3*t))/12 - 1)

>> DSol2.y
ans =
exp(2*t)/4 - exp(5*t)*((11*exp(-3*t))/12 - 1)

43

MATLAB Programming

(D =

Symbolic Expression

2 3 = 2 2
2
1

,
x
0
=

,
y
0
=
3
3
+ 4 = 3 2

x
Dx, y
Dy

syms x y x(t) y(t)


Dx = diff(x);
Dy = diff(y);
deq1 = Dx-2*x-3*y==2*exp(2*t);
deq2 = -x+Dy-4*y==3*exp(2*t);
DSol2 = dsolve(deq1, deq2, 'x(0)=-2/3', 'y(0)=1/3', t);

44

MATLAB Programming

Quiz

symbolic expression

=
=


+ 2

(
+ 1) = 1 , (0) = 1

12
1 2

=0

45

MATLAB Programming

46

MATLAB Programming

f(t) F(s) .

f(t) F(s) .

, ,

1. f(t) F(s) ( )
2. F(s)
3. F(s) f(t)

Laplace , Z-, Fourier ,

47

MATLAB Programming

Laplace

: =

: =

= 0

+
1

%
>> syms t w
>> laplace(t^2)

%
>> syms s w
>> ilaplace( 1/s^3 )

ans =
2/s^3

ans =
t^2/2

>> laplace(cos(w*t))

>> ilaplace( 3/(s+w) )

ans =
s/(s^2 + w^2)

ans =
3*exp(-t*w)

>> laplace(t^1)

>> ilaplace( s/(s^2+4) )

ans =
1/s^2

ans =
cos(2*t)
48

MATLAB Programming

Laplace

(1/2)

1 2 Laplace

2
2

= 0

= 2 (0)

+ 6 + 9 = 0, y 0 = 4, y 0 = 14
Laplace

+ 6 + 9 = + 6 + 9

= 2 0 + 6 6 0 + 9 = 0
2 + 6 + 9 = 4 + 14

4+14
2 +6+9

49

MATLAB Programming

Laplace

(2/2)
=

4+14
2 +6+9

F(s)
>> syms s
>> Y = (-4*s+14)/(s^2+6*s+9)
Y=
-(4*s - 14)/(s^2 + 6*s + 9)
>> ilaplace(Y)
ans =
26*t*exp(-3*t) - 4*exp(-3*t)
50

MATLAB Programming

Z : =

% Z
>> syms t
>> f = sin(2*t)

=
=0

% Z
>> syms z
>> f = 3*z/(z^2 - 4*z + 5);
>> f = 3*z/(z^2 - 4*z + 5)
f=
(3*z)/(z^2 - 4*z + 5)
>> ft = iztrans(f)

3
2 4 + 5

f=
sin(2*t)

ft =
((-1)^n*(- 2 - i)^(n - 1)*15*i)/4 - ((-1)^n*(- 2 + i)^(n - 1)*15*i)/4 + (3*(-1)^n*5^n*cos(n*(pi - a

>> fz = ztrans(f)

>> pretty(ft)

fz =
(z*sin(2))/(z^2 - 2*cos(2)*z + 1)
>> pretty(fz)
z sin(2)
------------------2
z - 2 cos(2) z + 1

/ /
/ 1/2 \ \ \
n n | |
|25 |||
3 (-1) 5 cos| n | pi - acos| ------ | | |
n
n-1
\ \
\ 5 / / / 15 (-1) (i - 2)
i
------------------------------------------- - ----------------------- +
1/2 n
4
2 (5 )
n
n-1
15 (-1) (- i - 2)
i
------------------------4

51

MATLAB Programming

Fourier

Fourier : =

Fourier : =

% Fourier
>> syms x u
>> f = x*exp(-abs(x))
f=
x*exp(-abs(x))

1

2

= ||

% Fourier
>> syms x
>> f = exp(-abs(x))

= ||

f=
exp(-abs(x))

>> g = fourier(f, u)
>> g = ifourier(f)
g=
-(u*4*i)/(u^2 + 1)^2

g=

>> pretty(g)

1/(pi*(t^2 + 1))

4ui
- --------2 2
(u + 1)

52

MATLAB Programming

Symbolic Math Toolbox

syms

symbolic

ezplot

symbolic plotting

double

symbolic double

diff

int

limit

symsum

solve

fourier

fourier transform

ifourier

inverse fourier transform

laplace

laplace transform

ilaplace

inverse laplace transform

ztrans

z transformation

iztrans

inverse z transformation

collect

expand

factor

hornor

nested form

simple

simplify

pretty

symbolic

53

You might also like