# Symbolic partial differentiation of a Lagrangian function including a summation

I'm using Mathematica to double-check that I correctly derived first order conditions for the models I'm researching. Before I get started, I wanted to actually teach myself how to use Mathematica, and check that I can derive FOC's for a model I know. It's the (fairly simple) stochastic growth model with additive labor. I'm solving the model by setting up the following Lagrangian function.

In:= sgmodEq1 =
util[c[t], l[t]] == (c[t]^(1 - \[Phi])/(1 - \[Phi])) +
Subscript[\[Chi], 0] z[t] ((1 - l[t])^(1 - \[Chi])/(1 - \[Chi]))

In:= sgmodEq2 =
f[k[t], l[t]] == a[t] *k[t]^\[Alpha]*(z[t]*l[t])^(1 - \[Alpha])

In:= sgmodEq3 = \!$$\*UnderoverscriptBox[\(\[Sum]$$, $$t = 0$$, $$\[Infinity]$$]$$\*SuperscriptBox[\(\[Beta]$$, $$t$$]*$$(util[c[t], l[t]] + \[Lambda][ t]*\((f[k[t], l[t]] - c[t] + \((1 - \[Delta])$$*k[t] -
k[t + 1])\))\)\)\)

In:= sgmodEq4 = D[sgmodEq3, k[t + 1]]

Out= \!$$\*UnderoverscriptBox[\(\[Sum]$$, $$k = 0$$, $$\[Infinity]$$]$$\*SuperscriptBox[\(\[Beta]$$, $$K$$] \[Lambda][K] $$(\(- \*SubscriptBox[\(\[Delta]$$, $$t, K$$]\) + $$(1 - \[Delta])$$
\*SubscriptBox[$$\[Delta]$$, $$1 + t, K$$] +
\*SubscriptBox[$$\[Delta]$$, $$1 + t, K$$]
$$\*SuperscriptBox[\(f$$, $$(1, 0)$$]\)[k[K], l[K]] -
c[t])\)\)\)


If you're not familiar with the problem, you should know this is not the correct answer by the fact that $$\delta$$ is a function of K, which is now capitalized for some reason?

Before I set up the Lagrangian, I checked that I was able to correctly take partial derivatives of the utility function and production function. Afterward, I also checked that if I break the problem up into pieces, without the summation, I could get the correct answer, as follows:

In:= sgmodEq5 = \[Beta]^
t*(util[c[t],
l[t]] + \[Lambda][
t]*(f[k[t], l[t]] - c[t] + (1 - \[Delta])*k[t] - k[t + 1]))

In:= sgmodEq6 = \[Beta]^(
t + 1)*(util[c[t + 1],
l[t + 1]] + \[Lambda][
t + 1]*(f[k[t + 1], l[t + 1]] -
c[t + 1] + (1 - \[Delta])*k[t + 1] - k[t + 2]))

In:= sgmodEq7 = D[sgmodEq5, k[t + 1]]

Out= -\[Beta]^t \[Lambda][t]

In:= sgmodEq8 = D[sgmodEq6, k[t + 1]]

Out= \[Beta]^(1 + t) \[Lambda][1 + t] (1 - \[Delta] +
\!$$\*SuperscriptBox[\(f$$,
TagBox[
RowBox[{"(",
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[k[1 + t], l[1 + t]])


The solution to sgmodEq4 is sgmodEq7+sgmodEq8.

Also, I wish I could attach a notebook, because the code looks super messy. Basically, I'm doing this:

\begin{align*} \mathscr{L} &= \sum_{t=0}^\infty \beta^t \left( \left( \frac{C_t^{1-\phi}}{1-\phi} + \chi_0 Z_t \frac{(1-L_t)^{1-\chi}}{1-\chi} \right) \right. \\[1ex] &+\left. \lambda_t \left( A_t K_t^{\alpha} (Z_t L_t)^{1-\alpha} - C_t + (1-\delta) K_t - K_{t+1}\right) \right) \end{align*}

And this is the result I should be getting:

$$\frac{\partial \mathscr{L}}{\partial k_{t+1}}:\ \lambda_t = \beta \ \lambda_{t+1} \left( \alpha A_t K_t^{\alpha-1} (Z_t L_t)^{1-\alpha} + (1-\delta) \right)$$

And, instead, I am getting this nonsense:

$$\sum_{K=0}^\infty \beta^{K} \lambda[K] \left( -\delta_{t,K} + (1-\delta) \delta_{1+t,K} + \delta_{1+t,K} f^{(1,0)}[k[K],l[K]] \right)$$

But, I can get the right answer if I break the problem into parts. So, my questions are: (1) Why do I get the wrong answer (and such a strange answer) when I put all the pieces together?

And, (2) how can I adjust my code so that I can correctly take the partial derivative of the Lagrangian function, including the summation, and get the right answer?

• Does Assuming[t ∈ NonNegativeIntegers, D[sgmodEq3, k[t + 1]]] help? Jun 24, 2021 at 22:45
• I would avoid using subscripts until you have solved all your other problems Jun 25, 2021 at 6:48

Changing Subscript[\[Chi], 0] to \[Gamma], and using Assuming[t ∈ NonNegativeIntegers, D[sgmodEq3, k[t + 1]]] gives me this result:
$$-\beta^t \left( \lambda_t - \beta \lambda_{t+1} + \beta \delta \lambda_{t+1} - \beta \lambda_{t+1} f^{(1,0)}(k_{t+1},l_{t+1}) \right)$$