Anda di halaman 1dari 55

AP3114/Computational Methods for

Physicists and Materials Engineers


5 & 6. Solving non-linear
equations
Oct 5 & 19, 2015

Non-linear equations
Non-linear equations:
algebraic one: quadratic, cubic, and polynomial
equations
ax2+bx+c = 0 x3+ax2+bx+c = 0 a0+a1x+a2x2+a3x3++anxn =0
non-algebraic ones: exponential (ex),
logarithmic (logx), trigonometric (sinx), and
hyperbolic functions (sinhx)
They are transcendental equations (i.e., they
transcend the realm of algebraic equations)
They involve complex numbers: Even the
simplest
of algebraic
equations, namely,
Solution
to non-linear
equations
quadratic equations, may produce a complex
solution
2

Contents

Lecture 5
Introduction to complex numbers
Solution to quadratic and cubic equations
The many roots of a real or complex number
Lecture 6
Polynomials and solutions to polynomial equations
Solution of a single non-linear equation

troduction to complex numbers


A complex number z is a number written as
z = x + iy,
where x and y are real numbers, and i is the imaginary unit defined by
i2= -1.
The complex number x+iy has a real part,
x = Re(z),
and an imaginary part,
y = Im(z).
We can think of a complex number as a point P(x,y) in the x-y plane, with the
x-axis referred
to as the real axis, and the y-axis referred to as the imaginary axis. Thus, a
complex number
represented in the form x+iy is said to be in its Cartesian representation.
A complex number can also be represented in polar coordinates (polar
representation) as
z = rei= rcos+ irsin
where
r = |z|= (x2+y2)1/2
is the magnitude of the complex number z, and
= Arg(z) = arctan(y/x)
is the argument of the complex number z.

troduction to complex numbers

The
complex conjugate of a complex number z = x + iy
= rei, is
= x iy = re-i
The complex conjugate of z can be thought of as the
reflection of z about the real (x-) axis.
Examples of basic complex number operations in
SCILAB
The unit imaginary number, i = (-1), is represented in SCILAB by the
symbol %i. A sequence of the first 10 powers of I can obtained by
using:

omplex number, say z = 3+5i, is written as:

troduction to complex numbers


The functions real and imag can be used to obtain the real and imaginary
parts, respectively,
of a complex number, for example:

The magnitude and argument of the complex number z are obtained as:
The magnitude of
the complex
number z:
r = |z|= (x2+y2)1/2
The argument of
the complex
number z:
= Arg(z) =

troduction to complex numbers


If we write the polar representation of a complex number as r exp(%i*theta),
SCILAB returns the Cartesian representation. For example,

SCILAB provides the function polar to obtain the magnitude and argument of
a complex
number. The following example illustrates its application:

The complex conjugate of a complex


number is obtained by using the
function conj:

troduction to complex numbers


The negative is simply obtained by adding a minus sign to the number,
i.e.,

Matrices in SCILAB can have complex numbers as elements. The following


commands, for
example, produce a 3x4 matrix of complex numbers. First, we generate
random matrices with
integer numbers to be the real and imaginary parts of the complex numbers:

troduction to complex numbers


Next, the complex-number matrix is put together by using:

To obtain the magnitude of the elements of the matrix just defined we simply
apply the
function abs to the matrix name, i.e.,

troduction to complex numbers


To find the arguments of the elements we can use the function atanm applied
to the term by term division of the imaginary part by the real part of the
elements. The function atanm is a
generalization of the function atan used when the argument is a matrix. Here
is the calculation of the arguments

10

troduction to complex numbers

omplex number calculations


