Numerical Computing, Spring 2024
Homework Assignment 4
Due Date: 05.03.2024, 11.59pm
This question concerns polynomial interpolation using the Lagrange formula as well as cubic interpolation.
a) Write two Matlab functions to implement the barycentric version of the Lagrange interpolation algorithm given in AG, Chap. 11, p. 434. The first function, baryConstruct, should compute the barycentric weights for the n+1 interpolation data points (xi, yi ), i = 0, … , n. The second function, baryEval, should evaluate the resulting polynomial, which has degree n, on a fine grid of points zj, j = 0, … , N where N is much larger than n. Be sure to check whether zj equals any of the interpolation points xi before applying the evaluation formula, as otherwise the result will be NaN; in this case, simply set the result to yi
b) Test your code on the case that the xi are the 11 equally spaced points from -1 to 1 (including the end points) and yi = f(xi ), where f is the Runge function (AG Example 11.6, p.445) defined by
and the zj are 101 equally spaced points from -1 to 1 (again including the end points). Plot both the interpolation points (xi, yi ) using, say, plot(x,y,’r*’) and the polynomial evaluated on the zj with, say, plot (z,pz,’ .’) or plot(z,pz,’-‘) in the same figure, as well as the Runge function itself evaluated on the zj . You can use legend to show which plots are which. Also, in the same figure, plot the cubic spline interpolating the same 11 interpolation points, obtained using MATLAB’s spline function, called with sz=spline(x,y,z) which uses the default not-a-knot conditions(see AG p.469).
c) Finally, in a new figure, plot the Runge function again and call the two-input-argument variant of spline on the same (x, y) data like this: pp=spline(x,y). The resulting field pp.coefs gives you the cubic spline coefficients for all 10 cubic polynomials making up the spline. Recalling that these are defined with the origin shifted by the relevant xi , using polyval, plot each of these cubics on all the zj points from -1 to 1, each in a different color. Matlab provides only 7 colors but you can generate your own by using Color [r g b].
版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。