KEMBAR78
Solving Nonlinear Equations in MATLAB | PDF | Numerical Analysis | Function (Mathematics)
0% found this document useful (0 votes)
44 views18 pages

Solving Nonlinear Equations in MATLAB

This document discusses solving non-linear equations using MATLAB. It covers topics like linear vs non-linear equations, solving equations with one variable, and numerical methods like bisection, Newton-Raphson. It provides an example of finding the root of x^2 - 2 = 0 using bisection, Newton-Raphson methods. It also discusses the fzero command in MATLAB to find zeros of non-linear functions.

Uploaded by

amit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views18 pages

Solving Nonlinear Equations in MATLAB

This document discusses solving non-linear equations using MATLAB. It covers topics like linear vs non-linear equations, solving equations with one variable, and numerical methods like bisection, Newton-Raphson. It provides an example of finding the root of x^2 - 2 = 0 using bisection, Newton-Raphson methods. It also discusses the fzero command in MATLAB to find zeros of non-linear functions.

Uploaded by

amit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Process Simulation Lab

(An Undergraduate Lab Course)

Lecture 5
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 NON-LINEAR
EQUATIONS USING
MATLAB
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Topics to be covered

 Linear Vs. Non-Linear Equations.


 Solving an Equation in One Variable.
 Solving Nonlinear Equation with One Variable in MATLAB – FZERO
 Other numerical methods
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Linear Vs. Nonlinear Equations


• Linear Equation
𝑦 = 𝑎𝑥 + 𝑏
 Has a single, unique solution.
 Can be solved directly (analytically).

• Nonlinear Equation
𝑦 = sin 𝑥
 May have 0… many solutions.
 Sometimes cannot be solved analytically.
 Usually, numerical solutions are iterative, and require a
starting guess for the solution.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Solving an Equation with One Variable


• An equation with one variable can be written in the form f(x) = 0.
• The solution is the value of x where the function crosses the x-axis
(the value of the function is zero), which means that the function
changes sign at x.
• An exact solution is a value of x for which the value of the function
is exactly zero.
• If such a value does not exist or is difficult to determine, a
numerical solution can be determined by finding a x that is very
close to the point where the function changes its sign (crosses x-
axis). This is done by iterative process where in each iteration the
computer determines a value of x that is closer to the solution. The
iterations stops when the difference in x between two iterations is
smaller than some measure.
• In general, a function can have none, one, several, or infinite
number of solutions.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Other Numerical Methods


• We know that fzero is a command which is used to evaluate the root of non-linear equations with one variable.
• This functions is based on various numerical methods that are used to find the solution of a non linear equation with the
help of iterations.

• Some of the numerical methods are listed below and discussed in the next few slides.
• Bisection Method
• Newton Raphson Method
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Bisection Method
• The bisection method in mathematics is a root-finding
method that repeatedly bisects an interval and then selects a
subinterval in which a root must lie for further processing. It is a
very simple and robust method, but it is also relatively slow.

• Because of this, it is often used to obtain a rough approximation


to a solution which is then used as a starting point for more
rapidly converging methods. The method is also called
the interval halving method, the binary search method, or
the dichotomy method.

• This method converges linearly with rate 1/2.


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

Bisection Method - continued


METHOD
The method is applicable for numerically solving the
equation f(x) = 0 for the real variable x, where f is a continuous
function defined on an interval [a, b] and where f(a) and f(b) have
opposite signs.

• Calculate c, the midpoint of the interval, c = (a + b)/2.

• Calculate the function value at the midpoint, f(c).

• If convergence is satisfactory (that is, c - a is sufficiently


small, or |f(c)| is sufficiently small), return c and stop
iterating.

• Examine the sign of f(c) and replace either (a, f(a)) or


(b, f(b)) with (c, f(c)) so that there is a zero crossing within
the new interval.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Newton Raphson Method


• In numerical analysis, Newton's method (also known as
the Newton–Raphson method), named after Isaac
Newton and Joseph Raphson, is a method for finding
successively better approximations to the roots (or zeroes) of
a real-valued function.
• Newton Raphson method is relatively faster than Bisection
method.
• 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

Newton Raphson Method - continued


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.
• Note: If the initial guess chosen for the newton Raphson
method is maxima or minima, then the method will fail as
the derivative of the function is zero at maxima or
minima.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Example Problem
Find the root for 𝒙𝟐 − 𝟐 = 𝟎 by using last three methods.

• BISECTION METHOD
 It can be predicted that the value of 2 lies between 1 and 2. So, 1 and 2 will be used as initial guesses.
