You are on page 1of 41

I.

Gi i thi u
y Tnh ton hnh th c. y Symbolic Math Toolbox.

Khai bo

y Khai bo bi n: y syms a b c x ho c y a = sym(a) y b = sym(b) y c = sym(c) y x = sym(x) y Khai bo bi n ph c y x = sym(x,real); y = sym(y,real) ho c syms x y real y z = x + i*y

Khai bo bi u th c: y Khai bo bi u th c: f = 2*x + b


y syms x b y f = 2*x + b

ho c
y f = sym(2*x + b) y sym((sqrt(2) + 1)/3) y g = syms(5) (khc g = 5) y syms x y y h = x^2 + y^2

y L nh findsym: tm bi n hnh th c trong bi u th c. y V d


y syms a b n t x z y s = x^n; g = sin(a*t + b) y findsym(f) y ans = x n y findsym(g) y ans = a b t

y findsym(g,1): tm bi n hnh th c m c y findsym(g,1) y ans = t

nh

Hi n th bi n hnh th c d

i d ng s h c

y t = 0.1 y sym(t, f ) y ans = '1.999999999999a'*2^(-4) y sym(t, r ) y ans = 1/10 y sym(t, e ) y ans = 1/10+eps/40 y sym(t, d ) y ans = .10000000000000000555111512312578 y digits(7) y sym(t, d ) y ans = .1000000

II. Cc php tnh vi tch phn


o hm y Tch phn y Gi i h n y Chu i s
y

o hm
y diff(Y)

Y: hm s ho c bi n hnh th c c n l y
y V d y syms x; f = sin(5*x) y diff(f) y ans = 5*cos(5*x) y g = exp(x)*cos(x) y diff(g) y ans = exp(x)*cos(x) exp(x)*sin(x) y c = sym(5); diff(c) y ans = 0

o hm.

y diff(5) y ans = [ ] v 5 khng ph i l bi n hnh th c

yL y

o hm c p 2

y diff(g,2)

ho c y diff(diff(g)) y ans = -2exp(x)*sin(x)

y
 

o hm a bi n G i f = f(x,y) th
o hm theo x: diff(f,x) o hm theo y: diff(f,y)

o hm c p 2 theo x: diff(f,x,2)  o hm c p 2 theo y: diff(f,y,2)  N u x l bi n m c nh c a f th diff(f,2) t diff(f,x,2).




ng

ng v i

o V d  syms s t  f = sin(s*t)  diff(f,t) => ans = cos(s*t)*s  diff(f,s) => ans = cos(s*t)*t  diff(f,t,2) => ans = -sin(s*t)*s^2  findsym(f,1)=> ans = t Suy ra bi n m c nh l t do diff(f,2) = diff(f,t,2)

o hm
 syms a x

i v i ma tr n

 A = [cos(a*x) sin(a*x); -sin(a*x) cos(a*x)]  A=

[cos(a*x), sin(a*x)] [-sin(a*x), cos(a*x)]


 diff(A)  ans =

[-sin(a*x)*a, cos(a*x)*a] [-cos(a*x)*a, -sin(a*x)*a]

Tch phn
y int(f,x) ho c int(f) : Tm nguyn hm c a hm f = f(x). y int(f,a,b) : Tnh tch phn c a f t a -> b. y V d
y syms x n a b t yf=x^n y int(f) ( ho c inf(f,x)) y ans = x^(n+1)/(n+1)

y g = cos(a*t + b) y int(g) y ans = sin(a*t + b)/a y h = sin(2*x) y int(h,0,pi/2) y ans = 1 y u = exp(-x^2) y int(u,0,inf) y ans = 1/2*pi^(1/2)

y Tch phn b i: I = y syms x y; y f = x^2 + y^2;

sinx

(x 2 + y 2 )dydx

0 0

y int(int(f,y,0,sin(x)),0,pi); y ans =

pi^2 32/9

Gi i h n
y limit(f) : y limit(f,x,a) :

lim f ( x)
x p0

lim ( x) xpa

ho c limit(f,a)
y limit(f,x,a,left) : y limit(f,x,a,right) :

lim f ( x )
x a

lim f ( x)
x a

y V d y sym h n x y limit((cos(x + h) cos(x))/h,h,0) y ans = - sin(x) y limit((1 + x/n)^n,n,inf) y ans = exp(x) y limit(x/abs(x),x,0,left) y ans = -1 y limit(x/abs(x),x,0,right) y ans = 1 y limit(x/abs(x),x,0) y ans = NaN

Chu i s
y Tnh:

1 1 s1 ! 2 !1 2 2 n !1 n
g

1 2 3
2

...

s2 !
k !0

