

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!


The purpose of this assignment is to reinforce your understanding of polynomials in MAT- LAB as well as understand how polynomials can be used to approximate nonlinear equations. Upon completion of this assignment, you should be able to:
We want to integrate the function sin(πx) with MATLAB. To do this, we will find the interpolating polynomial that approximates sin(πx) using Lagrange polynomials. This as- signment has been divided into 3 tasks to accomplish this goal.
Step 1: Write a function called sortedFactorRemoval which adheres to the following require- ments:
You can assume the root to remove will be less than or equal to the degree of the polynomial. As an example, if a polynomial with roots at -1, 3, 5 and 7 is passed to your function, and the index is 4, your function should ouput the polynomial with roots at -1, 3, and
[6 points]
function R = sortedFactorRemoval(polynom, index) % this function finds the roots of polynomial poly and sorts them % then removes the root indexed by index
R = sort(roots(polynom)); helper = ones(size(R)); helper(index)=0; R = poly(R(helper==1));
Step 2: Write a function called genLagrange that generates the Lagrange polynomials for some (given) indices for the function sin(πx).
[10 points]
function P = genLagrange(x) % x is the list of elements from which to generate the polynomial % this function avoids using sortedFactorRemoval in case a student % did not program that function
sz = size(x); N = sz(2); Li = zeros(sz(2),sz(2)); P = zeros(sz(2),sz(2));
if N > 1 % take care of the first element i = 1; Li(i,:) = poly(x(2:N)); Li(i,:) = Li(i,:)/polyval(Li(i,:),x(i));
% do all middle elements for i = 2:N- Li(i,:) = poly([x(1:i-1),x(i+1:N)]); Li(i,:) = Li(i,:)/polyval(Li(i,:),x(i)); end
% do the final element i = N; Li(i,:) = poly(x(1:i-1)); Li(i,:) = Li(i,:)/polyval(Li(i,:),x(i));
% now set up the polynomials y = sin(pix); %FUNCTION for i = 1:N P(i,:) = y(i)Li(i,:); end end
TO BE TURNED IN DURING CLASS (or to the TA office): Plot each Lagrange polynomial as well as sin(πx) on the same graph. Also on this graph, mark with an astericks (*) the points where the interpolant is sampled. For your