-
Notifications
You must be signed in to change notification settings - Fork 0
/
lagrange_interpolation.m
40 lines (35 loc) · 1.16 KB
/
lagrange_interpolation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
%Lagrange's interpolation
%clear all
% Interpolating data by Lagrange's method
%input
function yp = LAGRANGE_S(n, x, y, xp)
%main program
L = zeros(n);
for i =1:n
L(i)=1;
for j =1:n
if (i~=j)
L(i)= L(i)*(xp-x(j))/(x(i)-x(j));
end
end
end
yp=y*L';
%ouput
%fprintf('the required value of y at xp is %f',yp)
% Note: To see the data with multipliers use the follwoing 2 liines
%fprintf('x y L y.*L\n')
table = [x;y;L; y.*L]';
%---------------------------------------------------------------------
% OUTPUT 1
% enter the no. of data points = 5
% enter the x values as a row matrix [1 2 3 4 5]
% enter the y values as a row matrix [2 4 10 15 20]
% enter the x value for which y is to be evaluated = 1.5
% the required value of y at xp is 1.953125>>
%---------------------------------------------------------------------
% OUTPUT 1
% enter the no. of data points = 5
% enter the x values as a row matrix [1 2 3 4 5]
% enter the y values as a row matrix [2 4 10 15 20]
% enter the x value for which y is to be evaluated = 3.5
% the required value of y at xp is 12.703125>>