!1  

 ...

y syms x k y s1 = symsum(1/k^2,1,inf) y s2 = symsum(x^k,k,0,inf) y s1 = 1/6*pi^2 y s2 = -1/(x-1)

y Khai tri n Taylor: y syms x; taylor(sin(x),x,10); ans = x-1/6*x^3+1/120*x^5-1/5040*x^7+1/362880*x^9 y taylor(exp(x),4,2) ans = exp(2)+exp(2)*(x-2)+1/2*exp(2)*(x-2)^2+1/6*exp(2)*(x-2)^3 y taylor(exp(1/x^2),6,inf) ans = 1+1/x^2+1/2/x^4

III. Cc hm n gi n v thay th bi n trong bi u th c


y collect(f) - f = f(x) y collect(f,y) - f = f(x,y,)

n gi n hm f b ng cc nhm cc bi n x c cng s m . Tr ng h p f c nhi u bi n collect(f,y) s ch nh gom nhm theo bi n y. collect(f) gom nhm theo bi n m c nh c ch ra trong findsym(f).

y V d y syms x t
y f = x^3 6*x^2 + 11*x 6 y g = (x 1)*(x 2)*(x 3) y h = -6 + (11 + (-6 + x)*x)*x y pretty(f), pretty(g), pretty(h) y collect(f) => ans = x^3 6*x^2 + 11*x 6 y collect(g) => ans = x^3 6*x^2 + 11*x 6 y collect(h) => ans = x^3 6*x^2 + 11*x 6 y f = (1 + x)*t + x*t y collect(f) => ans = 2*x*t + t y collect(f,t) => ans = 2*x*t + t

y expand(f) : phn tch bi u th c f. y V d


y syms x y a b y f = a*(x + y) y expand(f) => ans = a*x + a*y y g = (x -1)*(x -2)*(x 3) y expand(g) => ans = x^3 6*x^2 + 11*x 6 y h = exp(a + b) y expand(h) => ans = exp(a)*exp(b) y cos(3*x) => ans = 4*cos(x)^3 3*cos(x)

y factor(f) : phn tch a th c f thnh nhn t chung y V d


y f = x^3 6*x^2 + 11*x 6 y g = x^3 6*x^2 + 11*x 5 y h = x^6 + 1 y factor(f) y ans = (x 1)*(x -2)*(x 3) y factor(g) y ans = x^3 6*x^2 + 11*x 5 y factor(h) y ans = (x^2 + 1)*(x^4 x^2 + 1)

y simplify(f): y V d

n gi n bi u th c f.

y f = x*(x*(x 6) + 11) - 6 y simplify(f) => ans = x^3 6*x^2 + 11*x 6 y g = (1 x^2)/(1 x) y simplify(g) => ans = x + 1 y syms x y positive y simplify(log(x*y)) => log(x) + log(y) y h = cos(x)^2 + sin(x)^2 y simplify(h) => ans = 1

y simple(f): rt g n bi u th c f, k t h p cc php ton c a simplify, collect, factor. y V d


y f = (1/a^3 + 6/a^2 + 12/a + 8)^1/3 y simplify(f) => ans = ((2*a + 1)^3/a^3)^1/3 y simple(f) => ans = (2*a + 1)/a y syms x y positive y h = log(x*y) y simplify(h) => ans = log(x) + log(y) y simple(h) => ans = log(x*y)

y subs(expr,old,new): thay th old b ng new trong bi u th c expr. y V d


y syms x y y f = sin(x) y subs(f,x,pi/3) => ans = 0.8660 y subs(f,x,sym(pi)/3) => ans = 1/2*3^1/2 y S = x^y y subs(S,{x y},{3 2}) y subs(S,{x y},{3 x+1}) y subs(S,y,1:5) => ans = [ x, x^2, x^3, x^4, x^5]

y [N D] = numden(f): trch t s v m u s c a f gn cho N v D. y V d


y syms s y H = -(1/6)/(s + 3) -(1/2)/(s + 1) + (2/3)/s y simplify(H) y pretty(ans) y [N D] = numden(H) yN=s+2 y D = (s+3)*(s+1)*s

y poly2sym(a,x): t o m t a th c theo bi n x v i cc h s c l y l n l t t m ng a. y V d
y syms x; a = [1 4 -7 -10] y p = poly2sym(a,x) y p = x^3 + 4*x^2 7*x - 10

y x = sym2poly(p): trch cc h s c a a th c p ch a vo m ng s. y V d
y syms x; p = 4*s^2 2*s^2 + 5*s 16 y x = sym2poly(p) y x = 4 -2 5 -16

III. Tnh ton trong tnh


