[SOLVED] Math551 - lab07

30.00 $

Category:

Description

Rate this product

Goal: In many practical applications it is necessary to approximate a function f(x) given only a finite set of function values {(x1,f(x1)),(x2,f(x2)),…,(xn,f(xn))}. In this lab, you will learn how to do this first by fitting a polynomial through the given data points and then by using Matlab’s interp1 command. During the lab session, your lab instructor will teach you the necessary Matlab code to complete the assignment.

 

Tasks

Consider the following set of data points on the xy-plane:

(−2.1,2.2),(−1.3,5.1),(0.1,−1),(0.5,0.1),(1.4,0.6),(2.0,1.8).                                              (?)

You will need to find a polynomial of degree at most 5 that passes through these 6 points. (Does this polynomial exist? Is it unique?) Please, complete the following steps:

  1. Create two row vectors x and y, containing respectively the x and y coordinates of the data points in

(?).

Variables: x,y

  1. Assuming there exits a degree 5 polynomial which passes through the points in (?), write down the system of 6 linear equations representing this information, i.e. each equation in the system shows that one of the (x,y) pairs in (?) solves a certain degree 5 polynomial. (You don’t need to submit this task.)
  2. Create a variable holding the coefficient matrix A of the system in Task 2. Do not enter the matrix entries by hand. Instead, initialize a zero matrix of the appropriate dimension using zeros and then fill in the coefficients using a pair of for So your code will look something like this

n = length(x); A = zeros(n,n); for i=1:n for j=1:n

A(i,j) = … <—- this is the part you figure out end

end

Variables: A

  1. If you have completed the previous tasks correctly, you will have generated the coefficient matrix A for your linear system from Task 2. Notice that the constants from the right-hand side are just the entries in the row vector y. Solve the system using Matlab’s backslash operator. (You’ll need to use y’ as your constant vector, since y is a row vector.) Name the solution p (a good name for a variable holding polynomial coefficients). Variables: p
  2. At this point, it would be a good idea to compare the values of polyval(p,x) with the vector y to make sure you got the right answer. To do this, add the following commands to your script.

% error check err = max( abs( polyval(p,x) – y ) ); fprintf( ’max error in polyval = %e\n’, err );

Now, when you run your script, Matlab will report the largest absolute difference between the vector y and the vector polyval(p,x). Due to errors introduced internally by rounding, this value will almost certainly not be zero, but it should be a very small number like 4.884981e-15, which is Matlab’s representation of the number 4.884981 × 10−15. (This step helps you check your work. There are no required output variables.)

  1. Divide the interval [−2.1,2.0] into 100 equal intervals using the linspace command and store it to the variable x1 (x1 should have 101 entries). If you haven’t worked with linspace before, see if you can figure out what the following commands do: linspace(-1,3,2), linspace(-1,3,3) and linspace(-1,3,11). Variables: x1
  2. Evaluate the polynomial at the points of the array x1 and store the result to the variable y1. Variables: y1

In applications with many data points, polynomial fitting is often not practical for a number of reasons. In those cases, other methods of interpolation are used. There are several methods of interpolation available in Matlab. We will consider the most commonly used linear, cubic (piecewise cubic Hermite interpolation) and (cubic) spline interpolation. Your lab instructor will explain the difference between those three.

Matlab’s basic interpolation command is called interp1. Use the command doc interp1 to bring up its documentation and see if you can figure out how to use it. Don’t try to read every detail. It can help to scroll down to the examples and see the command in action.

  • Use interp1 to compute the values y2 of the polynomial at the points x1 with ‘linear’ option. Variables: y2
  1. Use interp1 to compute the values y3 of the polynomial at the points x1 with ‘cubic’ option. Variables: y3
  2. Use interp1 to compute the values y4 of the polynomial at the points x1 with ‘spline’ option. Variables: y4
  3. Plot y1, y2, y3, y4 as functions of x1 on the same graph using the plot Use black color for y1, blue for y2, green for y3 and red for y4. Mark the initial set of points stored in x and y on this graph using black circles. The result should look something like this:

Notice the differences in the results of using the various methods of interpolation.