Docsity

Numerical Analysis Assignment: Polynomial Evaluation and Deflation - Prof. Victor E. Ginti, Lab Reports of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
CHE/COSC/MATH 4340-01 Numerical Analysis
Computer Laboratory Assignment 02
Due Date: Thursday, 01/29/09
Problem 1 [Evaluating Polynomial]
Consider a polynomial function pn(x)of degree nwhose form is
p(x) = a0+a1x+a2x2+a3x3+···+anxn=
n
∑
i=0aixi
.
where ai,i=0,1,2,···,nare the coefficients. A naive way of numerically computing the polynomial
value at a point rcan be mathematically written as
p(r) =
n
∑
i=0ai
i
∏
j=1x,
where the symbol ∏denotes product/multiplication. The algorithm below describes this procedure.
Algorithm 1 A Naive Polynomial Evaluation
Set rand val =a0
for i=1 to ndo
prod =1
for j=1 to ido
Compute prod =prod ∗r.
end for
Compute val =val +ai∗prod.
end for
Return val.
A more efficient way of evaluating polynomial is using the Horner’s algorithm. This algorithm is based
on a nested multiplication:
p(x) = a0+x(a1+x(a2+···+x(an−1+x(an)) ···)).
The procedure is described in the following algorithm
Algorithm 2 Horner’s Algorithm
Set rand val =an
for i=1 to ndo
Compute val =an−i+val ∗r.
end for
Return val.
Write MATLAB functions that implement these algorithms. Your MATLAB function for Algorithm 1
should look like
1
pf3
pf4

Partial preview of the text

Download Numerical Analysis Assignment: Polynomial Evaluation and Deflation - Prof. Victor E. Ginti and more Lab Reports Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

CHE/COSC/MATH 4340-01 Numerical Analysis

Computer Laboratory Assignment 02

Due Date: Thursday, 01/29/

Problem 1 [Evaluating Polynomial]

Consider a polynomial function pn ( x ) of degree n whose form is

p ( x ) = a 0 + a 1 x + a 2 x^2 + a 3 x^3 + · · · + anxn^ =

n

i = 0

aixi.

where ai , i = 0 , 1 , 2 , · · · , n are the coefficients. A naive way of numerically computing the polynomial value at a point r can be mathematically written as

p ( r ) =

n

i = 0

ai

i

j = 1

x

where the symbol (^) ∏ denotes product/multiplication. The algorithm below describes this procedure.

Algorithm 1 A Naive Polynomial Evaluation Set r and val = a 0 for i = 1 to n do prod = 1 for j = 1 to i do Compute prod = prod ∗ r. end for Compute val = val + ai ∗ prod. end for Return val.

A more efficient way of evaluating polynomial is using the Horner’s algorithm. This algorithm is based on a nested multiplication :

p ( x ) = a 0 + x ( a 1 + x ( a 2 + · · · + x ( an − 1 + x ( an )) · · · )).

The procedure is described in the following algorithm

Algorithm 2 Horner’s Algorithm Set r and val = an for i = 1 to n do Compute val = an − i + val ∗ r. end for Return val.

Write MATLAB functions that implement these algorithms. Your MATLAB function for Algorithm 1 should look like

function val = evaluatepolynomial(r, coeff, n) % % Syntax: val = evaluatepolynomial(r,coeff,n) % This function is to compute the value of a polynomial of degree n % at point r using standard/naive way. It returns the value to the % variable val. The variable coeff is a vector of dimension (n+1) % whose entries are the polynomial coefficients. %

Your MATLAB function for Algorithm 2 should look like

function val = myhorner(r,coeff,n) % % Syntax: val = myhorner(r,coeff,n) % This function is to compute the value of a polynomial of degree n % at point r using Horner’s algorithm. It returns the value to the % variable val. The variable coeff is a vector of dimension (n+1) % whose entries are the polynomial coefficients. %

WARNING: Keep in mind that in MATLAB index of a vector starts at 1 (and NOT 0). For exam- ple, MATLAB will complain if your code executes coeff(0). Thus, you have to modify the above algorithms accordingly to avoid accessing index 0.

Test your codes by computing a polynomial of degree 10^4 whose coefficients are ai = i + 1, for i = 0 , 1 , 2 , · · · , 104 at point r = 0 .1. Two things that you need to report:

  • The value p ( 0. 1 ). Make sure that both algorithms give the same value.
  • Elapsed time for each execution. We would like to see that Horner’s algorithm is in fact more efficient than the naive approach. To measure the elapsed time in MATLAB do the following: tic, val = myhorner(r,coeff,n), toc and tic, val = evaluatepolynomial(r,coeff,n), toc.

Test your code by computing Taylor coefficients’ of a polynomial of degree 100 whose coefficients are ai = i , for i = 0 , 1 , 2 , · · · , 100. Plot the resulting Taylor coefficients’ by executing plot(taylorcoeff).