You are on page 1of 21

NUMERICAL METHODS

LAB
Lagrange Interpolation

By Engr Ayaz Mahmood Khan


What is Lagrange Interpolation ??
• Lagrange interpolating polynomials are a particular form of
polynomials
• The method is based on creating a polynomial of degree n. The
degree depends on the number of points considered in the data set
so they should be n+1 points
• If data points are 5 then polynomial will be of degree 4

By Engr Ayaz Mahmood Khan


Basic Form
• Basic form of polynomial is given by :

By Engr Ayaz Mahmood Khan


Finding value of l

By Engr Ayaz Mahmood Khan


General form

General form after combining the above two:

By Engr Ayaz Mahmood Khan


EXAMPLE
• Certain corresponding values of x and y are (300,2.4771),
(304,2.4829),(305,2.4843) and (307,2.4871).
• Find y at xp=301 using Lagrange Interpolation

• Solution is 2.4786 at x=301

By Engr Ayaz Mahmood Khan


EXAMPLE
• For the table shown below find the value of Y at X=55 using
Lagrange Interpolation
X 0 20 40 60 80 100

Y 26 48.6 61.6 71.2 74.8 75.2

• Solution is 69.2440 at x=55

By Engr Ayaz Mahmood Khan


MATLAB Practical Problems 1
• How many times will the display command in the following script be
executed?
x = 3;
while (x < 8)
disp(‘I am learning?’)
x = x + 2.5;
end
MATLAB Practical Problems
• How many times will the display command in the following script be
executed?
• SOLUTION is 2 times
MATLAB Practical Problems 2
• B='MY NAME IS Ayaz Khan’
• Change the first name above and write it as Fraz
MATLAB Practical Problem 3
• Give the steps to solve the following system of linear equations using
MATLAB.
2x-3y+4z=5
y+4z+x=10
-2z+3x+4y=0
MATLAB Practical Problem 3 (Solution)
• A=[2 -3 4; 1 1 4; 3 4 -2];
• B=[5;10;0];
• X=inv(A)*B

• The Solution is X =

• -0.1351
• 1.2162
• 2.2297
MATLAB Practical Problem 3 (Solution)
• x is a new variable with the following command.

• x = [-10:-1:-15; -2:3];

• What is the Size of x ?


MATLAB Practical Problem 3 (Solution)
• x is a new variable with the following command.

• x = [-10:-1:-15; -2:3];

• The Size is 2 x 6

• Solution is
-10 -11 -12 -13 -14 -15
-2 -1 0 1 2 3
Difference between clc and clear all
• clc : Clears the screen

• clear all : deletes all variables from memory


Finding Errors in Code
• P =linespace(2,3)

• P[ 1, 2] = 4
Finding Errors in Code (Solution)
• P =linspace(2,3)

• P(1, 2) = 4
MATLAB Practical Problem 4
• Create a 6x6 matrix in the middle two rows, and the middle two
columns are 3's and the rest are 4's
MATLAB Practical Problem 4 Solution
• A=4*ones(6,6);
• A(:,3:4)=3;
• A(3:4,:)=3;
• disp (A)
Writing lines in MATLAB
• Type this is MATLAB
Writing lines in MATLAB (Solution)
• Type this is MATLAB

a=11;
b=pi;
X =[2 4 6 8];
Y=abs(X.*exp(a*X)-cos(b*X))

You might also like