Sunteți pe pagina 1din 2

Numerical Methods with Computer Applications

Machine Problem No. 10

Polynomial Interpolation: Lagrange Polynomials

Instructions and Reminders:


*Show your complete and comprehensive solution.
*Include all relevant plots, graphs, derivations, concepts, discussions, etc. whenever possible.
*Include screenshots of MatLab or Octave computation (in the Command Window) and plots.
*Label the plots and graphs completely. Refer to tutorials and the MatLab Help menu.
*Write a document file containing your solution and corresponding explanation in the format
<CourseCode>_<SectionNumber>_<GroupNumber>_MP10.doc. No extra sheets (fly leaf, front page,
back page, etc.) nor folders should be used.
*Save all file deliverables in a folder with the label
<CourseCode>_<SectionNumber>_<GroupNumber>_MP10.

Write an m-file with a function handle as lagpoly_<SecNum>_<GroupNum>(fx,x0,x1) that


requires input anonymous function fx with respective limits of x0 to x1, to be interpolated into a
polynomial using 12 intervals. The output is the graph of Lagrange polynomials P1(x), P2(x), P3(x), and
P4(x), of first, second, third, and fourth degrees respectively, over the evaluated points of the given
function. The algorithm of the m-file should comply to the following conditions:
a. The m-file must be able to divide the intervals into 12 strips of equal length between the two
given inputted intervals.
b. The given function can be an input using the anonymous function form in MatLab. For
example, a given function f (x) = e – x – cosx, can be written as an input in the form
>> fx = @(x) exp(–x) – cos(x).
c. Use scatter() to place the evaluated function values f (x) of the equidistant points x on
the graph.
d. Each degree of Lagrange polynomial requires a set of points to produce the desired
polynomial before being interpolated on the graph. This part will require your geometric
reasoning in deciding which points would be used to produce the Lagrange polynomial on
each degree intended. Hint: The number of intervals, 12, is chosen solely to satisfy the
requirement for degrees one through four.
e. Each Lagrange polynomial function obtained should be drawn, using plot(), from x0 to
x1 in 0.01-sized intervals.
f. An invalid interval value input (x1 ≤ x0) should be prompted and the program is exited.
g. A constant function must also be accepted. For this type of given problem, only trapezoidal
method can be used as an exact solution. Use ones() to address the problem that might
incur in handling constant functions.

Using the m-file, solve for the Lagrange polynomials P1(x), P2(x), P3(x), and P4(x) of the following
functions within the respective given intervals:
1. f ( x ) = 8 x 2 e−2 x sin ( 3x ) 0 ≤ x ≤ 8
2. f ( x ) = 16 x3e−4 x cos 2 x 5 ≤ x ≤ 10
3. f ( x ) = 16 − 9e−3 x tanh 2 x 1 ≤ x ≤ 12
4. f ( x ) = 12 0 ≤ x ≤ 36
Capture screenshots of your Command Window depicting the result of using the m-file (see samples on
the following pages). Paste these screenshots along with the manual solution of the functions using the
same method on the report file document (use Equation Editor for proper presentation of solution).
Sample problem and output:

>> fx = @(x) cos(x);


>> x0 = 0.0; x1 = 1.2;
>> lagpoly(fx,x0,x1)
Lagrange Polynomial Interpolation
1.1
Actual Points
1 1st order
2nd order
3rd order
0.9
4th order

0.8
f(x)

0.7

0.6

0.5

0.4

0 0.2 0.4 0.6 0.8 1 1.2 1.4


x

>> fx = @(x) 10;


>> x0 = 0; x1 = 12;
>> lagpoly(fx,x0,x1)

Lagrange Polynomial Interpolation


10

10

10

10
f(x)

10

10 Actual Points
1st order
2nd order
10
3rd order
4th order
10
0 2 4 6 8 10 12
x

S-ar putea să vă placă și