0% found this document useful (0 votes)
1 views5 pages

LagrangePracTut25

The document provides a practical guide for implementing the Lagrange Interpolating Polynomial in Matlab, including data handling, interpolation of points, and plotting results. It also outlines a practical assignment in Excel, requiring the creation of data sets, calculations of interpolation values, and error analysis. Additionally, it includes tutorial exercises to reinforce the concepts learned in the practical sessions.

Uploaded by

nideeneo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

LagrangePracTut25

The document provides a practical guide for implementing the Lagrange Interpolating Polynomial in Matlab, including data handling, interpolation of points, and plotting results. It also outlines a practical assignment in Excel, requiring the creation of data sets, calculations of interpolation values, and error analysis. Additionally, it includes tutorial exercises to reinforce the concepts learned in the practical sessions.

Uploaded by

nideeneo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Numerical Methods 2

Practical 1: Lagrange Interpolating Polynomial (Matlab part)

MAPV211 (2025)

Consider the MatLab Program below as discussed in practical session


%program to illustrate Lagrange Interpolating polynomial
clc

%data file handling


fileS = input('Enter name of Excel spreadsheet contianing data set : ','s');
fid = fopen(fileS,'r'); %r open file in read only mode
B = readmatrix(fileS);
fclose(fid);
[r c] = size(B);

%correct oriontation of matrix


if c>r %horizontal configuration
disp('Transpose data in file');
B = transpose(B);
n=c;
else %vertical configuration
n=r;
end

% interpolate a single point


tS = sprintf('Enter single point on domain [%2.4f, %2.4f] to interpolate : ',B(1,1),B(n,1));
t = input(tS);
Pnt = InterPol(B,t,n);
%interpolate all points on domain
M = 51;
x = linspace(B(1,1),B(r,1),M);
y = zeros(M,1);
for q = 1:M
y(q) = InterPol(B,x(q),n);
end
plot(B(:,1),B(:,2),'m^',x,y,'b-',t,Pnt,'r*');
legend('Data Set','Lagrange Polynomial','Interpolate point t','Location','best');
title('Lagrange Interpolation');
ylabel('Functional and polynomial values');
xlabel('Independent variable x');

%this section maybe placed in separate function script file


%here it is a sub function to the program and other programs will not be able to make use of it
function Pnx = InterPol(Mat,d,N)
sum = 0;
for t = 1:N
prod = 1;
for k = 1:N
if (k~=t)
prod = prod*(d-Mat(k,1))/(Mat(t,1)-Mat(k,1));
end
end
sum = sum + prod*Mat(t,2);
end
Pnx = sum;
end
Output to program:

Lagrange Interpolation
10

Data Set
Functional and polynomial values

4 Lagrange Polynomial

Interpolate point t

-2
-1.5 -1 -0.5 0 0.5 1 1.5 2

Independent variable x

The following data set was used obtained from function 5tanh(2x+0.4)+3:

Note: hyperbolic tan (tanh) function not tan function

-1.2 -1.82014
-0.4 1.100255
0.9 7.878716
1.5 7.988875
2 7.998493

Type program in Matlab, create data file with 5 paired data and run the program
to generate the results.
Practical in Excel (for submission)

Due Friday 28 February 2025 at 10h00

Data Set
j x f(x)
0 1 0.357278
1 4 0.208575
2 6 0.194573

1) Create the following in Excel: (See template)


a. Vertical vector containing x point on domain [1,6] in steps of 0.1.
b. Calculate 𝐿𝐿0 (𝑥𝑥), 𝐿𝐿1 (𝑥𝑥), 𝐿𝐿2 (𝑥𝑥) and 𝑃𝑃2 (𝑥𝑥) in separate columns for each
point in a)
c. Calculate abs error and relative error for each point in a)
d. Generate a graph illustrating the weightings in a).
2) Already created for you points t1 and t2 with their respective functional
values.
a. Calculate 𝐿𝐿0 (𝑥𝑥), 𝐿𝐿1 (𝑥𝑥), 𝐿𝐿2 (𝑥𝑥) and 𝑃𝑃2 (𝑥𝑥) for t1 and t2
b. Calculate 𝑓𝑓(𝑥𝑥) − 𝑃𝑃𝑛𝑛 (𝑥𝑥)
c. Calculate 𝑆𝑆 = (𝑓𝑓(𝑥𝑥) − 𝑃𝑃𝑛𝑛 (𝑥𝑥))/(∏𝑛𝑛𝑗𝑗=0(𝑥𝑥 − 𝑥𝑥𝑗𝑗 )
d. Compute optimization parameter 𝜇𝜇
e. Is a correction needed to this parameter? (Generalized Rolle’s
Theorem requirement 𝜇𝜇 ∈ [𝑥𝑥0 , 𝑥𝑥𝑛𝑛 ]). If so determine correction factor
and final 𝜇𝜇.
f. Determine equation of 𝑓𝑓 (𝑛𝑛+1) (𝑥𝑥)
g. Show by means of calculating terms that
𝑛𝑛
(𝑛𝑛+1)
𝑓𝑓(𝑥𝑥) − 𝑃𝑃𝑛𝑛 (𝑥𝑥) = 𝑅𝑅𝑛𝑛 (𝜇𝜇) = [𝑓𝑓 (𝜇𝜇) ��𝑥𝑥 − 𝑥𝑥𝑗𝑗 �]/(𝑛𝑛 + 1)!
𝑗𝑗=0
h. Generate graph illustrating the original function, polynomial, data
points and interpolated points t1 and t2.

Submit Excel spreadsheet to Moodle for assessment purposes.

Optional work: Extend your spreadsheet and perform calculations for the data
set of the Matlab section. (Note 5 paired points thus 𝐿𝐿0 , 𝐿𝐿1 , 𝐿𝐿2 , 𝐿𝐿3 and 𝐿𝐿4 )
Tutorial( Tuesday)

Consider the following data set:

Data Set
j x f(x)
0 1 0.357278
1 4 0.208575
2 6 0.194573

• Construct and determine 𝐿𝐿0 , 𝐿𝐿1 , 𝐿𝐿2 and 𝑃𝑃2 (3.1)


1
• If 𝑓𝑓(𝑥𝑥) = �9� sin(3𝑥𝑥 − 0.4) + 0.3 determine
o Absolute error and Relative error
o 𝑓𝑓(𝑥𝑥) − 𝑃𝑃𝑛𝑛 (𝑥𝑥)
o 𝑆𝑆.
o 𝜇𝜇
o Correction for 𝜇𝜇
o 𝑓𝑓 (𝑛𝑛+1) (𝜇𝜇)
• Repeat calculations for the case 𝑥𝑥 = 5.45

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy