2
$\begingroup$

I have a polynomial function of $n$ variables (at most second order) like this one:

\begin{equation} f_{i}(t_{1},t_{2},\dots,t_{n})=(\alpha+\beta\sum_{j=1}^{n}t_{j})^{2}+(\gamma+\delta\sum_{j=1}^{n}t_{j}+\omega t_{i})^{2} \end{equation}

I need to optimize this function with respect to $t_i$, i.e. I need to

1) differentiate $f_i$ with respect to $t_i$

2) calculate the optimal $t_i$ (taking the other $t_j$ as constants) by setting the derivative of $f_i$ to zero and solving for $t_i$(as a function of the other $t_j$)

3) solve the system of $n$ equations in all the $t_j$ (this step will be simplified by symmetry argument)

4) evaluate $f_i$ at the optimum value of all the $t_j$

I have seen quite a lot of posts about symbolic sums and “how to differentiate formally”, but I still cannot figure out how to deal with my function in mathematica. What is the most elegant and efficient way to define my function in Mathematica? How to do the differentiation? Evaluation? Could anyone please help me?

$\endgroup$
7
  • $\begingroup$ I not sure I get the last 2 parts but this may help you : f[i_] := (\[Alpha] + \[Beta]* Sum[t[j], {j, 1, n}])^2 + (\[Gamma] + \[Delta]* Sum[t[j], {j, 1, n}] + \[Omega]*t[i])^2 Solve[D[f[i], t[i]] == 0, t[i]] $\endgroup$ – David Baghdasaryan May 13 '17 at 14:37
  • $\begingroup$ Many thanks for your reply! Sorry for being unclear in my question and sorry to be slow (I am quite new to mathematica), I have not quite managed to make your solution work for me. In my function, $i\in[0,n]$. When I use your code, mathematica does not seem to have this information and so when it differentiates with respect to $t_i$ it only takes into account the solitary $t_i$ term and not the fact that it is also inside the sum. $\endgroup$ – Lednacek May 13 '17 at 15:27
  • $\begingroup$ (1) What system of n equations? (2) What has this to do with Mathematica? $\endgroup$ – Daniel Lichtblau May 13 '17 at 19:21
  • $\begingroup$ Is not there a sum missing after $\omega$ ? $\endgroup$ – yarchik May 13 '17 at 19:47
  • $\begingroup$ (1) There are $n$ functions $f_{i}$ defined as above (for $i\in[1,n]$), they are all polynomials of $n$ variables $t_{i}$ for $i\in[1,n]$. I need to calculate the optimal values of $t_{i}$ which optimise the $f_{i}$ functions. (2) I would like to do this calculation in mathematica, for this I need to find the proper way to define the functions in mathematica so that I can differentiate them with respect to the $t_{i}$s, solve the linear equations (derivatives=0) and then evaluate original functions at the optimal values. $\endgroup$ – Lednacek May 13 '17 at 20:45
3
$\begingroup$

Here is a way of doing the first two parts of your problem. I can't say anything about the third part because it isn't clear to me what system of $n$ equations you are talking about.

I choose a system with three variables for this example, but vars could be any arbitrary list of value-free symbols.

vars = {t1, t2, t3};

f[i_Integer, a_, b_, c_, d_, ω_, vars : {_Symbol ..}] := 
  (a + b Total[vars])^2 + (c + d Total[vars] + ω vars[[i]])^2

df[i_, a_, b_, c_, d_, ω_, vars : {_Symbol ..}] := 
  D[f[i, a, b, c, d, ω, vars], vars[[i]]]

solver[i_Integer, vars : {_Symbol ..}] := 
  Solve[df[i, a, b, c, d, ω, vars] == 0, vars[[i]]] // Simplify