Complex numbers can be added, subtracted, multiplied, and divided. The
rules for these operations are shown below:
Let z = x + iy = rei, z1= x1+ iy1 = r1ei1, and z2= x2+ iy2 = r2ei2,
be complex numbers. In these definitions the numbers x, y, x 1, x2, y1, and y2
are
real numbers. z1+ z2= (x1+ x2)+ i(y1+ y2)
Addition:
Subtraction:
z1- z2= (x1- x2)+ i(y1- y2)
Multiplication: z1z2 = (x1x2- y1y2) + i(x1y2+ x2y1) = r1r2ei(1+2)
Multiplication of a number by its conjugate results in the square of the
numbers magnitude,
i.e.:
z= (x + iy)(x - iy) = x2+ y2= r2= |z|2
Division:
Powers: zn= (re i)n= rne in
Roots: because the argument of a complex number zhas a periodicity of
2, we can write
z = rei(+2k) , for k = 0, 1, 2,
There are n n-th roots of z calculated as:

11

troduction to complex numbers

amples of operations with complex numbers


The following are operations with complex numbers using SCILAB:

Addition, subtraction, multiplication, and division:

12

troduction to complex numbers


The following sequence provides the first 5 integer powers of z1:

The following SCILAB command attempts to find one cubic root of z1:

13

troduction to complex numbers


There are, in fact, three cubic roots for any real or complex number. These
are calculated,
using the formulas indicated earlier, as follows. First, we calculate the
magnitude and
argument of complex number z1:

Next, we create an empty vector called cubic_roots_of_z1:

The vector is then filled with the three roots of z1 as follows:

To see the final result, enter the name of the vector:

14

troduction to complex numbers


To see the final result, enter the name of the vector:

15

e many roots of a real or complex number


Consider a complex number
z = rei = rcos+ irsin.
Because the functions sin and cos are periodic functions of period 2
in , the complex number z can be written as
z = rei(+2k), k = 0, 1, 2, 3, ...
In other words, there are infinite ways to represent the complex
number z. The most general representation being
z = rei(+2k)
To obtain the n-th root of this complex number we can write
z1/n= r1/nei[(/n)+2k/n]
We can check that we need only use the values k = 0, 1, 2, ... (n-1),
to produce all n
independent roots of the number z.

16

e many roots of a real or complex number


Example 1: Consider the number
z = 16ei= 16 cos + i 16 sin = -16,
which is actually a real number. Calculate the roots corresponding
to z1/4:
From
z = 16ei= 16ei(+2k)= 16ei(1+2k),
it follows that
r = z1/4= 161/4e

i(1+2k)(/4)

= 2e

i(1+2k)(/4)

, k = 0, 1, 2, 3

Thus,
For k = 0,
r1= 2e

i(1+20)(/4)

= 2e i/4 = 2cos(/4) + i2sin(/4) = 2(1 + i)

For k = 1,
r2 = 2e

=2e

i3/4

= 2cos(3/4) + i2sin(3/4) = 2(-1 + i)

= 2e

i5/4

= 2cos(5/4) + i2sin(5/4) = -2(1 + i)

i(1+21)(/4)

For k = 2,
r3= 2e

i(1+22)(/4)

For k = 3,
r4= 2e

i(1+23)(/4)

= 2e

i7/4

= 2cos(7/4) + i2sin(7/4) = 2(1 - i)

17

e many roots of a real or complex number


Using values such as k = -1 or k = 4 in the general expression
for the 4-th root of z = -16 will produce values already
accounted for in the four results found above.
This example, therefore, verifies that there are exactly n
independent n-th roots of a complex number, with n = 4 for this
case.
Notice that the results of this example actually correspond to
the 4-th roots of a real number, z = -16.
Since real numbers are special cases of complex numbers, the
approach outlined above for finding the n-th root of a complex
number is also applicable to a real number.
The only requirement is that the number be written in
the Polar form,
z = re i.
18

e many roots of a real or complex number


Using SCILAB, the previous calculations are performed as follows:
n=4;

19

e many roots of a real or complex number

Principal values of a cubic root