First let’s see the MATLAB Code for the Bisection Method.

 We are sure that √2 is in the initial interval [a,b]. This interval is repeatedly cut in half and always brackets the
answer. The entire process requires 51 steps. Here are the first few and the last few values.

 Bisection is slow. With the termination condition in the above code. But it is completely reliable. If we can find
a starting interval with a change of sign, then bisection cannot fail to reduce that interval to two successive
floating-point numbers that bracket the desired result.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Example Problem
• Script file of bisection method for the given problem

• Script file of bisection method for the given problem


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

Example Problem
• NEWTON RAPHSON METHOD
Here are the results which are obtained by Newton Raphson Method with starting point x = 1.
• Newton’s method takes only five iterations to solve the problem.
• When Newton’s method works as it does for square roots, it is very effective. It is the basis for many powerful
numerical methods. But, as a general-purpose algorithm for finding zeros of functions, it has three serious
drawbacks.
• The function f(x) must be smooth.
• It might not be convenient to compute the derivative f ′(x).
• The starting guess must be close to the final result.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Example Problem
• Script file of newton Raphson method for the given problem

• Script file of newton raphson method for the given problem


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

Solving Nonlinear Equation with One Variable in


MATLAB - FZERO
• Zero of a function of one variable.
• SYNTAX
z = fzero(‘function’, x0)
z = fzero(‘function’, x0, tol)
z = fzero(‘function’, x0, tol, options)

• DESCRIPTION
• fzero(‘function’, x0) finds a zero of function. Function is a string containing the name of a real valued function of single
real variable. The value returned is near a point where function changes sign, or NaN if search fails.
[NOTE : x0 can be a scalar or two element vector. If it is entered as a scalar, it has to be a value of x near the point where
the function crosses the x-axis. If x0 is entered as a vector, the two elements have to be the point on the opposite sides of
the solution such that f(x0(1)) has a different sign of that f(x0(2)).]
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

FZERO continued…
• fzero(‘function’, x0, tol) returns an answer accurate to within a relative error of tolerance.
• fzero(‘function’, x0, tol, options) provides for additional arguments passed to the function.
• NOTE: The fzero command finds zero of a function only where the function crosses the x-axis. The command does not
find a zero at points where the function just touches but does not cross the x-axis.
Arguments
• function : A string containing the name of a file in which a arbitrary function of one variable is defined.
• x0 : Your initial estimate of the x-coordinate of a zero of the function or an interval in which you think a zero is found.
• tol : The relative error tolerance. By default tol is eps.
• Options : Additional arguments passed to the function. Two of the more important options are:
• x = fzero(‘function’, x0, optimset(‘display’, ‘iter’)) displays the output of each iteration during the process of finding the
solution.
• x = fzero(‘function’, x0, optimset('PlotFcns‘, 'optimplotfval')) will make a plot in which the x axis contains the iteration
number and the y axis contains the function’s value after the iteration.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Practice Problem
1. Determine the solution of the equation 𝑥𝑒 −𝑥 = 0.2
2. 𝑥2 − 1 = 0
3. 𝑥 2 − 4𝑥 + 3 = 0
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Problem
Problem : Molar Volume and Compressibility Factor from van der Waals Equation.
Concept Used : Use of the van der Waals equation of state to calculate molar volume and compressibility factor for a
gas.
Problem Statement : The ideal gas law can represent the pressure–volume–temperature (PVT) relationship of gases
only at low (near atmospheric) pressures. For higher pressures, more complex equations of state should be used. The
calculation of the molar volume and the compressibility factor using complex equations of state typically requires a
numerical solution when the pressure and temperature are specified.
The van der Waals equation of state is given by
𝑎 𝑅𝑇 𝑎
𝑃+ 𝑉 − 𝑏 = 𝑅𝑇 or 𝑃= −
𝑉2 𝑉 −𝑏 𝑉2
27 𝑅 2 𝑇𝑐 2 𝑅𝑇𝑐
Where 𝑎 = and 𝑏 =
64 𝑃𝑐 8𝑃𝑐

where P is the pressure (atm), V is the molar volume (L g-1 mol-1), T is the temperature (K), R is the gas constant (R =
0.08206 atm L g-1 mol-1 K-1), Tc is the critical temperature (405.5 K for ammonia), and Pc is the critical pressure (111.3
atm for Ammonia). Reduced pressure is defined as 𝑃𝑟 = 𝑃/𝑃𝑐 and the compressibility factor is given by 𝑍 = 𝑃𝑉/𝑅𝑇.

You might also like