Flatten[solver[#, vars] & /@ Range[Length[vars]]]

solution

However, the solutions have an obvious pattern. Let's make use of it to get a more elegant and faster solution.

zeros[i_Integer, a_, b_, c_, d_, ω_, vars : {_Symbol ..}] :=
  Module[{var, others},
    {var, others} = {#1, {##2}} & @@ RotateLeft[vars, i - 1];
    var -> -((a b + c (d + ω) + Total[others] (b^2 + d (d + ω)))/(b^2 + (d + ω)^2))]

Here are the zeros for a system with four variables.

With[{vars = {t1, t2, t3, t4}}, 
  zeros[#, a, b, c, d, ω, vars] & /@ Range[Length[vars]]]

zeros

Update

The OP has raised a whole host of new issues in a series of comments. Far too many for me to address them all. I will, however, address the issue labeled with 5) which is quite simple.

One way to deal with indexing in symbolic computation is to use forms such as x[n], where n is an integer. Indeed, such forms are sometimes referred to as indexed variables.

To apply such forms to the matter in question, we could proceed as follows.

makeVars[id_Symbol, n_Integer] := Table[id[i], {i, n}]
vars = makeVars[t, 3]

{t[1], t[2], t[3]}

To use these new forms, we must make a minor modification to argument patterns the functions f and df.

Clear[f]
f[i_Integer, a_, b_, c_, d_, ω_, vars : {_Symbol[_Integer] ..}] := 
  (a + b Total[vars])^2 + (c + d Total[vars] + ω vars[[i]])^2

Clear[df]
df[i_Integer, a_, b_, c_, d_, ω_, vars : {_Symbol[_Integer] ..}] := 
  D[f[i, a, b, c, d, ω, vars], vars[[i]]]

Then we can compute, say, the derivative of f with respect to t[2] with

With[{i = 2}, df[i, a, b, c, d, ω, vars] /. (Rule[#, #[i]] & /@ {a, b, c, d, ω})]

2 b[2] (a[2] + b[2] (t[1] + t[2] + t[3])) + 2 (d[2] + ω[2]) (c[2] + d[2] (t[1] + t[2] + t[3]) + t[2] ω[2])

But it is not at all clear to me that the parameters should be indexed in the manner I have assumed here, so it is somewhat doubtful that this update will be of much help to the OP. It is this lack of clarity in the OP's articulation of the problem that makes it so very difficult to help the OP.

$\endgroup$
9
  • $\begingroup$ Thank you VERY much for your helpful reply. This is beautiful! I am sorry for not having been clear in my question. May I please try to clarify a few points and ask you a few subquestions prompted by your answer in order to solve fully my problem? 1) How can I display the intermediate steps of your calculation? In particular, how can I display the derivative(s)? I know this is a silly question, I am very new to all this. $\endgroup$ – Lednacek May 14 '17 at 18:36
  • $\begingroup$ 2) In your $n=3$ example, you obtain three derivatives ($df_{1}/dt_{1}, df_{2}/dt_{2}, df_{3}/dt_{3})$. Setting them equal to 0 and solving for ($t_{1},t_{2}, t_{3}$) respectively yields three equations $\{t_{1}\rightarrow h_{1}(t_{2},t_{3}), t_{2}\rightarrow h_{2}(t_{3},t_{1}),t_{3}\rightarrow h_{3}(t_{1},t_{2})\}$ (in my example, which is a very simplified (and maybe too much so) version of my real problem the $h$ functions are identical, but that may not be the case in my more complicated case). These can be viewed as three equations in three unknowns ($t_{1},t_{2}, t_{3}$). $\endgroup$ – Lednacek May 14 '17 at 18:41
  • $\begingroup$ How can I then solve for the unknowns ($t_{1},t_{2}, t_{3}$) only in terms of the parameters ($a, b, c,d, \omega$)? (Again my example may not be ideal for this, sorry, but I am interested in the methodology to use in Mathematica.) $\endgroup$ – Lednacek May 14 '17 at 18:41
  • $\begingroup$ 3) Once I have the solutions ($t_{1},t_{2}, t_{3}$) only in terms of the parameters ($a, b, c,d, \omega$), I would like to evaluate the initial function at this optimum, i.e. I would like to simplify, plot etc. $f_{1}(t_{1},t_{2}, t_{3})$. How can I do that? $\endgroup$ – Lednacek May 14 '17 at 18:41
  • $\begingroup$ 4) In the derivatives ($df_{1}/dt_{1}, df_{2}/dt_{2}, df_{3}/dt_{3})$, how can I impose additional conditions of the variables before solving for the optimal values that make the derivative $=0$? For example, in the derivative $df_{1}/dt_{1}$ which is a function of ($t_{1},t_{2}, t_{3}$), how can I tell Mathematica that $t_{1}=t_{2}=t_{3}=t$ and solve for $t$ which gives $df_{1}=0$? (I want to be able to impose relationships on the $t_{i}$ once I have the derivative, not in the initial function before differentiating.) $\endgroup$ – Lednacek May 14 '17 at 18:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.