Michael A - 8 months ago 52

Python Question

I have a simple equation like

`(f(t) * g(t))^a`

`a`

`f`

`g`

- Differentiate the expression with respect to , which should be an expression with
`t`

, and `g'(t). In the simple example up above, the result should be`f(t), g(t), f'(t)`

`a * (f(t) * g(t))**(a - 1) * (f'(t) * g(t) + f(t) * g'(t))`

- Now, we use some knowledge of this specific problem (a problem in economics), where
**at one specific steady state value only**, we know the values ofand`f(t)`

. Let's say they're`g(t)`

and`f(tss) = 1`

, where`g(tss) = 100`

is the steady state value, which I'll set as`tss`

arbitrarily. These are`tss = 7`

**not the general functional forms of f and g**. - Once we substitute in these values, we have an equation with two unknowns: the values of and
`f'(tss)`

. At this point it doesn't really matter if they're derivatives or not; they're just unknowns, and I have other equations that when combined with this one, give me a non-linear system that I can solve using`g'(tss)`

or one of sympy's solvers.`scipy.optimize.fsolve`

The question is, I'm stuck on steps 1 and 2. The code below doesn't seem to substitute the values in correctly.

`from sympy import *`

t = symbols('t')

a = symbols('a')

f, g = symbols('f g', cls=Function)

eq = (f(t) * g(t))**a

eq_diff = eq.diff(t)

output = eq_diff.evalf(subs={f:1, g:100, a:0.5})

output

This outputs

which doesn't substitute the values at all. What am I doing wrong?

Again, this is just a trivial mathematical example, but it demonstrates the question nicely.

Answer

You could do something like this:

```
fd, gd = symbols('f_d, g_d') #values of steady-state derivatives
output.subs({f(t).diff(t):fd, g(t).diff(t):gd, f(t):1, g(t):100, a:Rational(1,2)})
```

`5*f_d + g_d/20`

Source (Stackoverflow)