1
$\begingroup$

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?

$\endgroup$
4
  • $\begingroup$ Is there anything after $log$ as a parameter? $\endgroup$ Commented Jan 28, 2020 at 15:27
  • $\begingroup$ For n=3, lease post the actual expression for the "solution" and an example x along with the desired answer. $\endgroup$ Commented Jan 28, 2020 at 15:27
  • $\begingroup$ @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))$ $\endgroup$ Commented Jan 28, 2020 at 15:41
  • $\begingroup$ and lets assume $x_3=0.0009$ $\endgroup$ Commented Jan 28, 2020 at 15:47

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ You can skip [#]&. $\endgroup$ Commented Jan 29, 2020 at 6:45
  • $\begingroup$ @Kuba, yes, I edited my answer, thank you! $\endgroup$ Commented Jan 29, 2020 at 8:07
0
$\begingroup$

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] 
$\endgroup$
1
  • $\begingroup$ $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. $\endgroup$ Commented Jan 28, 2020 at 17:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.