The principal value for the n-th root of a complex (or real) number,
z = re i,
is the value corresponding to k = 0 in the general expression:
z1/n= r1/nei[(/n)+2k/n] ,
i.e.,
z1/n= r1/nei/n .
For the case of cubic root (n = 3), if the number z is a positive real
number, = 0, and the
result is straightforward,
z1/3= r

1/3

If the number z is a negative real number, = , and the principal value


is
z1/3= r1/3e

i/3

= r1/3(cos /3 + i sin /3).

Thus, the principal value of the root of a complex number is not


necessarily always a real number.
20

e many roots of a real or complex number

ample 2 - Cubic roots of a positive number:


To calculate the cubic roots of z = 81, with r = 81 and = 0, we use:

21

e many roots of a real or complex number


Example 3 - Cubic roots of a negative number:
To calculate the cubic roots of z = 8, with r = 9 and = , we use:

22

ution to quadratic and cubic equations


Quadratic equations, of the form
ax2+bx+c = 0
and cubic equations, of the form
ax3+bx2+cx+d=0
are the simplest non-linear, polynomial equations.
SCILAB provides function roots to solve polynomial equations of any order.
Therefore, function roots can be used to solve quadratic and cubic equations.
The methods presented next for solving quadratic and cubic equations, are
for SCILAB programming exercises.

Quadratic equations
Quadratic equations are those algebraic equations with one unknown that
can be reduced to
the form:
ax2+bx+c = 0
where a, b, and c are real numbers.
The roots:
x = [-b (b2-4ac)1/2]/(2a)

23

ution to quadratic and cubic equations

Quadratic equations
Quadratic equations are those algebraic equations with one unknown that
can be reduced to
the form:
ax2+bx+c = 0
where a, b, and c are real numbers.
The roots:
x = [-b (b2-4ac)1/2]/(2a)
The quantity under the square root in this result is known as the discriminant
of the equation,
i.e.,
D = b2-4ac.
Possible solutions:
If D>0, the quadratic equation has two distinct real solutions:
x1,2= (-b D1/2)/(2a).
If D=0, the quadratic equation has one real (double) solution,
x1= x2= -b/(2a).
If D<0, the quadratic equation has two conjugate complex solutions,
x1,2= (-b i(-D)1/2)/(2a),

24

ution to quadratic and cubic equations


The basic algorithm (written in pseudo-code) for solving a quadratic equation,
would look something like this:

Note: Pseudo-code is a way to present an algorithm using English-like


statements that are later
coded into a particular computer language. The statements are general 25

ution to quadratic and cubic equations

Cubic equations
The canonical (simplest)form of the cubic equation is
x3+ax2+bx+c = 0,
where a, b, c are real numbers.
Substituting the auxiliary variable
y = x + a/3
leads to the reduced form
y3+py+q = 0,
with
p = (3b-a2)/3,
and
q = c + 2a3/27 ab/3.
A discriminant for the reduced cubic equation is given by
D = (q/2)2+(p/3)3.
An Italian mathematician, Gierolimo Cardano, proposed the following
solutions to the cubic
equation:
x1= -(a/3)+u+v
x2,3= -(a/3)-(u+v)/2 i3(u-v)/2
where
Depending on the sign of the discriminant, D, the solutions to the cubic
equation can be

26

ution to quadratic and cubic equations


If D > 0, one real and two complex conjugate solutions (calculated using the
formulas shown
above).
If D = 0, three real solutions including a double solution (x 2 and x3).
If D < 0, three distinct real solutions (this case is known as the irreducible
case).
In the irreducible case, the formulas shown above for the three roots of the
cubic equation will
introduce complex expressions because the calculation of u and v involves
the square root of
D<0. To avoid introducing such complex expressions, we can use the socalled trigonometric
form of the solution:
and

where

27

ution to quadratic and cubic equations

28

ution to quadratic and cubic equations

29

HW-5
Due: 9am, Oct 12, 2015
Late policy: 10% off per day
To be submitted online to Canvas:
Save all your codes (*.sci files) into one folder to submit;
Do use // to add comments clearly which question you
are referring to, when we execute your codes, answers
shall be shown on the screen.

