clc
clear
% In The Name of Allah
% Interpolation using Gauus Central Differences
% Programmed by: WWW.chemical-eng.blog.ir 

N=input('Enter the number of X or Y Data = ');

for i=1:N

    x(i)= input('Enter the values of x = ');
end


x_des=input(' Enter the desired value of y = ');

fin=N;
n=fin;

h=x(2)-x(1);



if rem(N,2)==0
    N=N/2+1;
else
    N=N/2+0.5;
end

p=(x_des-x(N))/h;

for i=1:fin
    
    y(i)= input('Enter the corresponding values of y with respect to x = ');
end



for i= 1:n
    for j=1:(n)
        k_arr(i,j)=0;
    end
end


for i=1:n
    k_arr(i,1)=y(i);
end


i=1;
b=2;
while i<(fin+1)           

j=2;
c=1;

while j~=(n+1)           
    
    k_arr(c,b)=(k_arr(j,i)-k_arr(j-1,i));
    j=j+1;
    c=c+1;
    
    
end
i=i+1;
n=n-1;
b=b+1;

end 
fact(1)=1;
fact(2)=p;
fact(3)=(p*(p-1));
fact(4)=((p+1)*p*(p-1));
init=fact(4);
er=p-2;

if(fin>4)  
for i=5:fin
    fact(i)=init*er;
    init=fact(i);
    er=er-1;
end
end

for i=1:fin
    perm(i)=factorial(i);
end

ans=0;
c=1;
bck=N;
fig=N;
for i=1:(fin-1)
    ans=ans+(fact(i+1)*k_arr(fig,(i+1)))/perm(i);
    c=c+1;
 if c==2
     fig=fig-1;
     c=0;
 end
end
ans=ans+k_arr(bck,1)

