Learning Unit 4
Learning Unit 4
equations
4.1 Introduction
Having covered some equation-solving principles in your previous mathematics courses, you are
probably good at solving linear equations, 𝑎𝑥 + 𝑏 = 0, and quadratic equations, 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0.
You may also be familiar with the formulas for the roots of cubic equations and you are probably
capable of performing some trigonometric equations like 𝑠𝑖𝑛𝑥 + 12𝑠𝑖𝑛𝑥 = 0. But what happens
when you encounter an equation that cannot be solved using a pen and paper, that is, analytically?
In such a case, rather than finding an exact solution, you should use a numerical method to
determine an approximate solution. Numerical methods will allow you to solve any algebraic
equation.
In this learning unit, we will look at methods for solving non-linear equations, focusing on the
creation of loops for iterative processes; the use of built-in functions of Octave to determine the
roots of an equation; computational time and accuracy; and the secant, bisection and Newton’s
methods.
Up to this point, we have considered arithmetic in Octave and how to compute answers for simple
equations. But is it possible to perform a repetitive task for a process? Loops can be used to define
start and end conditions for repetitive tasks easily. They form an essential part of an algorithm
because they undertake repetitive actions in a fast manner. This is particularly useful when solving
equations numerically, as in section 4.5, where we need to define convergence criteria.
In this section we will look at for loops, while loops and if…elseif…else statements.
Additional notes and examples relating to this section are available at Wikibooks at this URL:
https://en.wikibooks.org/wiki/Octave_Programming_Tutorial/Loops_and_conditions.
1
4.3.1 A for loop
A for loop is a construct that allows the iterative execution of code to be implemented repeatedly.
The keyword for declares the start of the loop while end declares the end of the loop. We will
demonstrate the use of a for loop by means of a simple example:
Figure 4.1 shows the execution of the code. The disp statement is repeated six times. Change the
code to
and
Do you notice what happens? The disp statement is repeated three times and then not at all.
For a detailed explanation of for loops, watch the video clip “Octave tutorial 15 – repetitive
execution, part 1 (for loops)” (12:10) by Paul Nissenson at this URL: https://youtu.be/rTxm8ffbz-A.
2
4.3.2 A while loop
A while loop defines a logical condition that allows code to be executed repeatedly until the
condition defined is satisfied. Take the following example: we would like to determine √𝑥 for 𝑥 <
12:
>>x=2;
>>while x<12
disp (sqrt(x));
x = x+2;
end
Octave runs the code by first initialising a variable to x and since we have defined the initial value of
x=2, the code executes from this value. The condition will be satisfied from the initial value of 2 until
10 (2, 4, 6, 8 and 10), as long as x<12. Line 4 of the code defines x to be increased in increments of 2.
Figure 4.2 displays the output from Octave.
You could watch the video clip titled “Octave tutorial 16 – repetitive execution, part 2 (while
loops)” (10:39) by Paul Nissenson for additional examples – go to https://youtu.be/Y256ZMtdwNg.
When writing code, we have different conditional statements for different actions that we want the
code to perform. Examples of conditional statements are as follows:
3
• An if…else statement: this statement executes certain code if one condition is true and other
code if that condition is false.
• An if…elseif…else statement: this statement executes different codes for two different sets
of conditions.
The code below will generate a list of random integers between 0 and 50. Then from all the numbers
larger than 20, we will subtract 20. The script (i.e., the code below) is typed up in Editor and then
saved and executed as a loop. In this manner we can easily edit the script (which we have saved as
ifexample.m) without having to retype the code. Revise section 1.7 of learning unit 1 if you need
to refresh your memory on creating scripts. Revise section 2.6 if you have forgotten how to use the
rand function. Figure 4.3 shows the executed script for ifexample.m.
4
Figure 4.3: Using an if statement
Note that the semicolon in line 4 suppresses the output of all the iterations and, as a result, only the
final result is displayed after subtraction.
Suppose we would like to subtract 20 for all numbers ≥20 and add 20 to all numbers ≤20. We could
use an if…else statement as follows:
>>vec = round(rand(1,10)*50);
>>for k=1:10
if (vec(k)>=20)
vec(k)=vec(k)-20;
else
vec(k)=vec(k)+20;
endif
endfor
vec
5
Note: In Editor you can just add the else statement lines, that is, lines 5 and 6 and do not have to
retype the code. Save the edited file under a new name, ifelseexample.m. Figure 4.4 shows the
executed script.
An if…elseif…else loop can be used. The general syntax of the loop is given by
if (condition)
then body1
elseif (condition)
elseif body2
else
else body3
endif
x=55
if (rem(x,2)==0);
6
disp(“x is even\n”);
elseif (rem(x,5)==0)
disp(“x is odd and divisable by 5\n”);
else
disp(“x is odd\n”);
endif
7
Activity 4.1
1. Create a script that will calculate the square root for the first ten numbers:
For x=1:10
ans=sqrt(x)
end
3. Create a script that returns good morning, good day or good night, depending on the
present time:
time = hour(now);
if (time <= 12)
disp ("have a good morning");
elseif (time <= 17)
disp ("have a good day");
else
disp ("have a good night");
endif
4. Without using Octave, try to explain what sequence of numbers the following for loop will
print on the screen. Check your answer in Octave:
n=12;
for j =1:n
n = n-1;
j
end
Hint: As you may recall from mathematics, factorials are the products of 𝑛 numbers and are
followed by an exclamation mark (!). That is to say:
3! = 1 × 2 × 3 = 6.
Similarly:
𝑛! = 1 × 2 × 3 × 4 × … … … . (𝑛 − 1) × 𝑛
For example, the following code will generate a list of 𝑛 and 𝑛!:
n=5;
factorial=1;
for k=1:n
factorial=k*factorial;
disp([k factorial])
end
8
Try increasing the value of 𝑛 to 100 and 1 000. What is the largest value of 𝑛 for which
Octave can find 𝑛!?
In order to optimise computational performance, the execution time of a particular set of code has
to be determined. Convergence criteria need to be defined and programmed so as to ensure
convergence, within a reasonable time and with reasonable accuracy. Iterative techniques require
that convergence criteria be defined so that the program would know when to terminate the
iterations. Poorly chosen convergence criteria lead to the program giving results that have a large
error. Similarly, if we are too strict with the convergence criteria, the program might run for too
long. At a glance, there are no rules for choosing convergence criteria; instead, the criteria depend
on the specifics of the problem being solved. For example, if you are measuring distance in
kilometres, then you may want the accuracy to be within one metre, whereas if you are measuring
the positioning of a screw in a piece of wood, the tolerance may need to be within a few millimetres.
In mathematical terms, we can define the tolerance limit as
𝑒 = |𝑥 −𝑥 ∗ |
𝑒𝑎 = |𝑥 −𝑥 ∗ |
|𝑥 −𝑥 ∗ |
𝑒𝑟 =
𝑥
If the absolute error is less than the tolerance limit defined, then the solution is acceptable.
The relative error is the magnitude of the absolute error, divided by the magnitude of the exact
value. Multiplying the relative error by 100 gives the per cent error. This will be covered in greater
detail in section 4.5.
In this section, we will look at how to calculate the time taken to run a program, which serves as an
indicator of whether the program is efficient.
The use of tic and toc calculates the runtime of a loop, which would include the time taken while
the CPU was executing some other threads or waiting for an input structure. cputime displays only
the computational time to run a loop, that is to say, the time taken to run the code. Let us look at
some examples that demonstrate the use of the timing codes.
9
100 000
∑ 𝑛
𝑛=1
tic
s=0;
for n = 1:100 000
s=s+n;
end;
toc
Figure 4.6 shows the computational output time as 0.213012 seconds. What is the total time on your
machine?
Now let us compare tic and toc with cputime to generate a surf plot. Figure 4.7 shows the
comparison.
10
Figure 4.7: Comparison of tic and toc with cputime
From figure 4.7 we can see that the computational time taken to process the code is 0.156 seconds
using cputime and 0.198011 seconds using tic and toc. What do you think the difference in
runtime is due to?
The computational time can be reduced by avoiding for loops and vectorising instead. Take, for
example:
100 000
1
∑
𝑛2
𝑛=1
tic
s=0;
for n = 1:100 000
s=s+1/n^2;
end;
toc
On our PC, this took about 0.272015 seconds. Let us try vectorising:
11
tic
n = 1:100 000;
s=sum(s+1./n.^2);
toc
This took about 0.001 seconds. Compare the runtimes on your PC. Different processors (faster or
slower, depending how old/new your PC is) will give different processing times. In terms of
programming efficiency, by vectorising or improving the way we write our program and the
convergence criteria, the computational time can also be increased.
Activity 4.2
Numerical methods are used to find solutions to mathematical problems that have no analytical
solution. Numerical solutions are an approximation in comparison with analytical solutions, and
accuracy requires evaluation, especially when working with large sets of data and long runtimes. This
section briefly explores an area of numerical integration that has been developed to solve non-linear
equations.
In the following sections, we will look at the graphical technique, the bisection method, the secant
method and Newton’s method for solving equations.
One of the simplest ways of finding the root of an 𝑓(𝑥 ) = 0 equation is to plot the equation
graphically and then to determine where the x-axis passes. The intersection/s then provide/s a rough
estimate of the root. Often, the graphical method is used as a rough estimate in numerical methods
(Chapra & Canale 2015).
12
As you will recall from learning unit 3, section 3.3.1, 2D plots are drawn using the plot function.
Using the graphical technique, we need to estimate the roots of the equation:
𝑓 (𝑥 ) = 𝑥 3 + 𝑥 − 3
>>x=-2:0.1:2;
>>y=x.^3+x-3;
>>plot(x,y)
Intercept at x=1.2184
Using visual inspection, we see that an estimate of the equation is 1.2184. For the interval drawn,
we can see that there is only one root. Try changing the interval to >>x=[10;0.1:10]. Is it possible
to determine another root? What can you say about the shape of the curve? To increase the
accuracy of the root, reduce the interval further: >>x=[1;0.1:1.5].
Activity 4.3
Estimate the roots of the following equations using the graphical technique:
13
1. 𝑓(𝑥 ) = 3 + 5𝑥 + 2𝑥 2
2. 𝑓(𝑥) = 1 + 2𝑥 + 3𝑥 3
3. 𝑓(𝑥 ) = 𝑥 2 + 4𝑥 − 1
The bisection method is guaranteed to find a root if you can find two starting values for 𝑥𝐿 and 𝑥𝑅
between which the function changes sign. Consider the problem:
𝑓 (𝑥 ) = 𝑥 3 + 𝑥 − 3
We need to find, by trial and error, two values of 𝑥, 𝑥𝐿 and 𝑥𝑅 , such that 𝑓 (𝑥𝐿 ) and 𝑓(𝑥𝑅 ) have
different signs. Should we find two such values, then the root must lie somewhere in the interval
between them since 𝑓(𝑥) changes sign on the interval, as shown in figure 4.9.
𝑓(𝑥)
𝑥𝐿
𝑥𝑀 𝑥𝑅
If, in this example, we choose 𝑥𝐿 = 1 and 𝑥𝑅 = 2, then 𝑓(1) = −1 and 𝑓(2) = 7 will be reasonable
because we have 1 +ve value and another –ve value. The midpoint of the interval, 𝑥𝑀 , is estimated:
𝑥𝐿 + 𝑥𝑅
𝑥𝑀 =
2
• If 𝑓(𝑥𝑀 ) has the same sign as 𝑓(𝑥𝐿 ), the root lies between 𝑥𝐿 and 𝑥𝑀 . The new value of
𝑥𝐿 = 𝑥𝑀 .
• If 𝑓(𝑥𝑀 ) and 𝑓(𝑥𝐿 ) have different signs, the root lies between 𝑥𝑀 and 𝑥𝑅 . The new value of
𝑥𝑅 = 𝑥𝑀 .
• Having redefined 𝑥𝐿 or 𝑥𝑅 , we bisect the interval again and repeat the process until the
distance between 𝑥𝐿 and 𝑥𝑅 is as small as we need.
14
The advantage of this method is that the number of bisections needed to obtain a certain accuracy
can be estimated beforehand:
|𝑎 − 𝑏|
log ( )
𝑛> 𝐸
log(2)
Should you have difficulty understanding the mathematical principles of the bisection method,
watch the video clip titled “How to locate a root: bisection method” (12:51) by ExamSolutions at this
URL: https://youtu.be/OzFuihxtbtA.
1. Input 𝑎, 𝑏 and 𝐸
2. Initialise 𝑥𝐿 and 𝑥𝑅
3. Compute maximum bisections 𝑛
4. Repeat 𝑛 times:
Compute 𝑥𝑀
Let 𝑥𝐿 = 𝑥𝑀
otherwise
Let 𝑥𝑅 = 𝑥𝑀
5. Display root 𝑥𝑀
6. Stop
We need to calculate the root of the following polynomial introduced in section 4.5.1:
𝑥3 + 𝑥 − 3 = 0
function bisection()
f=@(x)x^3+x-3;
eps=1e-6; %absolute error defined
a=0; b=1000;
[solution, no_iterations] = bisection(f, a, b, eps);
if solution <=b
fprintf('number of function calls:%d\n',1+2*no_iterations);
fprintf('a solution is: %f\n', solution);
else
fprintf('abort execution .\n');
end
end
15
f_M=f(x_M);
iteration_counter=1;
while abs(f_M)>e
left_f=f(x_L);
right_f=f(x_R);
if left_f*f_M>0 %same sign
x_L=x_M;
else
x_R=x_M;
end
x_M=(x_L+x_R)/2;
f_M=f(x_M);
iteration_counter = iteration_counter +2;
end
result1=x_M;
result2=iteration_counter;
end
16
Activity 4.4
1. Try changing the relative error to 1𝑒 − 8 in the bisection_method example. How does
this change the number of function calls?
2. Set 𝑎 = 1 and 𝑏 = 2. Is the number of function calls increased or decreased? And if you set
𝑎 = 2 and 𝑏 = 3?
3. Calculate the processing time using the bisection method.
4. Solve the following equation using the bisection method:
𝑝(𝑥 ) = 3 + 5𝑥 + 2𝑥 2
Compare your answer with the estimate found in activity 4.3, Q1.
5. Compare the code for the bisection method that is provided in this section with the code
that can be found at this URL:
https://sites.google.com/site/numericalprocessespro2012/closed-
methods/bisection/octave-code.
What are the notable differences between the codes?
6. Solve the equation:
𝑥3 + 𝑥 − 3 = 0
Using the downloaded code from Q5, calculate the roots of the equation. Compare the
solution with the solution obtained in the example above. Comment on the changes, if any.
Newton’s method is one of the easiest and most efficient methods of solving equations and uses
iterations in that it repeatedly attempts to improve an estimate of a root. Refresh your memory by
reading the background notes on the method that can be found at this URL:
https://en.wikibooks.org/wiki/Calculus/Newton%27s_Method
Newton’s method, also known as the Newton–Raphson method, is used to find approximations to
the roots of a real-valued function. The method begins with a function 𝑓 defined over real numbers,
the derivative of the function 𝑓 ′ and an initial guess 𝑥0 in order to determine the root of the
function 𝑓. If the initial guess is close and the function satisfies the Newton conditions, then a better
approximation is defined:
𝑓(𝑥0 )
𝑥1 = 𝑥0 −
𝑓 ′ (𝑥0 )
𝑓(𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 −
𝑓 ′ (𝑥𝑛 )
If 𝑥𝑛 is an approximation to the root, then we can relate it to the next approximation 𝑥𝑛 + 1 using
the right-angled triangle in figure 4.11 (the tangent to the function).
17
𝑓(𝑥)
𝑠𝑙𝑜𝑝𝑒 = 𝑓′(𝑥𝑛 )
𝑓(𝑥𝑛 )
𝑓(𝑥𝑛 ) − 0 ∆𝑦
𝑓 ′ (𝑥𝑛 ) = 𝑜𝑟 𝑠𝑙𝑜𝑝𝑒 =
𝑥𝑛 − 𝑥𝑛+1 ∆𝑥
𝑑𝑓
where 𝑓 ′ (𝑥 ) = .
𝑑𝑥
𝑓 (𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 −
𝑓′(𝑥𝑛)
18
Let us see how to solve the equation 𝑥 3 + 𝑥 − 3 = 0, introduced in the bisection method, using the
above algorithm:
Now we will write a separate script file that will stop when the relative error is less than 10−8 or
after 20 steps (whichever occurs first):
steps = 0;
x = input (‘Initial guess: ‘); %estimate root
re = 1e-8;
myrel = 1;
if myrel <= re
disp (‘zero found at’)
disp(x)
else
disp(‘zero not found’)
end;
If our initial guess is 1, we achieve convergence in 5 steps, with a zero found at 1.213 (refer to figure
4.12). This is very efficient in comparison with the bisection method.
19
Figure 4.12: Execution of Newton’s rule
The code indicated above can be modified for solving other equations.
Should you need to learn more about the Newton–Raphson method, study the notes of the
Mathematics Department of the University of the British Columbia that are available at this URL:
http://www.math.ubc.ca/~anstee/math104/newtonmethod.pdf. Alternatively, watch the video clip
titled “Newton–Raphson method – maths made easy” (12.23) at this URL:
https://youtu.be/PIPiv6gn_Ls.
By way of example, the square root of any positive number may be found using Newton’s method.
Background notes on Newton’s method of finding square roots can be accessed at this URL:
http://www-math.mit.edu/~stevenj/18.335/newton-sqrt.pdf.
The structure of the algorithm for finding the square root of a positive number 𝑎 is as follows:
20
1. Initialise a
2. Initialise x to a/2
3. Repeat 6 times (6 times as an example)
Replace x by (x+a/x)/2
Display x
4. End
Let us look at the following example of finding the square root of 5 (√5) – refer to figure 4.13 for the
Octave output:
a=5;
x=a/2;
for i=1:6
x=(x+a/x)/2;
disp(x);
end
disp(sqrt (5))
Line 7 checks the answer using the Octave square root function. In this way we can compare the
accuracy of the solution. Try changing the format to format long to see if/how the decimal places
affect the change in convergence. What can you say about the convergence and the number of
iterations needed? Change the code to determine the square root of 2( √2).
Activity 4.5
1. Calculate the square root of 6 (√6). Determine the computational time needed to run the
code.
2. Using cputime, calculate the computational time taken to run the loop.
21
Feedback: See the code saved as newt.m below.
3. Compare the execution time related to Q1 by using tic and toc. Why is there a difference
in the time?
4. Solve the following equation using Newton’s method:
𝑥 2 + 4𝑥 − 1 = 0
Hint: You need to change the function files as follows:
5. Try changing the initial guess in Q4 to -1 and 0. Does this change the answer?
6. Solve the non-linear equation using Newton’s method. Calculate the cputime, too:
𝑝(𝑥 ) = 3 + 5𝑥 + 2𝑥 2
6. You have a spherical storage tank containing oil. The tank has a diameter of 2 m. You are
asked to calculate the height ℎ to which a dipstick that is 2.5 m long would become wet with
oil when immersed in the tank when it contains 2 m3 of oil. The equation giving the height
of the liquid in the spherical tank for the given volume and radius is as follows:
𝑓(ℎ) = 0.30ℎ3 − ℎ2 + 0.16 = 0
22
Dipstick
Spherical tank
The secant method is used when the function evaluations take too long using Newton’s method or
when it is difficult to determine the derivative of a function. In this method, we use secants instead
of taking tangents to the function (refer to figure 4.11 on Newton’s method). This is illustrated in
figure 4.14.
23
The secant method does not use the derivative of 𝑓(𝑥), but rather uses the approximation of the
straight line going through the most recent approximations 𝑥𝑛 and 𝑥𝑛−1 . The slope of the secant
would then be
𝑓(𝑥𝑛 ) − 𝑓(𝑥𝑛−1 )
𝑥𝑛 − 𝑥𝑛−1
𝑓(𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 −
( )
𝑓 𝑥𝑛 − 𝑓(𝑥𝑛−1 )
𝑥𝑛 − 𝑥𝑛−1
or
𝑥𝑛 − 𝑥𝑛−1
𝑥𝑛+1 = 𝑥𝑛 − 𝑓(𝑥𝑛 )
𝑓 (𝑥𝑛 ) − 𝑓(𝑥𝑛−1 )
In figure 4.13 we see that the first two iterations 𝑥0 and 𝑥1 are used to compute the next iteration
𝑥2 . Similarly, once we have 𝑥2 , 𝑥1 and 𝑥2 are used to compute the next iteration 𝑥3 . The procedure
is repeated until some limit on the number of iterations has been reached or 𝑓(𝑥𝑛 ) is below some
chosen limit value. Unlike Newton’s method, in order for the algorithm to start, we need two
guesses, 𝑥0 and 𝑥1 .
Should you have difficulty understanding the secant method, access the notes, which include
examples, that are available at this URL:
https://mat.iitm.ac.in/home/sryedida/public_html/caimna/transcendental/iteration%20methods/se
cant/secant.html.
You can also watch the video clip titled “Roots: secant method” (3:02) by Jacob Bishop at this URL:
https://youtu.be/ZfMibcIr5YA.
Let us calculate the roots of the polynomial introduced in the previous sections and compare the
secant method with the other methods:
𝑥3 + 𝑥 − 3 = 0
function secant()
f=@(x)x^3+x-3;
e=1e-6;
x0=1000; x1=x0-1;
[solution, no_iterations] = secant(f, x0, x1, e);
if no_iterations > 0 %solution is found
fprintf('number of function calls:%d\n',2 + no_iterations);
fprintf('a solution is: %f\n', solution);
else
fprintf('abort execution .\n');
end
24
end
function [solution, no_iterations]=secant(f, x0, x1, e)
f_x0=f(x0);
f_x1=f(x1);
iteration_counter = 0;
while abs(f_x1)> e && iteration_counter<100
try
denominator = (f_x1 - f_x0)/(x1 - x0);
x = x1-f(x1)/denominator;
catch
fprintf('error-denominator zero for x= \n', x1)
break
end
x0=x1;
x1=x;
f_x0=f_x1;
f_x1=f(x1);
iteration_counter = iteration_counter +1;
end
%solution found or too many iterations
if abs(f_x1)>e
iteration_counter = -1;
end
solution = x1;
no_iterations = iteration_counter;
end
From figure 4.15 we can see that the solution is found in 30 iterations, which is better than what is
obtained using the bisection method. Try changing 𝑥0 to 10. How many iterations are needed? And if
you change 𝑥0 to 1?
25
Figure 4.15: The secant method
Activity 4.6
follows an elementary rate law and is carried out isothermally in a flow system. The
concentrations of A and B in the feed streams are 2 M before mixing. The volumetric flow
rate of each stream is 5 dm3/min and the entering temperature is 300 K. The streams are
mixed immediately before entering. A 200 dm3 CSTR that can be heated to 77 °C or cooled to
0 °C is available. Calculate the conversion that can be achieved in the CSTR.
26
Hint:
20000 1 1
𝑘 = 0.07𝑒𝑥𝑝 ( − )
1.987 300 350
𝑑𝑚3
𝑘 = 8.45 . 𝑚𝑖𝑛
𝑚𝑜𝑙
(𝑉)(−𝑟𝐴 )
CSTR: 𝑋 =
𝐹𝐴0
𝑉(𝑘𝐶𝐴0 2 (1 − 𝑋)2 )
𝑋=
10
𝑑𝑚3 𝑚𝑜𝑙 2
(200𝑑𝑚3 ) (8.45 ) (1 ) (1 − 𝑋 ) 2
𝑚𝑜𝑙. 𝑚𝑖𝑛 𝑑𝑚3
𝑋=
𝑚𝑜𝑙
10
𝑚𝑖𝑛
simplify the expression and solve for 𝑋 using the bisection method, Newton’s method or the secant
method.
𝑓 (𝑥 ) = 0
Some built-in functions of Octave exist to solve for zeros of single linear or non-linear functions, as
well as systems of non-linear functions. These functions are summarised in table 4.1.
The following activity will demonstrate the abovementioned Octave functions by means of
examples.
Activity 4.7
27
>>roots([3 0 2 1])
The Octave output should give you one real root 𝑥 = −0.4 and two complex roots 𝑥 =
0.2 ± 0.9𝑖.
Compare this with the estimate obtained in activity 4.3, Q2.
>>x0=fsolve(@(x)sin(x),3)
Note: @(x) is a convenient function handle that can point to an existing function (like a .m
file) or an anonymous function. This is essential for problems that include solving a non-
linear equation or a differential equation. In the above code, the @(x) handle is used to
point to an anonymous function. Recall from Q3: the Help function >>help fsolve gives
fsolve (FCN, X0, OPTIONS).
𝑥 2 + 4𝑦 2 − 1 = 0
4𝑥 4 + 𝑦 2 − 1 = 0
𝑥 2 + 2𝑦 2 − 2 = 0
2𝑥 3 + 𝑦 2 − 3 = 0
28
the maximum possible conversion if the forward reaction is second order and the reverse
𝐿
reaction is first order: 𝑘1 = 0.4 and 𝑘2 = 0.1 𝑚𝑖𝑛−1 .
𝑚𝑜𝑙.𝑚𝑖𝑛
Hint:
𝑟𝐴 = −𝑘1 𝐶𝐴 2 + 𝑘2 𝐶𝐵
𝐶𝐴 = 𝐶𝐴0 − 𝑥 = 1.2 − 𝑥
𝐶𝐵 = 𝐶𝐵0 + 𝑥 = 0 + 𝑥 = 𝑥
𝑟𝐴 = −0.4(1.2 − 𝑥 )2 + 0.1𝑥 = 0
4.6 Conclusion
In learning unit 4 we looked at methods for approximating solutions for equations that cannot be
determined analytically. Loops for iterative processes; built-in functions in Octave for determining
the roots of an equation; computational time and accuracy; and the secant, bisection and Newton’s
methods were discussed. The key Octave functions discussed in this learning unit are summarised
below.
1. Chapra, SC & Canale, RP. 2015. Numerical methods for engineers. 7th edition. New York:
McGraw-Hill Education.
29