30

cise for The many roots of a real or complex number


Exercise 1
Calculate all the cubic roots of below numbers in complex
field:
1. 1
2. i
3. 5+3i
4. 2e (i*)/2
5. -2-3i
Note:
Intermediate results recorded using Print Screen
Save your results and submit them in your pdf file.

31

cise for Solution to quadratic and cubic equations

Exercise 2
Write a Scilab program quadroots to compute and print the roots of a
quadratic equation ax2+ bx + c = 0 according to the previous pseudo-code.
It should run with a command like
quadroots(1,3,2)
for the case of x2+ 3x + 2 = 0.
Test quadroots on the following examples, checking the results in each case.
1.
2.
3.
4.

x2+ 1 = 0
0x2+ 2x + 1 = 0
x2+ 3x + 2 = 0
4x2+ 24x + 36 = 0

Note:
Save the script file into yourstudentno_lecture7&8_exe2.sci file
Intermediate results recorded using Print Screen
Save your test results and submit them in your pdf file
32

cise for Solution to quadratic and cubic equations

Exercise 3
Write a Scilab program cubicroots to compute and print the roots of a
quadratic equation ax3+ bx2 + c +d = 0 according to the previous pseudocode.
It should run with a command like
cubicroots(4,5,1,2)
for the case of 4x3+5x2+ 1x + 2 = 0.
Test cubicroots on the following examples, checking the results in each case.
1.
2.
3.
4.

x3+ 1 = 0
3x3+ 5x2+ 2x + 6 = 0
x3+ x2+ x + 1 = 0
9x3+ 3x2+ 4x + 6 = 0

Note:
Save the script file into yourstudentno_lecture7&8_exe3.sci file,
Intermediate results recorded using Print Screen
Save your test results and submit them in your pdf file.

33

ynomials and solutions to polynomial equations


Polynomials are one of the categories of SCILAB data types.
A polynomial of order n, namely,
P = a0+a1x+a2x2+a3x3++anxn,
we use a row vector containing the coefficients as [a 0 a1 a2 an],
and call function poly using this vector, the independent variable
that defines the polynomial, say, x, and the qualifier coeff. For
example, to define a fourth-order polynomial with coefficients [1 -5
0 3 2],
use:

34

ynomials and solutions to polynomial equations


A polynomial of order 10 in y is generated next. The coefficients of
the polynomial are stored in vector c:

The resulting polynomial is:

35

ynomials and solutions to polynomial equations


To find the roots of a polynomial use the function roots. For example, the
roots of the fourth order polynomial p4 are:

The following example provides the roots of the polynomial p10:

36

ynomials and solutions to polynomial equations


The next example combines function roots and poly to calculate
the roots of a polynomial given its coefficients in the vector [2 -3
5 4 2 100]:

37

lution of a single non-linear equation


In an earlier section of this chapter we presented algorithms for
the solution of quadratic and cubic equations. These two types of
equations are examples of polynomial equations of the form:
anxn+ an-1xn-1 + an-2xn-2 ++ a2x2 + a1x + a0= 0,
with n = 2 corresponding to the quadratic equation, and n = 3 to
the cubic equation.
A linear equation would be one for which n = 1, and its solution is
straightforward.
Equations that can be cast in the form of a polynomial are referred
to as algebraic equations.
Equations involving more complicated terms, such as
trigonometric, hyperbolic, exponential, or logarithmic functions
are referred to as transcendental equations.
No general algorithms for solution exist for algebraic (or
polynomial) equations of order n 4, or for transcendental
equations. The methods presented in this section are numerical
38
methods that can be applied to the solution of such equations, to

lution of a single non-linear equation


