# Assigning values to multiple variables from a list

I have a solution of the following form:

$$F(x)= \\ \qquad c+(x_0+x_1+x_2+\ldots +x_n)- \\ \qquad (x_0\,\log(x_0+\sqrt{x_0})+x_1\,\log(x_1+\sqrt{x_1})+x_2\,\log(x_2+\sqrt{x_2})+\ldots +x_n\,\log(x_n+\sqrt{x_n}))$$

I want to replace $$x_0\ldots x_n$$ from an array containing $$x_0\ldots x_n$$ as $$x=\{x_0,x_1,x_2, \ldots,x_n\}$$

How this can be done in Mathematica?

Is there a direct Mathematica solution like Replace ( /. ) to this problem?

• Is there anything after $log$ as a parameter? – bcegkmqs23 Jan 28 '20 at 15:27
• For n=3, lease post the actual expression for the "solution" and an example x along with the desired answer. – Alan Jan 28 '20 at 15:27
• @bcegkmqs23 yes there is. Full solution for n = 3 looks like this: $F(x)=c+x_3-x_3*log(x_3+sqrt(x_3))$ – Cameron Jan 28 '20 at 15:41
• and lets assume $x_3=0.0009$ – Cameron Jan 28 '20 at 15:47

Suppose your F contains indexed variables x[0], x[1], .... You just need to make replacement rules:

xList = RandomReal[10, 10];
rules = Thread[Array[x, 10, 0] -> xList]


{x[0] -> 7.30088, x[1] -> 6.64534, x[2] -> 0.195005, x[3] -> 0.750164, x[4] -> 1.75732, x[5] -> 8.85201, x[6] -> 2.86472, x[7] -> 6.51873, x[8] -> 2.91673, x[9] -> 7.25998}

and use them as F /. rules.

• You can skip [#]&. – Kuba Jan 29 '20 at 6:45
• @Kuba, yes, I edited my answer, thank you! – Alx Jan 29 '20 at 8:07

Is there something you need listed below?

g = # - # Log[# + Sqrt@#] &;
f[x_List,] := c + Total[g /@ x]
f@{1, 2, 3, 4, 5}

g = # - # Log[# + Sqrt@#] &;
f = c + Total[g /@ Table[x[i], {i, 5}]];
With[{x = {1, 2, 3, 4, 5}[[#]] &}, f]

• $g$ in my case is already stored in the form described above. $c$ also doesn't matter. Only thing i want to do is assign values to $x_i$ from x wherever they appear in $g$. Apparently above two suggestion doesn't seem to work for me. First one gives the "SetDelayed::write" and second one appends $x$ to $g$, $i$ times. – Cameron Jan 28 '20 at 17:01