Class 8
Class 8
Class 8
The solve function is used for solving algebraic equations. In its simplest form, the solve function takes the
equation enclosed in quotes as an argument.
syms x
solve(x-5==0)
MATLAB will execute the above statement and return the following result −
ans =
5
syms x y
y = solve(x-5 = =0)
MATLAB will execute the above statement and return the following result −
y =5
You may even not include the right hand side of the equation −
solve(x-5)
MATLAB will execute the above statement and return the following result −
ans =5
If the equation involves multiple symbols, then MATLAB by default assumes that you are solving for x,
however, the solve function has another form −
solve(equation, variable)
For example, let us solve the equation v – u – 3t^2 = 0, for v. In this case, we should write −
syms u v t
solve(v-u-3* t^2==0, v)
MATLAB will execute the above statement and return the following result –
ans = 3* t^2 + u
The solve function can also solve higher order equations. It is often used to solve quadratic equations. The
function returns the roots of the equation in an array.
The following example solves the quadratic equation x2 -7x +12 = 0. Create a script file and type the
following code −
MATLAB will execute the above statement and return the following result −
ans =
3
3
7
• In case of higher order equations, roots are long containing many terms. You can get the numerical value
of such roots by converting them to double. The following example solves the fourth order equation
s = solve(eq);
The solve function can also be used to generate solutions of systems of equations involving
more than one variables. Let us take up a simple example to demonstrate this use.
ans = 22/19
ans = -5/57
ans = x^2 + 4* x – 45
ans = x^4 + x^3 - 43* x^2 + 23* x + 210
ans = 2* cos(x)* sin(x)
ans = cos(x)* cos(y) - sin(x)* sin(y)
ans = x^4 - 7* x^3
ans = x^6 - 8* x^5 + 15* x^4
Factorization and Simplification of Algebraic Expressions
The factor function factorizes an expression and the simplify function simplifies an expression. The
following example demonstrates the concept .
Example:
Create a script file and type the following code −
syms x
syms y
factor(x^3 - y^3)
factor([x^2-y^2,x^3+y^3])
simplify((x^4-16)/(x^2-4))
Differentiation
Start by declaring x for a variable. The command for differentiation is diff. It has the following form
diff(function)
For example,
>> syms x
>> diff(x^3-2*x+5)
ans = 3*x^2-2
• For example, to find the slope of a tangent line to x2+3x-2 at point 2, we need to find the derivative and
to evaluate it at x=2.
You can make a derivative (or integral or an output of some other function operator) into a function
automatically using matlab Function command using the following format.
f = matlabFunction ( command involving an operation on a given function )
f = matlabFunction(diff(x^2+3*x-2))
followed by f(2).
Optimization
Recall the steps needed in order to find minimum or maximum values of a given function (using second
derivative test)
In MATLAB, start with syms x.
1. Finding derivative: diff(function)
2. Finding critical points: solve(copy-paste the answer from step 1)
3. Finding second derivative: diff(function, 2)
4. Evaluating second derivative at critical points: g=@(x) paste the second derivative Then
use g(critical value) to find the value of the second derivative at the critical value.
5. Evaluating function at critical points: f=@(x) function formula followed by f(critical
value)
For example, to find extreme values of x^3 -2x+5, start by finding first derivative:
>> diff(x^3-2*x+5)
ans = 3*x^2-2
%Then find critical point(s):
>> solve(3*x^2-2)
ans = 6^(1/2)/3
-6^(1/2)/3
vpa(ans, 3)
ans = .816, -.816
%Find second derivative
>> diff(x^3-2*x+5, 2)
ans = 6*x
%Evaluate this at critical points.
>> g=@(x) 6*x
>>g(.816)
ans = 4.896
%Positive answer means that the function has minimum at x=.816
>> g(-.816)
ans = -4.896
%Negative answer means that the function has maximum at x=.816
%Finding y-values of maximum and minimum:
>> f=@(x) x^3-2*x+5
>>f(.816)
ans = 3.911 %This is the local minimum value.
>>f(.816)
ans = 6.088 %This is the local maximum value
Integration
We can use Matlab for computing both definite and indefinite integrals using the command int.
For the indefinite integrals, start with syms x followed by the command int(function)
For example, the command
>> int(x^3)
evaluates the integral ∫ 𝑥 3 ⅆ𝑥 and gives us the answer
ans = 1/4*x^4
Matlab can evaluate the definitive integrals of the functions that do not have elementary primitive
functions.
𝑠𝑖𝑛 𝑥
Suppose that we need to find the integral of ∫ 𝑥 ⅆ𝑥 from 1 to 3
MATLAB is primarily a numeric package, it can certainly solve straight forward differential equations
symbolically.
We can solve above equations in MATLAB’s using built-in dsolve(). So, we use
>>y = dsolve(’Dy = y’,’x’)
y = C1*exp(x)
>>eqn1 = ’Dy = y’
eqn1 =Dy = y
>>y = dsolve(eqn1,’x’)
y = C1*exp(x)
To solve an initial value problem, say, equation (1.1) with y(0) = 1, use
>>y = dsolve(eqn1,’y(0)=1’,’x’)
y = exp(x)
or
>>inits = ’y(0)=1’;
>>y = dsolve(eqn1,inits,’x’)
y = exp(x)
>>x = linspace(0,1,20);
>>z = eval(vectorize(y));
>>plot(x,z)
• You may notice a subtle point here, that eval() evaluates strings (character arrays), and y, as we have
defined it, is a symbolic object.
• However, vectorize converts symbolic objects into strings.
• Suppose we want to solve and plot the solution to the second order equation
• y′′(x) + 8y′(x) + 2y(x) = cos(x); y(0) = 0, y′(0) = 1.
y=
1/65*cos(x)+8/65*sin(x)+(-1/130+53/1820*14ˆ(1/2))*exp((-4+14ˆ(1/2))*x) -
1/1820*(53+14ˆ(1/2))*14ˆ(1/2)*exp((4+14ˆ(1/2))*x)
>>z = eval(vectorize(y));
>>plot(x,z)
Systems
• Suppose we want to solve and plot solutions to the system of three ordinary differential equations
First, to find a general solution,
>>[x,y,z]=dsolve(’Dx=x+2*y-z’,’Dy=x+z’,’Dz=4*x-4*y+5*z’)
x = 2*C1*exp(2*t)-2*C1*exp(t)-C2*exp(3*t)+2*C2*exp(2*t)-
1/2*C3*exp(3*t)+1/2*C3*exp(t)
y = 2*C1*exp(t)-C1*exp(2*t)+C2*exp(3*t)-C2*exp(2*t)+1/2*C3*exp(3*t)-
1/2*C3*exp(t)
z = -4*C1*exp(2*t)+4*C1*exp(t)+4*C2*exp(3*t)-4*C2*exp(2*t)-
C3*exp(t)+2*C3*exp(3*t)
To solve an initial value problem, we simply define a set of initial values and add them at the end of our
dsolve() command.
>>inits=’x(0)=1,y(0)=2,z(0)=3’;
>>[x,y,z]=dsolve(’Dx=x+2*y-z’,’Dy=x+z’,’Dz=4*x-4*y+5*z’,inits)
x = 6*exp(2*t)-5/2*exp(t)-5/2*exp(3*t)
y = 5/2*exp(t)-3*exp(2*t)+5/2*exp(3*t)
z =-12*exp(2*t)+5*exp(t)+10*exp(3*t)
MATLAB has a number of tools for numerically solving ordinary differential equations. We will focus on the
main two, the built-in functions ode23 and ode45 , which implement versions of Runge–Kutta 2nd/3rd-
order and Runge–Kutta 4th/5th-order, respectively.
We can solve the same ODE as first defining f(x, y) as an M-file firstode.m.
Calculating the Laplace F(s) transform of a function f(t) is quite simple in Matlab. First you need to specify
that the variable t and s are symbolic ones. This is done with the Command
>> syms t s
Next you define the function f(t). The actual command to calculate the transform is
>> F=laplace(f,t,s)
To make the expression more readable one can use the commands, simplify and pretty.
Here is an example for the function f(t),𝑓(𝑡) = −1.25 + 3.5𝑡𝑒 −2𝑡 + 1.25𝑒 −2𝑡
>> syms t s
>> f=-1.25+3.5*t*exp(-2*t)+1.25*exp(-2*t);
>> F=laplace(f,t,s)
>> simplify(F)
ans = (s - 5)/(s*(s + 2)^2)
>> pretty(ans)
𝑠−5
𝑠(𝑠 + 2)2
Alternatively, one can write the function f(t) directly as part of the laplace command:
>>F2=laplace(-1.25+3.5*t*exp(-2*t)+1.25*exp(-2*t))
F2 =
5/(4*(s + 2)) + 7/(2*(s + 2)^2) - 5/(4*s)
>> simplify(F)
ans =
(s - 5)/(s*(s + 2)^2)
>> pretty(ans)
𝑠−5
𝑠(𝑠 + 2)2
The command one uses now is ilaplace. One also needs to define the symbols t and s.
Lets calculate the inverse of the previous function F(s),
𝑠−5
𝐹(𝑠) =
𝑠(𝑠 + 2)2
>> syms t s
>> F=(s-5)/(s*(s+2)^2);
>> ilaplace(F)
ans = (5*exp(-2*t))/4 + (7*t*exp(-2*t))/2 - 5/4
>> simplify(ans)
ans = (5*exp(-2*t))/4 + (7*t*exp(-2*t))/2 - 5/4
>> pretty(ans)
exp(-2 t) 5 t exp(-2 t) 7 5
-------------- + ---------------- - -
4 2 4
We can also do inverse Laplace transform using partial fraction expansion, and MATLAB can help you with
that.
If you want to find the partial-fraction expansion, the following MATLAB program gives you the coefficients
in the expansion. You write the coefficients of the numerator and the denominator in separate vectors and
MATLAB gives you the coefficients with the corresponding poles in the expansion.
Example:
>> F(s)=(-3/(s+2))+(4/(s+1))-(1/s)+(2/s^2);
>> ilaplace(F)
ans =2*t + 4*exp(-t) - 3*exp(-2*t) - 1
>> simplify(ans)
ans =2*t + 4*exp(-t) - 3*exp(-2*t) - 1
>> pretty(ans)
2 t + 4 exp(-t) - exp(-2 t) 3 - 1
OR
Directly we can use
>> F(s)=(4*s^2+4*s+4)/(s^2*(s^2+3*s+2));
>> ilaplace(F)
ans =2*t + 4*exp(-t) - 3*exp(-2*t) - 1
>> x = optimvar('x','LowerBound',-1,'UpperBound',1.5);
>> y = optimvar('y','LowerBound',-1/2,'UpperBound',1.25);
>> prob = optimproblem('Objective',x + y/3,'ObjectiveSense','max');
>> prob.Constraints.c1 = x + y <= 2;
>> prob.Constraints.c2 = x + y/4 <= 1;
>> prob.Constraints.c3 = x - y <= 2;
>> prob.Constraints.c4 = x/4 + y >= -1;
>> prob.Constraints.c5 = x + y >= 1;
>> prob.Constraints.c6 = -x + y <= 2;
>> prob.Constraints.c7 = x + y/4 == 1/2;
sol =
0.1875
1.25
fval =
-0.60417
Exercises:
(1) Find the approximate root of the following equations . Also plot the graph to check answers.
(a) cosx=sinx (b) 𝑥 + log 𝑥 = 2 (c) 𝑥 3 − 3𝑥 − 5 = 0 (d) sin 𝑥 + 𝑥 = 1
5 4 3 2
(2) Find the real roots of 𝑥 + 𝑥 + 𝑥 + 𝑥 + 𝑥 + 1 = 0
(3) Solve the following system of linear equations
(a) 4𝑥 + 𝑦 − 2𝑧 = 0, 2𝑥 − 3𝑦 + 3𝑧 = 9, −6𝑥 − 2𝑦 + 𝑧 = 0
(b) 𝑥 + 𝑦 = 9, 𝑦 + 𝑧 = 7, 𝑥 − 𝑧 = 2
(c) 2𝑦 + 𝑧 = 3(−𝑥 + 1), 𝑥 − 3𝑦 + 𝑧 = 4, −2(3𝑥 + 2𝑦 + 𝑧) = 1
(4) Find the extreme values of the following functions:
1 5 4𝑥
(a) 𝑓(𝑥) = 3 𝑥 3 − 2 𝑥 2 + 4𝑥 (b) 𝑓(𝑥) = (𝑥 2 − 1)3 (c) 𝑓(𝑥) = (1+𝑥 2 )
(5) Solve the following differential equations also plot the graphs.
(a) 𝑦 ′′ − 3𝑦 ′ + 𝑦 = −6 − 2𝑥 + 2𝑒 2𝑥 + cos 𝑥 , 𝑦(0) = 0, 𝑦 ′ (0) = 1
(b) 𝑡𝑦 (𝑖𝑣) + 2𝑦 ′′′ − 𝑡𝑒 𝑡 𝑦 ′′ + (𝑡 3 − 4𝑡)𝑦 ′ + 𝑡 2 sin 𝑡 𝑦 = 0, 𝑦(0) = 0, 𝑦 ′ (0) = 1, 𝑦 ′′ (0) = 0, 𝑦 ′′′ (0) = 1,
(c) 𝑡 4 𝑦 ′′ − 2𝑡 3 𝑦 ′ − 𝑡 8 𝑦 = 0, 𝑦(0) = 0, 𝑦 ′ (0) = 1
(6) Determine {𝑒 2𝑥 , sin 3𝑥, cos 𝑥} is linearly independent.
𝜋
(7) Find the Wronskian of {3 cos 2𝑥, 3 − 6 sin2 𝑥} at 𝑥 = 0, 𝑥 = 2 .
(8) Solve the following system of Differential equations. Also plot the graph.
(a) 𝑥 ′ (𝑡) = 2𝑥 − 3𝑦, 𝑦 ′ (𝑡) = 3𝑥 + 8𝑦, 𝑥(0) = 2, 𝑦(0) = 3
(b) 𝑥 ′ = 3𝑥 + 4𝑦 − 2𝑧, 𝑦 ′ = 2𝑥 + 𝑦 − 4𝑧, 𝑧 ′ = 𝑥 + 2𝑦, 𝑥(0) = 0, 𝑦(0) = 0, 𝑧(0) = 0
𝜋
(c) 𝑥 ′ = 2𝑥 − 3𝑦, 𝑦 ′ = 𝑥 + 2𝑦, 𝑥 (2 ) = 0, 𝑦(0) = 0
1 1
(9) Solve ∫0 1+𝑥 2 ⅆ𝑥 by taking six equidistance subintervals using simpson’s 1/3rd rule.
(10) solve 𝑦 ′ = 1 + 𝑥𝑦, 𝑦(0) = 2 by Runge-kutta method for 2/3 order and 4/5th order.
(11) Solve the following LPP
(a) min 𝑧 = 5𝑥 + 3𝑦
constraints 𝑥 + 2𝑦 ≤ 14, 3𝑥 − 𝑦 ≥ 0, 𝑥 − 𝑦 ≤ 2
(b) min 𝑧 = 200𝑥 + 300𝑦
constraints 2𝑥 + 3𝑦 ≥ 1200, 𝑥 + 𝑦 ≤ 400, 2𝑥 + 1.5𝑦 ≥ 900, 𝑥 ≥ 0, 𝑦 ≥ 0
(c) min 𝑧 = 40𝑥 + 60𝑦
constraints 2𝑥 + 𝑦 ≥ 70, 𝑥 + 𝑦 ≥ 40, 𝑥 + 3𝑦 ≥ 90, 𝑥 ≥ 0, 𝑦 ≥ 0
(12) Find Inverse Laplace transform of the following by factorization.
12 12+78 105𝑠+840
(a) 𝑉(𝑠) = 𝑠(𝑠2 +8𝑠+16) (b) 𝑉(𝑠) = (𝑠2 +8𝑠+52) (c) 𝑉(𝑠) = (𝑠2 +9.5𝑠+17.5)(𝑠2 +8𝑠+80)