program for lagranges interpolation method using single for loop

1 visualización (últimos 30 días)
PJS KUMAR
PJS KUMAR el 23 de Ag. de 2018
Respondida: Geoff Hayes el 23 de Ag. de 2018
how to program for lagranges interpolation method using single for loop
  4 comentarios
Geoff Hayes
Geoff Hayes el 23 de Ag. de 2018
Please rename your local variable sum since it conflicts with the MATLAB built-in function of the same name.
Out of curiosity, why do you want to eliminate the inner for loop? For performance reasons?
PJS KUMAR
PJS KUMAR el 23 de Ag. de 2018
yes, for performance reasons can we write the program by using vector operations.

Iniciar sesión para comentar.

Respuestas (1)

Geoff Hayes
Geoff Hayes el 23 de Ag. de 2018
PJS - if we assume that x is a vector/array then u is simply
u = (a - x(1)) * (a - x(2)) * ... * (a - x(i-1)) * (a - x(i+1)) * ... * (a - x(n));
where n is the number of elements in x. If this is true, then couldn't we do something like
for i = 1:length(x)
u = 1;
l = 1;
xt = x;
xt(i) = []; % remove the ith element
u = prod(a - xt);
% etc.
end
We use prod to calculate the product of all elements in the array. Similarly, for l
l = (x(i) - x(1)) * (x(i) - x(2)) * ... * (x(i) - x(i-1)) * (x(i) - x(i+1)) * ... * (ax(i) - x(n));
which means that the above can be replaced with
l = prod(x(i) - xt));
Try the above and see what happens!

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by