I have a function of the following type where $p(i)$ is a variable on which the agent can optimize and $p(-i)$ a vector of variables that he has to take as parameters.
This is a demand function and from this function, one can compute a profit function.
My goal is to find the optimal solutions $p_1^*,...,p_N^*$ by computing the first-order conditions of each profit function with respect to $p(i)$ and setting each of them to 0.
Following the first answer of @user42582, I modified my code as follows:
(* Demand function *)
(* Takes a vector p1,...,pn and optional scalar arguments v and g and
returns a vector q1, ..., qn *)
(* Rn to Rn *)
q[p_, v_: V, g_: \[Gamma]] := Module[
{meanP = Mean[p]},
v - p - g (p - meanP)]
(* Profit function *)
(* Takes a vector p1,...,pn and optional scalar arguments cost, v and \
g and returns a vector profit1,...,profitn *)
(* Rn to Rn *)
profit[p_, cost_: c, v_: V, g_: \[Gamma]] := (p - cost) * q[p, v, g]
(* Getting the Jacobian matrix *)
(* Differentiating a Rn to Rn function wrt to X => N x N matrix *)
dvector[p_, cost_: c, v_: V, g_: \[Gamma]] :=
D[profit[p, cost, v, g], {p}]
(* Displaying the Jacobian on an example *)
MatrixForm[dvector[{x, y, z}]]
(* Displaying only the element 2,2 of the Jacobian matrix *)
dvector[{x, y, z}][[2, 2]]
(* Solving for the prices *)
solution[p_, cost_: c, v_: V, g_: \[Gamma]] := Module[
{ n = Length[p], foc, leqs},
(* Get the derivatives *)
foc = Diagonal[dvector[p, cost, v, g]];
(* Set the equations *)
leqs = Thread[foc == ConstantArray[0, n]];
(* Solve the equations *)
Solve[leqs, p][[-1]]
]
(* Check on an example *)
solution[{x, y, z}]
Now, the solution function is fully functionnal on any example price vector of defined length like {x,y,z} or {p1, p2, p3, p4, p5} for instance.
Yet, my goal is to find the general solution of the system, which is a function of N the length of my price vector. More precisely, if p(1),...,p(N) is my price vector, I would like to express, for any i, p(i) as a function of the primitives of the model as well as N, the length of the price vector.
Is there a way to solve this in its general form ?
Thanks in advance,
M
solution
insoln[n_Integer] := Module[{prices = Array[Subscript[p, #] &, n]}, solution[prices]]
to get a function that takes the length of the price vector as input (and use it assoln[3]
,soln[6]
etc.) $\endgroup$