You are on page 1of 18

Process Simulation Lab

(An Undergraduate Lab Course)

Lecture 6
Introduction to MATLAB

Course Instructor: Dr. Shabih Ul Hasan & Dr. Parul Katiyar


Motilal Nehru National Institute of Technology Allahabad
Prayagraj - 211004

1
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

SOLVING MULTI
VARIABLE NON-
LINEAR EQUATIONS
USING MATLAB
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Topics to be covered

 Multi-Variable equations
 Linear equations in multiple variable
 Non linear equations in multiple variables - fsolve
 Other numerical methods – Modified Newton Raphson method
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Solving a System of Multiple Variable Equations


• An equation with multiple variable can be written in the form

𝑓 𝑥1, 𝑥2, x3, x4, … … . . xn = 0


• It can be linear or non-linear.

• To get the solution of multi-variable, one must have enough number of equations i.e., equal to the number of variables.
These equations combined are known as system of equations.

• It is easier to get the solution if the equations are linear, one can simply get it by using analytical methods.

• In matlab, we solve system of linear eqauations by simply defining the coefficient matrices. An example is provided in the
next slide.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Solving a System of Multiple Variable Equations –


Linear Equations
• Suppose we have the given system of linear algebraic equations

• To solve this system of equations, one has to define the equations in the form;
AX = B
• Where, A is a matrix of coefficients of x, y and z and B is a column vector of scalars on the right side of the
equations. X is a vector of variables x, y and z.
• One can get the solution of equations by finding the inverse of the coefficient matrix A and multiplying it with vector B.
X = A-1B
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Solving a System of Multiple Variable Equations –


Linear Equations
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Practice Problem

• Solve the following system of linear equations


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Solving a System of Multiple Variable non-Linear


Equations
• Solving non-linear equations analytically is not always possible and the best way to approach non-linear equations is by numerical
methods.

• An example of this type of system of equations is Lorenz system of algebraic equations.

𝑥 −𝑦 =0
2𝑥 − 𝑥𝑧 − 𝑦 = 0
𝑥𝑦 − 3𝑧 = 0

• There are various numerical methods developed for solving system of non-linear equations. Some of these are listed below:
 Fixed Point Iteration
 Modified Newton Raphson Method
 Conjugate Gradient Method
 Broyden’s Method

• MATLAB has an inbuilt solver fsolve to deal with a system of non-linear equations in multiple variables.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Solving a System of Equations with Multiple Variables


- FSOLVE
• The inbuilt solver can be used by using the inbuilt function fsolve

• SYNTAX
z = fsolve(function, x0)
z = fsolve(function, x0, options)

• Its syntax is almost similar to the syntax of fzero function, but it takes inputs of different type.

• fzero takes single non-linear equation in one variable and single guess value and provides single solution which is nearest
to the guess value.
• In case of fsolve, the function is a column vector which takes different non linear equations in different rows in the
column vector,
• Similarly, x0 is also a column vector of same length as the length of function vector and takes same no. of initial guesses
as the no. of equations.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Solving a System of Equations with Multiple Variables


- FSOLVE
• The input options can be used to set the format of the solution that is to be returned on the command window.

• Try the following commands on matlab command window


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Example Problem
Providing a system a non-linear equation in a function file
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Example Problem
Solving the problem in the command window
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Newton Raphson Method


• The method starts with a function f defined over the real numbers x, the function's derivative f ′, and an initial
guess x0 for a root of the function f. If the function satisfies the assumptions made in the derivation of the
formula and the initial guess is close, then a better approximation x1 is

𝑓(𝑥0 )
𝑥1 = 𝑥0 −
𝑓′(𝑥0 )
Geometrically, (x1, 0) is the intersection of the x-axis and the tangent of the graph of f at (x0, f (x0)).
𝑓(𝑥𝑛 )
• The process is repeated as 𝑥𝑛+1 = 𝑥𝑛 − until a sufficiently accurate value is reached.
𝑓′(𝑥𝑛 )

• This method has quadratic convergence rate.


• The idea of the method is as follows: one starts with an initial guess which is reasonably close to the true root,
then the function is approximated by its tangent line (which can be computed using the tools of calculus), and
one computes the x-intercept of this tangent line (which is easily done with elementary algebra). This x-intercept
will typically be a better approximation to the function's root than the original guess, and the method can
be iterated.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Modified Newton Raphson Method


• We know from Linear Algebra that we can take systems of equations and express those systems in the form of
matrices and vectors.
−1
𝑥 𝑘 = 𝑥 𝑘 −1 − 𝐽 𝑥 𝑘 −1
𝐹 𝑥 𝑘 −1

where k = 1, 2, ………. n represents the iteration, x ∈ ℝ𝑛 , F is a vector function, and J(𝑥)−1 is the inverse of the
Jacobian matrix.

• This equation represents the procedure of Newton’s method for solving nonlinear algebraic systems. However,
instead of solving the equation f(x) = 0, we are now solving the system F(x) = 0.

• Jacobian Matrix is defined as,


𝛿𝑓1 𝛿𝑓1 𝛿𝑓1
𝛿𝑥 𝛿𝑦 𝛿𝑧
𝛿𝑓2 𝛿𝑓2 𝛿𝑓2
𝐽 𝑥, 𝑦 =
𝛿𝑥 𝛿𝑦 𝛿𝑧
𝛿𝑓3 𝛿𝑓3 𝛿𝑓3
𝛿𝑥 𝛿𝑦 𝛿𝑧
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Steps…
Step – 1: Let 𝑥 (0) = (𝑥 0
1, 𝑥
0
2, 𝑥 0
3, … … … … … … … … 𝑥
0
𝑛) be the given initial vector.

0 0
Step – 2: Calculate 𝐽 𝑥 𝑎𝑛𝑑 𝐹 𝑥 .

Step – 3: Once 𝑦 (0) is found, we can now proceed to finish the first iteration by solving for 𝑥 (1) .

𝑥 (1) = 𝑥 (0) − 𝑖𝑛𝑣 𝐽 𝑥 0


×𝐹 𝑥 0

Step – 5: Once we have calculated 𝑥 (1) , we repeat the process again, until 𝑥 (𝑘) converges to x.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Example Problem
Providing a system a non-linear equation in a function file
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Example Problem
Program of modified Newton-Raphson method
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Practice Problem
• Solve the following nonlinear system
𝟏
𝟑𝒙𝟏 − 𝒄𝒐𝒔 𝒙𝟐 𝒙𝟑 − =𝟎
𝟐

𝒙𝟐𝟏 − 𝟖𝟏 𝒙𝟐 + 𝟎. 𝟏 𝟐
+ 𝒔𝒊𝒏 𝒙𝟑 + 𝟏. 𝟎𝟔 = 𝟎

𝟏𝟎𝝅 − 𝟑
𝒆−𝒙𝟏 𝒙𝟐 + 𝟐𝟎𝒙𝟑 + =𝟎
𝟑

With initial guess as 0.1, 0.1, -0.1

You might also like