You are on page 1of 8

COMPUTER SCIENCE & ENGINEERING DEPARTMENT ` OBAFEMI AWOLOWO UNIVERSITY . . . . ` `. ILE-IFE, OSUN STATE, NIGERIA . .

A . . Harmattan Semester, 2011-2012 Session PRACTICAL LAB II CSC 307: Numerical Computations I

April 24, 2012

Contents
1 Laboratory Assignment II 1.1 Task 01: Canonical Polynomials . . . . . . . 1.2 Task 02: Horners Polynomials . . . . . . . . 1.3 Task 03: Root of Polynomials . . . . . . . . 1.4 Task 04: Evaluating Function as Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 4 5 6 6 7

Engineering Application 2.1 Task 05: Application of Polynomials . . . . . . . . . . . . . . . . . .

Laboratory Assignment II

During our lectures, we have seen that polynomials provide an efcient means of representing some mathematical expressions. We have also seen that most trigonometric, hyperbolic, and exponential functions can be expressed in the form of polynomial. The accurate, efcient and effective evaluation of polynomials are therefore important in the development of computational solution to scientic and engineering problems. A polynomial of degree n is written as: Pn (x) = a0 x0 + a1 x1 + a2 x2 +, . . . , +a(n1) x(n1) + an xn Equation 1 can also be written in the form:
n

(1)

Pn (x) =
i=0

ai xi

(2)

Equation 1 and 2 are called the Canonical, Nave, Series or Sequential form of polynomial expression. The values ai , i = 0, 1, 2, . . . , n are the parameters of the polynomial and x is the variable (independent). A polynomial of degree one, i.e. n = 1, is called linear (it describes a line function or relation). Polynomials with degree two or more, i.e. when (n 2), are called non-linear functions. Specic names for polynomial commonly used in engineering applications are listed in Table 1. Table 1: Polynomial Nomenclature Degree 1 2 3 4. Name Linear Quadratic Cubic Quatic Representation a0 + a1 x a0 + a1 x + a2 x2 a0 + a1 x + a2 x2 + a3 x3 a0 + a1 x + a2 x2 + a3 x3 + a4 x4

Polynomials are frequently used in engineering problem description, modelling and simulation. This is because they exhibit a number of unique properties which include the following [2, 1, 3]: 1. Polynomials are smooth. 2. Polynomials are easy to store and manipulate on a digital computer. To store polynomials, we only need to store the coefcients (i.e. the ai s) and one variable (i.e. x). To manipulate polynomials we require only basic arithmetic operations of addition (+), subtraction (-) and multiplication (*). 3. The derivative and integral of a polynomial will produce another polynomial whose coefcient can be computed algebraically. 4. The root of a polynomial can be easily computed using an algorithm. 5. Given a continuous function or a set of values over a closed interval, say [a, b], it is possible to nd a polynomial to model or represent it. 6. Most engineering problems can be expressed as a polynomial of suitable degree n.

1.1 Task 01: Canonical Polynomials


1. Using the four types of polynomial in Table 1, show that n(n + 1)/2 multiplications and n additions are needed to compute Equation 1, where n is the degree of the polynomial. 2. Study the pseudo-code in Table 2 and discuss the Design of a solution algorithm for Equation 1 using a owchart.

Linear y = a0 + a1x x

Quadratic y = a0 + a1x + a2x2 x

Cubic y = a0 + a1x + a2x2 + a3x3

Quatic y = a0 + a1x + a2x2 + a3x3 + a4x4 x

Figure 1: Illustration of Polynomials

3. Write the programme to implement the algorithm you designed for Equation 1 using Plato Fortran and Octave.

Table 2: Algorithm for Canonical Polynomial Evaluation ____________________________________________________ START: " i => Index of the coefficient array " n => Degree of the polynomial " A[n] => Array element n " P => The result of the polynomial computations INTEGER i,n REAL A[n], X, P READ n READ A[i], i <- 0, n P = A[0] For i <- 1, n P <- P + A[i] * Xn NEXT i WRITE P END: ____________________________________________________

1.2 Task 02: Horners Polynomials


Another form of expressing Equation 1 is presented in Equation 3. Pn (x) = a0 + x(a1 + x(a2 + . . . , +x(an1 + x(an )))) Equation 3 is called the Horners or nested form of polynomial expression. 1. Using the four types of polynomial in Table 1, show that n multiplications and n additions are required to compute Equation 3, where n is the degree of the polynomial.. 2. Study the pseudo-code in Table 3 and discuss the Design of a solution algorithm for Equation 3 using a owchart. 3. Write the programme to implement the algorithm you designed for Equation 3 using Plato Fortran and Octave. (3)

Table 3: Algorithm for Horners Polynomial Evaluation ____________________________________________________ START: " i => Index of the coefficient array " n => Degree of the polynomial " A[n] => Array element n " P => The result of the polynomial computations INTEGER i,n READ n REAL A[n], X, P READ A[i], 0 <- 1,n P = A[n] For i <- 1, n P <- P*X + A[n-i] NEXT i WRITE P END: ____________________________________________________

1.3

Task 03: Root of Polynomials

Remember also that Equation 1 can be expressed as Equation 4. Pn (x) = (x x0 )(x x1 )(x x2 ), . . . , (x xn1 )(x xn ) or, in the short form
n

(4)

Pn (x) =
i=0

(x xi )

(5)

In Equation 4 and 5, x0 , x1 , . . . , xn are the roots of the polynomial. A root is a value which when you substitute into an equation, the result will be zero. This value corresponds to the point of stability or equilibrium of the system represented by the polynomial. Equation 5 requires n multiplications and n subtractions. Note that the computational load of the subtraction operation is more than that of addition, usually by a factor of 1.5. 1. Design the solution algorithm for Equation 4 using owchart and pseudo-code. 2. Write the programme to implement the algorithm you designed in 1. using Plato Fortran and Octave. Nota that, the programme you developed in Task 3 could be used to explore the root of the system modelled by Equation 1.

1.4 Task 04: Evaluating Function as Polynomials


As stated earlier, the models for most scientic and engineering problems can be expressed in the form of a polynomial. In fact programming languages implement Builtin subprogrames which are used to compute most functions encountered in scientic and engineering problem solving. Such functions are implemented using algorithms based on Taylor series, which are polynomials. 1. Study the functions in Table 4 (on page 7) and conrm their correctness. 2. Design an algorithm to implement each of the functions. Your algorithm should facilitates a computation that takes variable number of terms (i.e. different values of n) in the series. 3. Implement the solution algorithm you designed in 2. using Octave. 4. Study your programme by computing the values of x in the interval [0.0, ] with 2 step 0.15 for 3, 5, 10 and 15 terms of each equation. 5. Using the Octave built-in functions, repeat the experiment in 4. [Note that you do not know the number of terms used in the Octave built-in functions]. 6. Plot your results in 4. and 5. and discuss your observations. Note that the rst neglected term in each series is taken to be the error in the computation. Polynomials will work well on sufciently small intervals. When larger intervals are used, however, severe oscillation often appears, particularly when the degree of the polynomial is more that 4. For practical engineering problems therefore, polynomials of degree 4 or less is recommended. If the interval is long, it can be divided up into smaller intervals and separate polynomials used to represent each of the smaller pieces of intervals: (This approach is the basis of the Spline interpolation method).

Engineering Application

As stated in the previous sections, most scientic and engineering models can be represented using polynomials. Some general problems have been studied and the numerical methods for their theoretical solution using polynomials have been developed. Examples of such polynomials include [4]: Chebychev polynomials, Laguerre polynomials and Hermite polynomials. For example, the second-order differential equation is useful in modelling the behaviour of a number of dynamic systems commonly encountered in engineering. The Laguerre form of such equation is given by d2 y dy + (1 x) + ny = 0.0 (6) dx2 dx In Equation 6, y is the dependent variable and x is the independent variable and n is a positive integer. The solution to this problem is a set of polynomial, which are x

orthogonal. Six of the Laguerre polynomials, which solves the model in Equation 6, are as listed in Equations 7 through 12: L0 (x) = 1.0 L1 (x) = 1.0 x L2 (x) = L3 (x) = L4 (x) = 1 2 (x 4.0x + 2.0) 2! (7) (8) (9) (10) (11)

1 (x3 + 9x2 18.0x + 6.0) 3!

1 4 (x 16.0x3 + 72.0x2 96.0x + 24.0) 4!

L5 (x) =

1 (x5 + 25.0x4 200.0x3 + 600.0x2 600.0x + 120.0) 5!

(12)

2.1 Task 05: Application of Polynomials


1. Design the algorithm for computing the six Laguerre polynomial Equations. 2. Write a program to implement the algorithm in 1. and plot their values between x = 0.0 to x = 6.0 with step 0.5 using the Octave programming environment. 3. Discuss the accuracy of your results.

References
[1] J. G. Bronson. Algorithm Development and program design using C, volume 1. West Publishing Company, 1996. [2] U. Manber. Introduction to Algorithms: A creative approach, volume 1. AddisionWesley, 1989. [3] K. H. Rosen. Discrete Mathematics and Its Application, volume 5th . McGraw-Hill Higher Education, United States, 2003. [4] F. Scheid. Schaums Outline of Theory and Problems of Numerical Analysis. McGraw-Hill Publishing, New Delhi, 2nd edition, 2006.

Function ex

Table 4: Polynomial Representation of Some Functions Polynomial expression (n terms) x3 x4 x5 x2 1.0 + x + + + + ... = 2! 3! 4! 5!
n i=0

xi i!

cos x

1.0

x4 x6 x8 x10 x2 + + ... = 2! 4! 6! 8! 10!

(1)i
i=0

x2i 2i!

sin x

x3 x5 x7 x9 x11 + + ... = 3! 5! 7! 9! 11!

(1)i
i=0

x(2i1) (2i 1)!

sinh x

x+

x3 x5 x7 x9 x11 + + + + ... = 3! 5! 7! 9! 11!

n i=1

x(2i1) (2i 1)!

cosh x

1.0 +

x2 x4 x6 x8 x10 + + + + + ... = 2! 4! 6! 8! 10!

n i=1

x(2i) (2i)!

1.0 (1.0 x)

1.0 + x + x2 + x3 + x4 + x5 , . . . =
i=0

xi