y Khai bo ma tr n

i s tuy n

y syms a b c d t y A =[a b; c d] y B = [cos(t) sin(t); -sin(t) cos(t)] y C = [t 1 0;1 t 1; 0 1 t] y d = round(rand(3,3)) y D = sym(D)

y Cc php ton: v i 2 ma tr n A v B

A+B AB  A*B  A\B ( = A*inv(B) )  A/B ( = inv(A)*B )  A^n  A.

y Cc hm x l ma tr n:  inv(A)

 det(A)  rank(A)  diag(A)  tril(A)  triu(A)  poly  rref  eig

y V d
y c = floor(10*rand(4)) y D = sym(c) y A = inv(D) y inv(A)*A y det(A) y b = ones(1,4) y x = b/A y x*A y A^3

y C th dng cc hm rt g n v l y trn ma tr n. y V d y syms a b s y K = [a+b, a-b;b-a, a+b] y G = [cos(s) sin(s);-sin(s) cos(s)] y L = K^2 y collect(L)
y factor(L) y diff(L,a) y int(K,a) y J = K/G y simplify(J*G) y simplify(G*(G.))

o hm, tch phn

IV. Gi i ph
y solve(f) : gi i ph y V d

ng trnh
ng trinh f(x) = 0.

is

y syms a b c x y f = a*x^2 + b*x + c; y solve(f) y ans =

[1/2*a(-b + (b^2 4*a*c)^1/2)] [1/2*a(-b - (b^2 4*a*c)^1/2)]

y solve(f) : gi i ph

ng trnh theo bi n m c c ch

nh

c ng

ch ra trong hm findsym(f), x. solve(f,a): gi i theo bi n t cho b,c).


y V d
y solve(f,b) y ans = -(a*x^2 + c)/x

y findsym(f) -> ans = nh l a (t

y solve(f-g): gi i ph

ng trnh f(x) = g(x). Ho c solve(f(x)

= g(x)).

y V d y s = solve(`cos(2*x) + sin(x) = 1`) y s= [ 0] [ pi] [ 1/6*pi] [ 5/6*pi] y solve(f(x),g(x),h(x),): gi i h nhi u ph

ng trnh.

y V d

Gi i h :

2 2 y =0 x x-y / 2=

y syms x y alpha y [x y] = solve(x^2*y^2=0,x y/2 = alpha)

x= [ 0] [ 0] [ alpha] [ alpha] Nghi m: v = [x, y]

y= [ -2*alpha] [ -2*alpha] [ 0] [ 0]

u 2 + v 2 = a2 y Gi i h : + v = 1 u 2 - 2a = 3 a y S = solve(u^2+v^2=a^2,u+v=1,a^22*a=3)
yS=

a: [2x1 sym] u: [2x1 sym] v: [2x1 sym] y S.a


ans = [ 3] [ -1]

Gi i ph
y V d y Gi i:

ng trnh vi phn
dy ! 1  y 2 , y (0) ! 1 dt

y Hm: dsolve

y dsolve(Dy=1+y^2,y(0)=1) y y = tan(t + 1/4*pi)

y Gi i:

d2y d ! cos(2 x)  y, y(0)=1, y (0) ! 0 2 dx dx

y y =dsolve(D2y=cos(2*x) y,y(0)=1,Dy(0)=0,x) y simplify(y); ans = 4/3*cos(x) 2/3*cos(x)^2+1/3

y Gi i:

d 3u 3 !u dx (0) ! 1; u '(0) ! 1; u ''(0) ! T u

y dsolve(D3u=u,u(0)=1,Du(0)=-1,D2u(0)=pi),x)

df ! 3 f (t )  4 g (t ) , f (0) ! 0 dt y Gi i: dg ! 4 f (t )  3 g (t ) , g (0) ! 1 dt
y [f g] = dsolve(Df = 3*f + 4*g,Dg = -4*f + 3*g,

f(0) = 0,g(0) = 1)
y f = exp(3*t)*sin(4*t); g = exp(3*t)*cost(4*t)

V. V
y Trong 2 D:  Hm ezplot(f).  V d
y syms t x y y f = sin(2*x)

th

y g = t + 3*sin(t) y h = 2*x/(x^2 -1) y ezplot(f); ezplot(g); ezplot(h) y ezplot(x*exp(-x), [-1 4])

y Trong 3 D y Hm ezplot3(x,y,z) y V d
y syms x y z t y x = 3*t/(1 + t^3) y y = 3*t^2/(1 + t^3) y z = sin(t) y ezplot3(x,y,z)

y ezcontour / ezcontourf y ezmesh / ezmeshc y ezsurf / ezsurfc

You might also like