In general, we will be searching for one, or more, solutions to the
equation,
f(x) = 0.
We will present the methods of interval halving (or bisection
method) and the Newton-Raphson algorithm. In the intervalhalving method we need to provide two initial values of x to get
the algorithm started. In the Newton-Raphson methods only one
initial value is required.
Because the solution is not exact, the algorithms for any of the
methods presented herein will not provide the exact solution to
the equation f(x) = 0, instead, we will stop the algorithm when the
equation is satisfied within an allowed tolerance or error, .
In mathematical terms this is expressed as
|f(xR)| < .
The value of x for which the non-linear equation f(x)=0 is satisfied,
i.e., x = xR, will be the solution, or root, to the equation within an
error of units.
39

lution of a single non-linear equation

nterval-halving or bisection method


This method starts by identifying two values of x, a and b, with
a<b, for which the
corresponding function values, namely, f(a)and f(b), have opposite
signs, i.e., , f(a)f(b) < 0.
Below figure, presents two of such cases.

40

lution of a single non-linear equation


Obviously, since the function y = f(x)changes sign as x goes from x = a to x
= b, somewhere
within the interval (a,b), x must take a value xR for which f(x) = 0. The midpoint of the
interval, namely,
c = (a+b)/2,
Two situations are possible, as illustrated
in the figure below: (1) a < x R< c <
will
be(2)
closer
b; or,
a < to
c <xR than either a or b can be.
xR < b.

In the first case, f(a)f(c) < 0, while f(b)f(c) > 0, and x R is contained in the
interval (a,c). In the second case, f(a)f(c) > 0, while f(b)f(c) < 0, and x R is
contained in the interval (c,b).
41

lution of a single non-linear equation


We can think of c replacing b in the first case in a general interval (a,b),while
c replaces a in the interval (a,b)in the second case. In other words, in the first
case b takes the value of c, bc, and in the second case, ac. We can think
of the method, therefore, as finding the center-point of an ever-decreasing
interval. The mid-point value replaces the interval extreme for which the
product of its function and that of the mid-point is a positive number.
The process is then repeated until a value of c is found so that (3) is satisfied,
i.e., until
|f(c)|
< .
Pseudo-code for the interval-halving
or bisection
method
The following is one possible pseudo-code for the interval-halving method:
1. Function f(x) must be loaded through deff or getf.
2. Enter initial values of a and b.
3. Check that a < b, if not, send message indicating error in input data and
requesting user to
re-enter values of a and b.
4. When input data is correct, check that f(a)f(b) < 0. If that is not the case,
inform the
user that his/her initial values of a and b do not satisfy the problem
conditions for solution.
5. If problem conditions are satisfied, proceed to calculate c according to42c =

lution of a single non-linear equation


7. If convergence solution is not satisfied, replace values of a or b according
to the following procedure: If f(b)f(c) > 0, b=c, else a=c.
8. Repeat procedure from step 5 on. Stop if the number of iterations is too
large. Send a message to user indicating that the process is not converging
after a large number of
iterations.
SCILAB function for interval-halving or bisection
The following function, can be used to obtain a solution using interval
halving. The
function looks like this:

43

lution of a single non-linear equation

half.sci function

//

44

lution of a single non-linear equation


Example of interval-halving (bisection) method application
Let's apply it to an example, first define the function p(x) as follows:

We will produce a plot of the function to check possible solutions visually, for
example:

45

lution of a single non-linear equation


The figure shows a root between 2 and 3. The next step in the solution is to
load function
half, and call it with the proper arguments:

To check the solution evaluate the function at the value of x:

The function does not evaluates to zero. However, the error involved is in the
order of 5x10-6,
small enough to accept the solution provided.

46

lution of a single non-linear equation

