+ All Categories
Home > Documents > AP3114 Lecture WK6 WK8 NonlinearEquations

AP3114 Lecture WK6 WK8 NonlinearEquations

Date post: 12-Dec-2015
Category:
Upload: guoxuanchan
View: 216 times
Download: 2 times
Share this document with a friend
Description:
Physics
Popular Tags:
55
AP3114/Computational Methods for Physicists and Materials Engineers 5 & 6. Solving non-linear equations Oct 5 & 19, 2015 1
Transcript

AP3114/Computational Methods for Physicists and Materials Engineers

AP3114/Computational Methods for Physicists and Materials Engineers5 & 6. Solving non-linear equationsOct 5 & 19, 2015

11Non-linear equationsSolution to non-linear equationsNon-linear equations: algebraic one: quadratic, cubic, and polynomial equations 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, quadratic equations, may produce a complex solution

Numerical Methods: bisection, Newton-Raphson, and secant2ax2+bx+c = 0x3+ax2+bx+c = 0a0+a1x+a2x2+a3x3++anxn =0 Lecture 5Introduction 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

Contents3Introduction to complex numbersA complex number z is a number written asz = 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 referredto as the real axis, and the y-axis referred to as the imaginary axis. Thus, a complex numberrepresented 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) asz = rei= rcos+ irsinwherer = |z|= (x2+y2)1/2is the magnitude of the complex number z, and = Arg(z) = arctan(y/x)is the argument of the complex number z.The relationship between the Cartesian and polar representation of complex numbers is givenby the Euler formula:ei= cos+ i sin

4Introduction to complex numbers

Examples of basic complex number operations in SCILABThe 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:

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

5ipow=[];for j=1:10,ipow=[ipow %i^j]; end; ipowz=3+5*%i5Introduction to complex numbersThe 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:

6The magnitude of the complex number z: r = |z|= (x2+y2)1/2 The argument of the complex number z:= Arg(z) = arctan(y/x)real(z), imag(z)abs(z), atan(imag(z)/real(z))6If we write the polar representation of a complex number as r exp(%i*theta), SCILAB returns the Cartesian representation. For example,Introduction to complex numbers

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

The complex conjugate of a complex number is obtained by using the function conj:

75*exp(0.25*%i)conj(z)[r,theta]=polar(z)[r,th]=polar(z)7The negative is simply obtained by adding a minus sign to the number, i.e.,Introduction to complex numbers

Matrices in SCILAB can have complex numbers as elements. The following commands, forexample, produce a 3x4 matrix of complex numbers. First, we generate random matrices withinteger numbers to be the real and imaginary parts of the complex numbers:

8xMs=10*rand(3,3),yMs=10*rand(3,3)8Next, the complex-number matrix is put together by using:Introduction to complex numbers

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

9zMs = xMs + yMs*%iabs(zMs)

9Introduction to complex numbersTo 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 ageneralization of the function atan used when the argument is a matrix. Here is the calculation of the arguments

10atanm(yMs./xMs)10Complex 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, x1, x2, y1, and y2 are real numbers.Complex number calculationsIntroduction to complex numbers

Powers: zn= (re i)n= rne inRoots: because the argument of a complex number zhas a periodicity of 2, we can writez = rei(+2k) , for k = 0, 1, 2,

There are n n-th roots of z calculated as:

11Introduction to complex numbersExamples of operations with complex numbersThe following are operations with complex numbers using SCILAB:

Addition, subtraction, multiplication, and division:

12z1 = -5+2*%i, z2 = 3+4*%iz1+z2z1-z2z1*z2z1/z212Introduction to complex numbersThe following sequence provides the first 5 integer powers of z1:

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

13z1pow = []; for j=1:5, z1pow=[z1pow z1^j]; end; z1powz1^(1/3)

13Introduction to complex numbersThere 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 andargument 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:14z1=-5+2*%i,z2=3+4*%icubic_roots_of_z1=[];fork=0:2cubic_roots_of_z1=[cubic_roots_of_z1r1^(1/3)*exp(%i*((theta1+2*k*%pi)/3))];end;cubic_roots_of_z1

14Introduction to complex numbers

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

