Programming Assignment I - Numerical Analysis | MATH 128A, Assignments of Mathematical Methods for Numerical Analysis and Optimization

Material Type: Assignment; Class: Numerical Analysis; Subject: Mathematics; University: University of California - Berkeley; Term: Summer 2009;

Typology: Assignments

Pre 2010

Uploaded on 10/01/2009

koofers-user-eb0
koofers-user-eb0 🇺🇸

5

(1)

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATH 128A, SUMMER 2009: PROGRAMMING ASSIGNMENT 1
In this assignment, we will use Newton’s divided difference algorithm to approximate the function cos(x)
by a Lagrange polynomial in divided-difference form.
There are three steps involved. The first step is to write a general function that computes the divided-
difference coefficients ak=f[x0, . . . , xk] of the Lagrange interpolating polynomial, given as input a vector
containing x0, . . . , xn. The second step will be to evaluate the Lagrange interpolating polynomial (in its
divided-difference form Pn(x) = Pn
k=0 f[x0,...xk]Qk
j=0(xxj)) at a few sample points. The third step
will be to use the MATLAB plot function to graph the interpolating polynomial for the Cosine function,
together with the Cosine function on the same set of axes over an appropriately-chosen interval.
We begin by giving some example code. The following recursive MATLAB function will evaluate a0+
a1(xx0) + · · · +an(xx0). . . (xxn1), given as input vectors containing a0, . . . , anand x0, . . . , xn.
When n > 0, it works by evaluating
a0+ (xx0)[a1+a2(xx1) + · · · +an(xx1). . . (xxn1)].
function y=newton_eval(x,ai,xi)
if length(xi)==1
y = ai(1);
else
y = ai(1) + (x-xi(1)) .* newton_eval(x,ai(2:end),xi(2:end));
end
1. Write a recursive function y=divided_difference(f, xi), which computes f[xi(1),...,xi(end)]
directly from its definition:
f[x0] = f(x0)
f[x0, . . . , xn] = f[x1,. . . , xn]f[x0, . . . , xn1]
xnx0
,if n > 0
Note: The algorithm given in section 3.2 is more efficient.
2. Create a set of 6 equally-spaced nodes in the interval [0,10]:
xi = linspace(0, 10, 6);
Calculate a vector ai, where ai(k) =f[xi(1),...,xi(k)].
Use newton_eval to evaluate the interpolating polynomial at x=π/3, x=π/2, and x=π. Give
these estimates, and the absolute error, accurate to 6 significant digits.
3. Plot y= cos(x) and the interpolating polynomial y=P(x) on the same set of axes:
xdata = linspace(0, 10);
ydata1 = newton_eval(xdata, ai, xi);
ydata2 = cos(xdata);
plot(xdata, ydata1, xdata, ydata2)
Your report should contain your code for divided_difference, the MATLAB commands you used, the
numerical results from problem 2, and the graph from problem 3.
Date: Due Monday 7/20.
1

Partial preview of the text

Download Programming Assignment I - Numerical Analysis | MATH 128A and more Assignments Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

MATH 128A, SUMMER 2009: PROGRAMMING ASSIGNMENT 1

In this assignment, we will use Newton’s divided difference algorithm to approximate the function cos(x) by a Lagrange polynomial in divided-difference form. There are three steps involved. The first step is to write a general function that computes the divided- difference coefficients ak = f [x 0 ,... , xk] of the Lagrange interpolating polynomial, given as input a vector containing x 0 ,... , xn. The second step will be to evaluate the Lagrange interpolating polynomial (in its divided-difference form Pn(x) =

∑n k=0 f^ [x^0 ,... xk]^

∏k j=0(x^ −^ xj^ )) at a few sample points.^ The third step will be to use the MATLAB plot function to graph the interpolating polynomial for the Cosine function, together with the Cosine function on the same set of axes over an appropriately-chosen interval. We begin by giving some example code. The following recursive MATLAB function will evaluate a 0 + a 1 (x − x 0 ) + · · · + an(x − x 0 )... (x − xn− 1 ), given as input vectors containing a 0 ,... , an and x 0 ,... , xn. When n > 0, it works by evaluating a 0 + (x − x 0 )[a 1 + a 2 (x − x 1 ) + · · · + an(x − x 1 )... (x − xn− 1 )]. function y=newton_eval(x,ai,xi) if length(xi)== y = ai(1); else y = ai(1) + (x-xi(1)) .* newton_eval(x,ai(2:end),xi(2:end)); end

  1. Write a recursive function y=divided_difference(f, xi), which computes f [xi(1),... , xi(end)] directly from its definition: f [x 0 ] = f (x 0 )

f [x 0 ,... , xn] =

f [x 1 ,... , xn] − f [x 0 ,... , xn− 1 ] xn − x 0 , if n > 0

Note: The algorithm given in section 3.2 is more efficient.

  1. Create a set of 6 equally-spaced nodes in the interval [0, 10]: xi = linspace(0, 10, 6); Calculate a vector ai, where ai(k) = f [xi(1),... , xi(k)]. Use newton_eval to evaluate the interpolating polynomial at x = π/3, x = π/2, and x = π. Give these estimates, and the absolute error, accurate to 6 significant digits.
  2. Plot y = cos(x) and the interpolating polynomial y = P (x) on the same set of axes: xdata = linspace(0, 10); ydata1 = newton_eval(xdata, ai, xi); ydata2 = cos(xdata); plot(xdata, ydata1, xdata, ydata2) Your report should contain your code for divided_difference, the MATLAB commands you used, the numerical results from problem 2, and the graph from problem 3.

Date: Due Monday 7/20. 1