Wednesday, May 9, 2012

Newtons Forward Interpolation Using Matlab


function fp=forward_interpolation(x,y,p)
n=length(x);
for i=1:n
    diff(i,1)=y(i);
end
for j=2:n
    for i=1:n-j+1
        diff(i,j)=diff(i+1,j-1)-diff(i,j-1);
    end
end
answer=y(1);
h=x(2)-x(1);
s=(p-x(1))/h;
for i=1:n-1
    term=1;
    for j=1:i
        term=term*(s+j-1)/j;
    end
    answer=answer+term*diff(1,i+1);
end
fp=answer;

Arguments sent to the function are:- lagrange(xdataset,ydataset,interpolating point)

2 comments:

  1. code is not giving correct answer

    ReplyDelete
    Replies
    1. clc;
      clear all;
      close all;
      a=[8 10 12 14 16];
      b=[1000 1900 3250 5400 8950];
      n=length(a);
      x=1;
      d(:,1)=b';
      for j=2:n
      for i=j:n
      d(i,j)=(d(i-1,j-1)-d(i,j-1))/(a(i-j+1)-a(i));
      end
      end
      c=diag(d)';
      df(1)=1;
      y(1)=c(1);
      for j=2:n
      df(j)=(x-a(j-1)).*df(j-1)
      y(j)=c(j).*df(j)
      end
      fp=sum(c)

      Delete