e Newton-Raphson method
Consider the Taylor-series expansion of the function f(x) about a value x = x o:
f(x)= f(xo)+f'(xo)(x-xo)+(f"(xo)/2!)(x-xo)2+.
Using only the first two terms of the expansion, a first approximation to the
root of the equation
f(x) = 0
can be obtained from
f(x) = 0 f(xo)+f'(xo)(x1 -xo)
Such approximation is given by,
x1= xo- f(xo)/f'(xo).
The Newton-Raphson method consists in obtaining improved values of the
approximate root
through the recurrent application of equation. For example, the second and
third approximations to that root will be given by
x2= x1- f(x1)/f'(x1),
and
x3= x2- f(x2)/f'(x2),
respectively.
This iterative procedure can be generalized by writing the following equation,
47
where i

lution of a single non-linear equation


After each iteration the program should check to see if the convergence
condition, namely,
|f(x i+1)|<,
is satisfied.
The figure below illustrates the way in which the solution is found by using
the Newton-Raphson method. Notice that the equation f(x) = 0 f(x 0)+f'(x0)
(x1 x0)represents a straight line tangent to the curve y = f(x)at x = x 0. This
line intersects the x-axis (i.e., y = f(x) = 0) at the point x 1 as given by x1= x0f(x0)/f'(x0). At that point we can construct another straight line tangent to y =
f(x) whose intersection with the x-axis is the new approximation to the root of
f(x) = 0, namely, x = x2. Proceeding with the iteration we can see that the
intersection of consecutive tangent lines with the x-axis approaches the
actual root relatively fast.

48

lution of a single non-linear equation


The Newton-Raphson method converges relatively fast for most functions
regardless of the
initial value chosen. The main disadvantage is that you need to know not
only the function
f(x), but also its derivative, f'(x), in order to achieve a solution. The
programming algorithm of any of these methods must include the option of
stopping the program if the number of iterations grows too large. How large
is large? That will depend of the particular problem solved. However, any
interval-halving, fixed iteration, Newton-Raphson, that takes more than 1000
iterations to converge is either ill-posed or contains a logical error. Debugging
of the program will be called for at this point by changing the initial values
provided to the program, or by checking the program's logic.

49

lution of a single non-linear equation


A SCILAB function for the Newton-Raphson method
The function newton, listed below, implements the Newton-Raphson
algorithm. It uses as
arguments an initial value and expressions for f(x) and f'(x).

50

lution of a single non-linear equation


We will use the Newton-Raphson method to solve for the equation, f(x) = x 32x2+1 = 0. The
following SCILAB commands define the function f(x) and its derivative, fp(x),
and load the
function newton.sci:

To have an idea of the location of the roots of this polynomial we'll plot the
function using the
following SCILAB commands:

51

lution of a single non-linear equation


We see that the function graph crosses
the x-axis somewhere between -0.8 and
-0.4, between 0.8
and 1.2, and between 1.6 and 2.0. The
following commands use the function
newton with
different initial values. The nature of the
function is such that most initial values
converge to
either of the two real roots, a few diverge,
but it is very difficult to make the
algorithm
converge to the negative root. The
number listed before the variable name
ans in each
solution is the number of iterations
required to obtain a solution.

52

HW-6
Due: 9am, Oct 26, 2015
Late policy: 10% off per day
To be submitted online to Canvas:
Save all your codes (*.sci files) into one folder to submit;
Do use // to add comments clearly which question you
are referring to, when we execute your codes, answers
shall be shown on the screen.

53

cise for Polynomials and solutions to polynomial equations


Find the roots of below polynomial using the function roots:
1. -10+x+5x3+3x5
2. 1-x2+x5-6x7
3. 3+x-x2+x4

Note:
Intermediate results recorded using Print Screen
Save your results and submit them in your pdf file.

54

rcise for Solution of a single non-linear equation


Show that f (x ) = x3+ 4x2 10 = 0 has a root in [1, 2] and use the
Bisection method and Newton-Raphson respectively to determine an
approximation to the root that is accurate to at least within 10 4.

Note:
Intermediate results recorded using Print Screen
Save your results and submit them in your pdf file.

55

Anda mungkin juga menyukai