Solving Algebraic Equations and
Other Symbolic Tools
Solving Basic Algebraic Equations ax + y = z
solve('a*x + y=z',‘x') x = -(y - z)/a
solve(equation, variable)
solve('a*x + y=z',‘a') a = -(y - z)/x
solve('a*x + y=z',‘y') y = z - a*x
Solving Quadratic Equations
𝐚 ∗ 𝐱𝟐 + b*x + z = 0 𝐱𝟐 - 6*x - 12 = 0 s = solve('x^2+ 6*x - 12 = 0')
s (1)= 3 + 21^(0.5) s (2)= 3 - 21^(0.5)
x1 = double (s(1)) = -7.5826 x1 = double (s(2)) = 1.5826
Plotting Symbolic Equations
𝐱𝟐 + 6*x - 12 = 0 d = 'x^2 +6*x – 12';
ezplot(d)
-6 -4 -2 0 2 4 6
-20 -10 0 10 20 30 40 50 60 70
x x2+ 6 x - 12
Plotting Symbolic Equations
d = 'x^2 –6*x – 12';
ezplot(d,[-2,8])
ezplot(f, [x1 , x2 ])
-2 -1 0 1 2 3 4 5 6 7 8
-20 -15 -10 -5 0 5
x x2- 6 x - 12
Solving Higher Order Equations
(x+1)^2- (x-2) = 0 x^4–5*x^3+4*x^2–5*x+6 d= ‘x^4–5*x^3+4*x^2–5*x+6’
s=solve(d)
x1 = double (s(1)) = 4.2588 x2 = double (s(2)) = 1.1164
x3 = double (s(3)) = -0.1876 + 1.1076i x4 = double (s(4)) = -0.1876 - 1.1076i
-10 -8 -6 -4 -2 0 2 4 6 8 10
0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
x
x4-5 x3+4 x2-5 x+6
ezplot(d,[-10,10])
Systems of Equations
aX + bY = Z cX − dY = H
solve(equation1, equation2)
Example
x = s.x = 0.76 y = s.y = 0.21
s = solve('5*x + 4*y = 3','x – 6*y = 2'); Or s = solve('d1', 'd2');
5*x + 4*y = 3 x – 6*y = 2
Expanding and Collecting Equations
(x -1)* (x +4)
expand(sin(x+y)) ans = x^2 +3*x – 4
cos(x+y) syms x
expand((x–1)*(x+4)) Expanding
syms x syms y
ans = cos(x)*cos(y)–sin(x)*sin(y) (x + 2) (x − 3) = x2− x − 6
ans =(x–1)*(x+4)
factor(x^2 +3*x – 4) factor(cos(x)*cos(y)–sin(x)*sin(y))) ans = sin(x+y)
Expanding and Collecting Equations
x*(x^2− 2) = x^3− 2*x
simplify (x^3− 2*x)) ans = x^3− 2*x
syms x
collect (x*(x^2− 2)) Collecting
ans = x*(x^2− 2
Differential Equations
We can compute symbolic derivatives using MATLAB with a call to the diff command. Simply pass the function you want to differentiate to diff as this example shows:
syms x t f = x^2;
g = sin(10*t);
diff(f) ans = 2*x
diff(g) ans = 10*cos(10*t)
Higher derivatives
The second derivative of (t^3)
diff(f) syms t
f = t^3
f = 3*t^2 diff(f,2) f = 6*t Example
Plot the function f (x) = x4 –2x3 and show any local minima and maxima.
Solving ODE’s
𝐝𝐲
𝐝𝐱 = 𝟐 ∗ 𝐲 + 𝐜𝐨𝐬 𝐱
‘Dy = 2*y + cos(x)’
Example
𝒚′′ + 𝒚′ = 𝟓 ∗ 𝐬𝐢𝐧𝟕𝐱 'D2y + 2Dy = 5*sin(7*x)' dsolve(‘eqn’,‘cond1’, ‘cond2’,…)
eq1 ='Dy=a*y+cos(x)';
s=dsolve(eq1)
Ans [ s= -(cos(x) - C2*exp(a*t))/a]
C2 = 2; a = 4;
f = subs(s)
Ans [ f = exp(4*t)/2 - cos(x)/4]
𝐝𝐲
𝐝𝐱 = 𝐚 ∗ 𝐲 + 𝐜𝐨𝐬 𝐱 𝐂 = 𝟐 , 𝐚 = 𝟒
Solving ODE’s
Example
s =dsolve ('Dy = y*t/(t–5)','y(0) = 2') Ans [ s= -(2*exp(t + 5*log(t - 5)))/3125]
𝐝𝐲
𝐝𝐭 = 𝒕
𝒕 − 𝟓𝐲(𝐭) 𝐲(𝟎) = 2
Example; Second and higher order equations 𝒅𝟐𝐲
𝒅𝒕𝟐 − 𝐲 = 𝟎 𝐲(𝟎) = -1 𝒚′(𝟎) = 2 s =dsolve('D2y – y = 0','y(0) = –1','Dy(0) = 2') Ans [ s= 1/2*exp(t) –3/2*exp(–t)]
Solving ODE’s
s = dsolve('DX = Y','DY = –X','X(0)= –1','Y(0)=2');
s.x
Ans [ –cos(t)+2*sin(t)]
s.Y
Ans [sin(t)+2*cos(t)]
𝐝𝐱
𝐝𝐭 = 𝐲 , 𝐝𝐲
𝐝𝐭 = −𝐱 𝐲(𝟎) = 2 , x(0) = -1
Systems of Equations
Numerical Solution of ODEs
Integration 𝑥
𝑛dx =
1(𝑛+1)
𝑥
𝑛+1int('x^n')
ans = x^(n+1)/(n+1) Example
What is the integral of f(x) = b^x ? Evaluate the resulting expression for b = 2, x = 4.
syms b x f = b^x;
F = int(f)
F = 1/log(b)*b^x subs(F,{b,x},{2,4}
ans = 23.0831
Integration
3𝑦 ∗ sec (𝑥)
int('3*y^2*sec(x))
int('3*y^2*sec(x)',y) ans = y^3*sec(x) int('3*y^2*sec(x)',x)
ans = 3*y^2*log(sec(x)+tan(x))
dx dy
Not acceptable
Definite Integration
𝐟 𝐱 𝐝𝐱 = 𝐟 𝐚 − 𝐟(𝐛)
𝐚
𝐛
𝐱 𝐝𝐱 = 𝟏
𝟐 𝒙𝟐 𝟑
𝟐 = 𝟏
𝟐 ∗
𝟑
𝟐 𝟗 − 𝟏
𝟐 ∗ 𝟒 = 𝟓
𝟐
int('x',2,3) ans = 5/2
Integration
Example
What is the area under the curve f(x) = x^2cosx for −6 ≤ x ≤ 6?
syms x
f = x^2*cos(x);
s = int(f,–6,6)
s = 68*sin(6)+24*cos(6) double(s)
ans = 4.0438
Multidimensional Integration 𝑥𝑦
2𝑧
5dx dy dz
syms x y z
int (int (int(x*y^2*z^5,x),y),z) ans = 1/36*x^2*y^3*z^6
syms y x f = x^2*y;
F = int(int(f,x,2,4),y,1,2) F = 28
𝒙𝟏𝟐 𝟐𝟒 𝟐y dx dy
The Laplace Transform
syms t
laplace(t^5) ans =120/s^6
The Laplace transform of a function of time f(t) is given by
ℓ 𝐭
𝐧=
𝐬𝐧+𝟏𝐧!The Inverse Laplace Transform
syms s
ilaplace(120/s^6) ans =t^5
Solving Differential Equations
The Laplace transform simplifies differential equations, turning them into algebraic ones. The Laplace transform of the first derivative of a function is:
syms s
ilaplace(120/s^6) ans =t^5
And the Laplace transform of the second derivative of a function is: