BG2802 Solution of Roots of Equations (Computing) : School of Chemical and Biomedical Engineering
BG2802 Solution of Roots of Equations (Computing) : School of Chemical and Biomedical Engineering
BG2802
Location: N1.3-B4-02
Name: _______________________________________
Group: _______________________________________
GRADE: _____________
1. INTRODUCTION
The aim of this practical is to develop programs using MATLAB for finding numerical
approximations for the roots of non-linear equation. Specifically, Newton-Raphson
method will be implemented.
2. OBJECTIVES
3. BACKGROUND
The Newton-Raphson method is one of the most useful algorithms that relies on the
continuity of f’(x) and f’’(x). The Newton-Raphson method approximates a root of f(x)
= 0 using an initial approximation p0 and using the iteration formula given:
𝑓(𝑝𝑘−1 )
𝑝𝑘 = 𝑝𝑘−1 − for k = 1, 2, …
𝑓 ′ (𝑝𝑘−1 )
An equation relating p1 and p0 can be derived by using two versions for the slope of the
tangent line L:
0 − 𝑓(𝑝0 )
𝑚= (1)
𝑝1 − 𝑝0
Which is the slope of the line through (p1, 0) and (p0, f(p0) ), and
1
𝑚 = 𝑓 ′ (𝑝0 ) (2)
By equating the values of the slope m in equations (1) and (2) and solving for p1, the
equation relating p1 and p0 would be:
𝑓(𝑝0 )
𝑝1 = 𝑝0 − (3)
𝑓 ′ (𝑝0 )
The process described can be repeated to obtain a sequence {pk} that converges to p.
The steps to apply Newton-Raphson method to find the root of an equation f(x) = 0 are
as follow:
1. Derive f ‘(x)
2. Use an initial guess of the root, pk-1, to estimate the new value of the root pk as
𝑓(𝑝𝑘−1 )
𝑝𝑘 = 𝑝𝑘−1 − for k = 1, 2, …
𝑓(𝑝𝑘 )
𝑝𝑘 − 𝑝𝑘−1
| ∈𝑎 | = | |
𝑝𝑘
2
4. Compare the absolute relative approximate error, | ∈𝑎 | with the pre-defined
relative error tolerance, ∈𝑠 . If |∈𝑎 | > ∈, or the number of iterations has not
exceeded the maximum number of iterations, repeat step 2, else stop the
algorithm.
3. Root Jumping: For function f(p) that oscillates and has a number of roots, an
initial estimate may have been chosen to be close to a root. However, the
estimate may jump and converge to some other root.
Enzymes are proteins which perform as catalysts and generally act on substrates with
high specificity. The consumption of a substrate source can be described by the kinetic
depletion of the substrate in terms of the enzyme activity. The kinetics of enzyme
reactions can be formulated in terms of the Michaelis-Menten model, given as:
𝑑𝑠 𝑉𝑚𝑎𝑥 𝑠
𝑟𝑠 = − =
𝑑𝑡 𝐾𝑚 + 𝑠
Where
rs is the reaction rate of the substrate in moles/volume/time;
s is the substrate concentration in moles/volume;
Vmax is the maximal substrate consumption rate;
Km is the concentration of the substrate that elicits half-maximal consumption
rate.
The analytical non-linear solution for the substrate concentration as a function of time
is given as:
𝑠0
𝐾𝑚 log ( ) + (𝑠0 − 𝑠) = 𝑉𝑚𝑎𝑥 𝑡
𝑠
3
Where
4. EQUIPMENT
Personal computer
MATLAB software (Ver. 2019)
5. EXERCISE
1. First, create a new folder “Roots of Equations” in your network directory. This
folder will be your working directory where you will save all the programs written
in this practical. Now specify the MATLAB working directory as your new folder,
as illustrated in Figure 2. Remember to save all the files in this folder.
Go to Home tab and select New Script icon or New icon followed by Script. A
new window called untitled will open. In Editor Tab, select Save and a dialog
4
box will open with a default entry untitled.m. Replace that with Newton1.m and
click Save as you did before. Save the file into the working directory. (Be sure
to save your work when you have finished typing.)
3. Enter the following code into Newton1.m. Remember to save your work along the
way:
% func is the object function
% dfunc is the derivative of func
% p0 is the initial approximation to a zero of f
% maxIteration is the maximum number of iterations
% tolerance is the desirable tolerance
% func and dfunc are defined as M-file functions
clear all;
maxIteration = 5;
p0 = 0.05;
tolerance = 0.0001;
for k = 1 : maxIteration
p1 = p0 – func1(p0)/dfunc1(p0); %Q1:what is the function?
fprintf(' k=%3.0f, p0 =%12.5e, p1 = %12.5e, ', k, p0, p1);
%Q2:what is the function?
absrelerr = abs(p1 - p0)/abs(p1);
p0=p1;
fprintf(' Absolute relative approximate error = %12.5e \n',
absrelerr);
if(absrelerr < tolerance), break, end
end
fprintf('End\n\n');
fplot(@(x) x.^3 - 0.165*x^.2 + 0.0003993, [-0.1 0.2]); grid on
title(['Newton 1 by STUDENT NAME']);
You may have noticed that Newton1.m file calls two functions, (i.e. func1 and
dfunc1). func1 function defines the f(x) equations that need to be solved and dfunc1
function define the derivative of f(x). We will create the two functions in the next
section by working on an exercise.
A. EXERCISE ONE
𝑥 3 − 0.165𝑥 2 + 3.993𝑥10−4 = 0
Since
5
Show that the derivative of the above equation is
𝑓 ′ (𝑥) = 3𝑥 2 − 0.33𝑥
We will create the two .m files that contain the equations that we have derived above.
2. Create a new .m file and save as func1.m. Enter the following code.
function y = func1(x);
3. Now create another new .m file and save as dfunc1.m. This file contains the
derivative of the function f(x). Enter the following code.
function y = dfunc1(x);
y = 3*x^2 - 0.33*x;
4. Run Newton1.m file by typing the following command in the command window in
Matlab, as illustrated in Figure 2.
>> Newton1
5. Answer the questions Q1 and Q2 inside the code. Write your answers as comments
in the code. Note down the values of x at iteration 1, 2 and 3; check your results
with the TA to make sure that it is right before proceeding to the Exercise Two.
6
B. EXERCISE TWO
1
𝑓 ′ (𝑠) = −𝐾𝑚 ( ) − 1
𝑠
Using the following values for the variables
Km = 0.5 mM;
Vmax = 5.0 mM/min;
s0 = 1.0 mM;
Find the substrate concentration when time t = 0, 5, 30 minute using the Newton-
Raphson method. You may like to use the initial estimate of 0.95, 0.80, 0.20 at t =
0, 5, 30 minute respectively.
Time t Substrate
0 Concentration, s
5
30
2. Hint:
𝑠0
𝑓(𝑠) = 𝐾𝑚 log ( ) + (𝑠0 − 𝑠) − 𝑉𝑚𝑎𝑥 𝑡
𝑠
3. Create a new .m file and save as func2.m and enter the following code:
4. Create another .m file and save as dfunc2.m and enter the following code:
function y = dfunc2(s, Km);
y = -Km*(1/s) - 1;
5. Create a new .m file and save as Newton2.m and enter the following code. The code
is similar to Newton1.m which you have created in Exercise One. The only
difference is now this code is calling func2 and dfunc2 instead of func1 and dfunc1.
Replace YOUR_INITAL_ESTIMATE with the initial estimate value at each time
t that you are running.
7
clear all;
maxIteration = 10;
p0 = YOUR_INITAL_ESTIMATE;
tolerance = 0.0001;
t = 0; %Time
so = 1.0; %initial substrate concentration
vmax = 0.05; %maximal substrate consumption rate
Km = 0.5; %Concentration of the substrate that
elicit half-maximal consumption rate
for k = 1 : maxIteration
p1 = p0 - func2(p0, t, Km, so, vmax)/dfunc2(p0, Km);
fprintf('k=%3.0f, p0 =%12.5e, p1 = %12.5e, ', k, p0, p1);
absrelerr = abs(p1 - p0)/abs(p1);
p0=p1;
fprintf(' Absolute relative approximate error = %12.5e \n',
absrelerr);
if(absrelerr < tolerance), break, end
end
fprintf('End\n\n');
fplot(@(x) (0.5*log(1./x)+(1-x))./0.05, [0 1]); grid on
title(['Newton 2 by STUDENT NAME']);
REFERENCES