Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Math 3040, Numerical Methods, Assignment 1: Interpolation

Contents

1   Piecewise linear interpolation 1

2   Assignment requirements 4

The aim of this assignment is to develop a    ATLAB function that imple- ments piecewise linear interpolation, test it, and then apply it to some real data.

1    Piecewise linear interpolation

Consider some function f(x) on an interval [a,b]. Dene the points xj, for j = 1, . . . ,N , such that a = x1  < x2 < ··· < xN = b and let fj = f(xj) . Note that the xj may be unevenly spaced.

In this assignment, you are to apply linear interpolation locally to the pairs of data points (xj 1 ,fj 1 ) and (xj,fj) for j = 1, 2, . . . ,N . ?? shows how the local linear interpolant ts to pairs of data points obained from some function f(x).

1. Write down the linear Lagrange interpolating polynomial that passes through the pair of data points (xj 1 ,fj 1 ) and (xj,fj).

2. Write  a     ATLAB function  called  linInterp .m that implements piecewise linear interpolation.  Your function must use the inter- face:

function  p  =  linInterp(x,  xj,  fj)

where x is  a vector of points  at which the interpolant is to be evaluated, xj is a vector containing the data points xj  and fj is a vector containing the data values fj .  The output vector p must be the same size as x and contain the values of the piecewise linear interpolant p1 (x) for each element of the vector x.

You should assume that the values in xj are arranged in ascending order and that the lengths of the vectors xj and fj are equal, al- though you should allow them to be either row or columns vectors (one could be a row vector and the other a column vector). Do not assume that the data points are equi-spaced.  The output p must be the same size as input x (so, both are row vectors or both are column vectors).

Include linInterp in your report and also upload it to     ATLAB

Grader.

Hint:   The rst thing to do when you develop a new piece of code is to test that it works on a case or cases where you know the correct answer.  Thus rst test your code on something like the function f(x) = 1−2x and sampling it at, say, just ve evenly spaced points. Then try a slightly more complicated problem, and so on until you are ready to test your code in     ATLAB Grader.

For full marks:   your code must include appropriate documen- tation, must follow good style conventions, and must be fully vec- torised (without any for loops).

3. Use the Polynomial Interpolation Error theorem to nd the mini- mum number Nmin of equally-spaced data points that are needed to approximate the function f(x) = cos2 x on the interval 0 ≤ x ≤ 2π to an accuracy of 10 2 . Show all your working.

4. Write a script called linTest .m that samples the function f(x) = cos2 x at N equi-spaced data points over the interval [0, 2π], with rst data point x0  = 0 and last data point xN  = 2π .  Then your script must call linInterp to evaluate the piecewise linear inter- polant p1 (x) over the interval [0, 2π].

Choose the number of data points N such that the interpolation error is no more than 10 2  for all x ∈ [0, 2π].  You may use Nmin calculated in the previous question,  or if you failed to nd this number,  use your code to determine a suitable number of data points.

Your script should create two plots.   One plot should be of the interpolant function and data points. The other plot should be the absolute error  |ϵ1 (x)| for 0 ≤ x ≤ 2π .  Check that this absolute error is indeed bounded above by 10 2 .

In your assignment, present your    ATLAB code and your two plots. Ensure your code has adequate documentation and good style and that your plots are well presented.

5. We will now use linInterp .m for a practical problem.

An engineer must design a walkway over a hilly area. The walkway is to be constructed from many at rectangular pieces which are supported by xed-length posts, as shown in Figure 1.

The main aim is to determine appropriate spacing for the posts which support the walkway.  We do not want to damage the hills, so the posts must spaced such that the walkway never touches the

x

Figure 1:  A hill (solid black) with a walkway over the hill (red).  This walkway is supported by posts (blue) of a xed length so that the ends of the walkway pieces are always a xed height above the hill (dashed black).

hill. Also, it is more ecient to have longer walkway pieces, so they should not be placed very close together.

A survey of the hill has found that it is well modelled by g(x) = x cos2 x

over the dimensionless distance x ∈ [0, 3T/2].  The dimensionless height of each post is 10 3 .

(a) Assuming that the posts are equi-spaced, calculate an appro- priate spacing between the posts. How many walkway pieces are required? Show all your working.

Hint│ to nd minima or maxima for the second derivative of the function being interpolated, use a graphical solution (i.e., plot the function).

(b) Write  a     ATLAB  script  called ✇a│❦✇ay .m that  models  the walkway and creates two plots.

When generating the two plots, use the post spacing deter- mined in the previous question, or determine it numerically. The rst plot should show a small portion of the walkway and hill, and clearly indicate that the walkway is always above the hill (choose a domain for x which you think nicely illustrates this). The second plot should show the size of the gap between the walkway and hill for all x ∈ [0, 3T/2] (this should always be positive).

In your assignment, present your    ATLAB code and your two plots.

2    Assignment requirements

1. You must submit your function linInterp to     AT  AB Grader as

follows.

(a) Follow the     AT  AB Grader link provided on learnOnline for

this Assignment.

(b)  Copy and paste your code into the text box provided.

(c) Do not use functions like clear or any random number gener- ator in your code as these may conict with the testing proce- dure.

(d) Assess your code by clicking on Submit". This will grade your code and provide some feedback.

(e) You can revise and resubmit your function as often as you like, up until the deadline.  Your nal     AT  AB Grader mark will be part of the assessment for this assignment.

(f) You do not have to submit linTest .m or walkway .m to    AT-

AB Grader.

2. You must also submit a written report to learnOnline. This report must answer each question and must be a single PDF le. Where relevant, this report will include:

(a) relevant scripts and functions  (including code submitted to AT  AB Grader as well as any other code),

(b) numeric output and/or graphs,

(c)  any supporting analysis, and

(d)  answers to any questions.

These answers must be professionally presented in a readable and informative manner.

❼ Graphs and tables must be properly labelled. Graphs must be

of high quality, as provided by a vector graphics format (e.g., pdf, eps), not a bitmap format (e.g., png, jpg).

❼ All scripts and functions must be properly documented and and set out using the conventions established in this course. Present each    AT  AB script or function i♥ f✉lldo not break them up and have dierent parts scattered through your re- port.   Do not present your scripts and functions as bitmap

images as these are often hard to read.         Haphazard and illegible work will not be marked.

3. You may discuss the assignment questions with other students, but the work you submit must be your own and not copied from some- one else.