write a matlab function which will find a lagrange polynomia

write a matlab function which will find a lagrange polynomial of degree 2 through 3 given points. Use your function which will find the lagrange polynoimal of degree 2 through points (0,2),(1,3) and (3,-1). Can not use built in function for lagrange

Solution

function y=lagrangePoints(x,xpoint,ypoint)
   len=size(xpoint,2);
   Z=ones(len,size(x,2));
   if (size(xpoint,2)~=size(ypoint,2))
   fprintf(1,\'points must have same length\');
   y=NaN;
   else
   for i=1:len
       for j=1:len
           if (i~=j)
               Z(i,:)=Z(i,:).*(x-xpoint(j))/(xpoint(i)-xpoint(j));
           end
       end
   end
   y=0;
   for i=1:len
       y=y+ypoint(i)*Z(i,:);
   end
   end
end  

%calling function
x = [0 1 3];
y = [2 3 -1];
xx = linspace(0,3);
yy = lagrangePoints(xx,x,y);
plot(x,y,\'o\',xx,yy,\'.\')  

write a matlab function which will find a lagrange polynomial of degree 2 through 3 given points. Use your function which will find the lagrange polynoimal of d

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site