Monday, June 28, 2010

Symbolic calculation in Matlab

1) Solve equations:
example 1
--------------------------------
syms x % define x is symbol
y=x^3-8
Solve(y,x)
----------------------
example 2
-----------------------
syms x y
S = 2*x + 4 - y; %S is the 'dummy'
solve(S, x)
(ans = -2 + 1/2*y)
--------------------------
example 3
-----------------------
syms a b
f(1) = a + b - 3;
f(2) = a + 2*b - 6;
[A,B] = solve(f(1), f(2)) %solve f(1) = 0, f(2)=0
(A = 0 B = 3)

2. Differentiation and integration:
example
--------------------------------------
syms x
f = x^2 - 3*x + 4;
diff(f) % or diff('x^2 - 3*x + 4'), differentiation
(ans =2*x-3)
int(f) %integration
(ans = x^3/3-3*x^2/2)
int(f,0,1) % definite integral
(ans = 17/6)
3. Solve ODE
example 1
----------------------------------
syms x
dsolve('D2x = -x')
(ans = C1*sin(t)+C2*cos(t) )
-----------------------------------
example 2
-----------------------------------
syms x
f=dsolve('D2x = -x', 'Dx(0) = 2', 'x(0) = 4')
(f = 2*sin(t)+4*cos(t))
subs(f, 't', 5) %calculate f(5)
ezplot(f, [-2:0.1:2]) % plot f(t) vs t



No comments:

Post a Comment