% hw1sol(x,y,secenek) secenek 1den 3e yontemi belirler
% ornek: hw1sol([0 2 3],[7 11 28],1)

function sonuc = hw1sol(xdeger,ydeger,secenek)

uzunluk = length(xdeger);
x=[1 0];
sonuc=0;
if secenek==1
for i=1:uzunluk
L{i}=1;
end

for i=1:uzunluk
    for j=1:uzunluk
        if j~=i 
           pay = x-[0 xdeger(j)];
           payda = 1/(xdeger(i)-xdeger(j));
           bolum = pay*payda;
           L{i}= conv(L{i},bolum);
        end  
    end
end
for i=1:uzunluk
    sonuc = ydeger(i)*L{i} + sonuc ;
end
end

if secenek ==2
           mat(:,1)= xdeger;
           mat(:,2)= ydeger;
           k=2;
       sayi=uzunluk-1;
       for i=3:(3+uzunluk-1)
           sayac=1;
           for j=k:(k+sayi-1)
               i2=i-2;
               i1=i-1;
               mat(j,i)=((mat(j,i1)- mat(i2,i1))/(mat(j,sayac)-mat(i2,sayac)));
           end
           k=k+1;
           sayi=sayi-1;
           sayac=sayac+1;
       end
for i=1:uzunluk
    ai(i)=mat(i,i+1);
end
for i=1:uzunluk
M{i}=ai(i);
 end
for i=2:uzunluk
    for j=i-1:-1:1
        M{i}= conv( M{i} ,(x- [0 xdeger(j)]));
    end
end
max=length(M{1});
for i=1:uzunluk
 if length(M{i})> max 
   max=length(M{i});
end
for i=1:uzunluk
    if length(M{i})< max
  sayac=max- length(M{i});
        for j=1:sayac
         M{i}= [0 M{i}];
        end
    end
end


end

for i=1:uzunluk
 sonuc= sonuc + M{i};
end

end

if secenek ==3

uzunluk=uzunluk-1;
k=uzunluk;
for i=1:uzunluk
    for j=1:uzunluk
        neville{i,j}=0;
    end
end

for i=1:uzunluk
    for j=1:k
        if i==1
           neville{j,i}= (ydeger(j)*(x-[0 xdeger(j+1)])+ydeger(j+1)*([0 xdeger(j)]- x ))*(1/(xdeger(j)-xdeger(j+1)));
        else 
           neville{j,i}= (conv(neville{j,i-1},(x-[0 xdeger(i+j)]))+conv(neville{j+1,i-1},([0 xdeger(j)]-x)))*(1/(xdeger(j)-xdeger(i+j)));
        end
    end
    k=k-1;
end
sonuc=neville{1,i};
end
end

