Hi, I am trying to create a function for Lagrange's interpolation and I wanted to know if there is a way to vectorize it that makes the code run faster
function y=lagrange(xx,yy,x)% xx and yy are vectors of data values, x is the vector I want to interpolate
n=size(xx,2);y=zeros(1,length(x));for k=1:length(x) for i=1:n L=1; for j =1:n if j~=i L=L*(x(k)-xx(j))/(xx(i)-xx(j)); end end y(k) =y(k)+yy(i)*L; endendend
I tried to do the following but the code is actually slower
for k=1:length(x) for i=1:n j=1:n;j(j==i)=[]; L=prod((x(k)-xx(j))./(xx(i)-xx(j))); y(k) =y(k)+yy(i)*L; endend
Any help is appreciated, thank you!
Best Answer