


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Computer Laboratory Assignment 02
Due Date: Thursday, 01/29/
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:
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).