15The many roots of a real or complex numberConsider a complex numberz = rei = rcos+ irsin.Because the functions sin and cos are periodic functions of period 2 in , the complex number z can be written asz = rei(+2k), k = 0, 1, 2, 3, ...In other words, there are infinite ways to represent the complex number z. The most general representation beingz = rei(+2k)To obtain the n-th root of this complex number we can writez1/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 nindependent roots of the number z.16The many roots of a real or complex numberExample 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:Fromz = 16ei= 16ei(+2k)= 16ei(1+2k),it follows thatr = z1/4= 161/4e i(1+2k)(/4) = 2e i(1+2k)(/4) , k = 0, 1, 2, 3Thus,For k = 0, r1= 2e i(1+20)(/4) = 2e i/4 = 2cos(/4) + i2sin(/4) = 2(1 + i)For k = 1, r2 = 2e i(1+21)(/4) =2e i3/4 = 2cos(3/4) + i2sin(3/4) = 2(-1 + i)For k = 2, r3= 2e i(1+22)(/4) = 2e i5/4 = 2cos(5/4) + i2sin(5/4) = -2(1 + i)For k = 3, r4= 2e i(1+23)(/4) = 2e i7/4 = 2cos(7/4) + i2sin(7/4) = 2(1 - i)17The many roots of a real or complex numberUsing 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.18Using SCILAB, the previous calculations are performed as follows:The many roots of a real or complex number

n=4;19z = -16; r=abs(z); theta = -%pi;roots_of_z = [];for k = 0:3roots_of_z = [roots_of_z r^(1/4)*exp(%i*((theta+2*k*%pi)/3))];end;roots_of_z

19Principal values of a cubic rootThe many roots of a real or complex numberThe 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 theresult 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.

20To calculate the cubic roots of z = 81, with r = 81 and = 0, we use:The many roots of a real or complex number

Example 2 - Cubic roots of a positive number:21z = 81, r = abs(z), theta = 0for k = 0:2r^(1/3)*exp(%i*(theta+2*k*%pi)/3)end

21The many roots of a real or complex numberExample 3 - Cubic roots of a negative number:To calculate the cubic roots of z = 8, with r = 9 and = , we use:

22z = -8, r = abs(z), theta = %pifor k = 0:2r^(1/3)*exp(%i*(theta+2*k*%pi)/3)end

22Solution to quadratic and cubic equationsQuadratic equations, of the form ax2+bx+c = 0and cubic equations, of the form ax3+bx2+cx+d=0are 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 equationsQuadratic equations are those algebraic equations with one unknown that can be reduced tothe form:ax2+bx+c = 0where a, b, and c are real numbers.The roots:x = [-b (b2-4ac)1/2]/(2a)23Solution to quadratic and cubic equationsQuadratic equationsQuadratic equations are those algebraic equations with one unknown that can be reduced tothe form:ax2+bx+c = 0where 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, one real and two complex conjugate solutions (calculated using the formulas shownabove).If D = 0, three real solutions including a double solution (x2 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 willintroduce complex expressions because the calculation of u and v involves the square root ofD 0, while f(b)f(c) < 0, and xR is contained in the interval (c,b). 41We 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)| < .

Solution of a single non-linear equationPseudo-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 tore-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 theuser 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 to c = 1/2(a+b).6. Check if convergence condition, |f(c)| < , is satisfied. If it is so, print the solution andstop.42Solution of a single non-linear equation7. 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 ofiterations.SCILAB function for interval-halving or bisectionThe following function, can be used to obtain a solution using interval halving. Thefunction looks like this:43Solution of a single non-linear equation

half.sci function44//function [x]=half(a,b,f)//interval halving routineN = 100; eps = 1.e-5; // define max. no. iterations and errorif (f(a)*f(b) > 0) thenerror('no root possible f(a)*f(b) > 0')abort;end;if(abs(f(a)) < eps) thenerror('solution at a')abort;end;if(abs(f(b)) < eps) thenerror('solution at b')abort;end;while (N > 0)c = (a+b)/2;if(abs(f(c)) < eps) thenx = c;xreturn;end;if(f(a)*f(c) < 0)thenb = c;elsea = c;end;N = N - 1;end;error('No convergence')abort;//end functionEndfunction

44Example of interval-halving (bisection) method applicationSolution of a single non-linear equationLet'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:

45deff('[y]=p(x)',['y=x^3-2*x^2-2*x-1'])xx = 0:0.1:5; yy = p(xx); size(xx) zeroLine = zeros(1,51);plot2d([xx' xx'],[yy' zeroLine'])

45The figure shows a root between 2 and 3. The next step in the solution is to load functionhalf, and call it with the proper arguments:Solution of a single non-linear equation

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.

46exec('half.sci'); x = half(2,3,p)

46Solution of a single non-linear equationThe Newton-Raphson methodConsider the Taylor-series expansion of the function f(x) about a value x = xo: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 equationf(x) = 0can be obtained fromf(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 rootthrough the recurrent application of equation. For example, the second and third approximations to that root will be given byx2= x1- f(x1)/f'(x1),andx3= x2- f(x2)/f'(x2),respectively.This iterative procedure can be generalized by writing the following equation, where irepresents the iteration number:xi+1= xi- f(xi)/f'(xi).47After each iteration the program should check to see if the convergence condition, namely,|f(x i+1)|


Recommended