0

enter image description hereI would be very grateful if you could help me to construct this matrix in Mathematica.

This is the code I have so far:

m = 10;
a = 0;
b = 1;
h = (b - a)/(m);

x[1] = 0; x[m] = 1;

For[ n = 2, n <= m, n++, x[n] = x[1] + n h; ]

xvalues = Table[x[i], {i, 1, m}]; subs = Subsets[xvalues, {Length@xvalues - 1}]; complements = Flatten[Complement[xvalues, #] & /@ subs];

d = Table[ Times @@ (1/(subs[[i]] - complements[[i]])), {i, 1, Length@subs}]; For[ n = 1, n <= m, n++, w[n] = d[[n]]; ]

aa = Table[Delete[Table[w[i]/w[n]/(x[n] - x[i]), {n, 1, m}], i], {i, 1, m}]

MarcoB
  • 67,153
  • 18
  • 91
  • 189
Osama
  • 13
  • 4
  • Welcome to the Mathematica Stack Exchange. Please edit the question further and review it before submitting to the forum. Thanks. – Syed Sep 23 '22 at 13:54
  • I will do it, Thanks – Osama Sep 23 '22 at 13:55
  • What have you tried so far? Have you tried at least defining your relationships as Mathematica functions? Can you include those as text code so it can be copy-pasted? – MarcoB Sep 23 '22 at 13:55
  • m = 10;

    a = 0; b = 1; h = (b - a)/(m); x[1] = 0; x[m] = 1; For[n = 2, n <= m, n++, x[n] = x[1] + n h; ]

    xvalues = Table[x[i], {i, 1, m}];

    subs = Subsets[xvalues, {Length@xvalues - 1}];

    complements = Flatten[Complement[xvalues, #] & /@ subs];

    – Osama Sep 23 '22 at 14:04
  • d = Table[ Times @@ (1/(subs[[i]] - complements[[i]])), {i, 1, Length@subs}];

    For[n = 1, n <= m, n++, w[n] = d[[n]]; ] In[20]:= aa = Table[Delete[Table[w[i]/w[n]/(x[n] - x[i]), {n, 1, m}], i], {i, 1, m}]

    – Osama Sep 23 '22 at 14:05
  • I reached here, I don't know how to continue. – Osama Sep 23 '22 at 14:07
  • Note that for simple assignments, Do is easier than For; it's exactly like Table, but doesn't return a list. So you can skip d altogether and just write Do[ w[i] = Times @@ (1/(subs[[i]] - complements[[i]])), {i, 1, Length@subs}]; just thought I'd share! – thorimur Sep 25 '22 at 03:28

1 Answers1

0

I'd write it like this. I use the trick that if I have the product over all values except $k$, I just replace the $k$th term with $1$; if I have the sum over all values except $k$, I just replace the $k$th term with $0$.

I left x as is, but check out Range and Subdivide; you might find something like Clear[x]; Evaluate@Array[x, {m}] = Subdivide[a, b, m - 1] useful, which sets all x's at once.

Note: I assume there was a typo in the definition of w, and that it should have been $\prod_{k\neq j}$. If not, the code might need modification.

I also can't help but wonder: is there a typo in $L''$? The sum over $k$ includes a (relatively) constant term, $\frac{1}{x_i-x_j}$, which does not depend on $k$ and thus can come out of the sum with a factor of $n-1$ attached (m in the code). In any case, I translated the expressions "verbatim", matching the code to the expression as closely as possible, with the exception of using Total or Times and Array instead of Sum or Product for some speed.

I also use a trick for memoization, to remember explicit values: f[x_] := (f[x] = ...) will e.g. calculate f[1] only when it is first called, but then remember that explicit calculated value for later. See here.

m = 10;
a = 0;
b = 1;
h = (b - a)/(m);

x[1] = 0; Do[x[i] = x[1] + i h, {i, 2, m}];

Do[w[j] = 1/(Times @@ Array[If[j == #, 1, x[j] - x[#]] &, {m}]), {j, 1, m}]

Lpp[j_][i_] := (Lpp[j][i] = If[j == i, -Total @ Array[If[i == #, 0, Lpp[#][i]] &, {m}], -2 (w[j]/w[i])/(x[i] - x[j]) (Total @ Array[ If[i == #, 0, (w[#]/w[i])/(x[i] - x[#]) + 1/(x[i] - x[j])] &, {m}]) ])

D2 = Array[Lpp[#2][#1] &, {m, m}]

Hope this helps! Let me know if there's any unfamiliar syntax.

thorimur
  • 9,010
  • 18
  • 32
  • I am grateful for your help. Unfortunately, when I run the code, I get this message "Syntax::sntxf: "k|" cannot be followed by "->If[i==k,0,(w[k]/w[i])/(x[i]-x[k])+1/(x[i]-x[j])]"." what does it means? – Osama Sep 26 '22 at 08:37
  • 1
    @Osama I suspect you're running an older version of Mathematica from before |-> syntax for functions was introduced; I've edited the answer. Hopefully it's compatible now? – thorimur Sep 26 '22 at 18:58
  • I am grateful for your help Dr. thorimur – Osama Sep 30 '22 at 12:29
  • Thank you Dr. thorimur for your help, your code is fantastic. Unfortunately, I got the number 9 in the numerator of some fractions but I don't know from where. can you please run this part of your code, `m = 10; a = 0; b = 1;

    Lpp[j_][i_] := (Lpp[j][i] = If[j == i, -Total@ Array[If[i == #, 0, Lpp[#][i]] &, {m}], -2 (w[j]/w[i])/(x[i] - x[j]) (Total@ Array[If[i == #, 0, (w[#]/w[i])/(x[i] - x[#]) + 1/(x[i] - x[j])] &, {m}])])

    D2 = Array[Lpp[#2][#1] &, {m, m}]` @ Thorimur

    – Osama Oct 05 '22 at 18:00