Plotting Basis Order function
If you want to use Mathematica as it is designed to generate the Lagrange basis as a list of polynomials, then use InterpolatingPolynomial
:
lBasis[nodes_, x_] := Table[
InterpolatingPolynomial[
Transpose@{nodes, UnitVector[Length@nodes, k]}, x],
{k, Length@nodes}]
To plot them:
xj = {0, 2, 3, 7, 10, 11};
Plot[lBasis[xj, x] // Evaluate, {x, Min[xj], Max[xj]},
Epilog -> {Red, Point@Thread[{xj, 1}], Point@Thread[{xj, 0}]},
GridLines -> {xj, {1}}]
To get the same thing from my comment, fix the typo in the comment and use Table
to list the basis:
L[i_, xj_, x_] := (* i-th Lagrange basis function *)
Fold[Times, (x - #)/(xj[[i]] - #) &@Drop[xj, {i}]];
lBasis[nodes_, x_] := Table[L[k, nodes, x], {k, Length@nodes}]
If you want to write something like a C program as an exercise and not avoid the for loop, then maybe someone else can help with that.