Docsity

Approximating Nonlinear Equations with Polynomials & Lagrange Interpolation, Assignments of Computer Science

Toggle sidebar
bg1
CS 257 Homework 5
Assigned: March 15, 2005
Due: March 29, 2005
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:
•Manipulate polynomials in MATLAB.
•Find the Lagrange polynomials when given a nonlinear function.
ASSIGNMENT:
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:
•Your function should find the roots of the given polynomial, sort them in ascending
order, then remove the root that the input indexes.
•The first input is a MATLAB polynomial.
•The second input is an integer of the root to remove (where the number 1 corre-
sponds to the smallest root).
•As output, sortedFactorRemoval should return the new MATLAB polynomial.
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
5.
[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));
1
pf3

Partial preview of the text

Download Approximating Nonlinear Equations with Polynomials & Lagrange Interpolation and more Assignments Computer Science in PDF only on Docsity!

CS 257 Homework 5

Assigned: March 15, 2005

Due: March 29, 2005

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:

  • Manipulate polynomials in MATLAB.
  • Find the Lagrange polynomials when given a nonlinear function.

ASSIGNMENT:

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:

  • Your function should find the roots of the given polynomial, sort them in ascending order, then remove the root that the input indexes.
  • The first input is a MATLAB polynomial.
  • The second input is an integer of the root to remove (where the number 1 corre- sponds to the smallest root).
  • As output, sortedFactorRemoval should return the new MATLAB polynomial.

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).

  • The input is a row vector of indices.
  • The output should be a matrix where each row represents a Lagrange polynomial in the MATLAB polynomial format.

[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