CS412 Spring Semester 2011 Homework Assignment 3 Due Thursday March 10th 2011 in class Sum of all problems 120 Maximum possible score 100 1 40 In this problem you will write a MATLAB function that computes the coefficients of a cubic Lagrange polynomial You will also be introduced to the MATLAB functions poly polyval and error Consider N 1 values along the x axis denoted by x0 x1 x2 xN 1 xN Each of the Lagrange polynomials l0 x l1 x lN x is a polynomial of degree N defined in such a way that it satisfies the following property li xj 1 0 j i j 6 i 1 In class we derived a formula for each li x as follows Q x x0 x xi 1 x xi 1 x xN j6 i x xj Q li x xi x0 xi xi 1 xi xi 1 xi xN j6 i xi xj For this problem you must generate each li in the more standard form li x a3 x3 a2 x2 a1 x a0 Write a MATLAB function lagrange cubic x0 x1 x2 x3 k which returns a row vector p a3 a2 a1 a0 with the coefficients of the cubic Lagrange polynomial lk x defined over the points x0 x1 x2 x3 If the parameter k is not an integer between 0 and 3 the function should abort with the error message Index out of bounds using the MATLAB command error Turn in your code and also test your implementation by using x0 1 x1 2 x2 3 x3 5 and report the coefficients you generated for k 0 1 2 3 Hint Each Q li can be expressed compactly as li x qi x qi xi where qi x j6 i x xj The MATLAB function poly u takes as argument a vector u u1 u2 um and returns another vector c cm c1 c0 with the coefficients of the polynomial cm xm c1 x c0 x u1 x u2 x um You can use function poly to generate the coefficients of qi x defined above Once these coefficients have been computed and stored in a vector say c the function polyval c w can be used to evaluate this polynomial at an arbitrary point w Thus by calling polyval c xi you can also compute the quantity qi xi 1 2 40 Using the function lagrange cubic from Problem 1 write a function lagrange interpolation x y which takes the following arguments x is a row vector containing the 4 values x x0 x1 x2 x3 y is a row vector containing the 4 values y y0 y1 y2 y3 This function should implement the Lagrange interpolation method to construct a cubic polynomial P x a3 x3 a2 x2 a1 x a0 which interpolates the four data points x0 y0 x1 y1 x2 y2 and x3 y3 Function lagrange interpolation should return a row vector p a3 a2 a1 a0 containing the coefficients of the interpolant P x Turn in your code and test your implementation by computing the coefficients of the cubic polynomial that interpolates the four data points 1 10 0 4 2 2 3 14 Additionally the result of your function lagrange interpolation x y should be identical to the MATLAB built in function polyfit x y 3 which performs the same task but using the Vandermonde matrix approach Check that the two methods produce the same result for the points given 3 40 In this last problem you will construct a piecewise cubic polynomial function that interpolates the N data points x1 y1 x1 y1 xN yN In each subinterval Ik xk xk 1 we define our interpolant as a cubic k k k k polynomial sk x a3 x3 a2 x2 a1 x a0 The cubic polynomials sk are constructed such that For k 2 3 n 2 sk x should interpolate points xk 1 yk 1 xk yk xk 1 yk 1 and xk 2 yk 2 s1 x should interpolate x1 y1 x2 y2 x3 y3 and x4 y4 sN 1 x should interpolate xN 3 yN 3 xN 2 yN 2 xN 1 yN 1 and xN yN Implement a function piecewise cubic x y with arguments x is a row vector containing the N values x x1 x2 xN y is a row vector containing the N values y y1 y2 yN The function should return a N 1 4 matrix M with the coefficient of each sk on the respective row M 1 a3 2 a3 N 1 a3 1 a2 2 a2 N 1 a2 2 1 a1 2 a1 N 1 a1 1 a0 2 a0 N 1 a0 Turn in your code and also test your implementation by running the commands on the MATLAB script file provided to you in http pages cs wisc edu cs412 1 hw hw3 m Note You are free to use either function lagrange interpolation x y from Problem 2 or the built in function polyfit x y 3 in your implementation 3
View Full Document