Está en la página 1de 1

%Program for Lagrange's Interpolation.

%Academic Year:
%Roll No:
%Batch:
clc;
clear all;
n=input('Enter the number of data points in table: ');
x=zeros(10);
y=ones(10);
l=ones(10);
for i=1:n
fprintf('Enter the value of x(%d):',i);
x(i)=input('');
fprintf('Enter the corresponding value of y(%d):',i);
y(i)=input('');
end
yg=input('Enter the value for yg:');
xg=0;
for j=1:n
nu=1;
de=1;
for i=1:n
if (i~=j)
nu=nu*(yg-y(i));
de=de*(y(j)-y(i));
end
end
l(j)=nu/de;
xg=xg+l(j)*x(j);
end
fprintf('Value of y at y=%d is %f', yg, xg);
output:
Enter the number of data points in table: 4
Enter the value of x(1):0
Enter the corresponding value of y(1):0
Enter the value of x(2):1
Enter the corresponding value of y(2):1
Enter the value of x(3):2
Enter the corresponding value of y(3):7
Enter the value of x(4):3
Enter the corresponding value of y(4):25
Enter the value for yg:2
Value of y at y=2 is 1.716138>>
Solver:
>> x=[0 1 2 3];
>> y=[0 1 7 25];
>> interp1(y,x,2,'spline')
ans =
1.7161

También podría gustarte