Complement : Sage’s notation for derivatives is somewhat baroque, and this 
does not help in the present case.

Here, both k and x are functions of t ; writing h as a function of x and k 
is just an intricate way to write a function of t, unique independent 
variable.

As for notations : what we’d “manually” write in this case would be :
[image: \displaystyle{\frac{\partial h}{\partial 
t}}\,=\,\displaystyle{\frac{\partial h}{\partial x}\,\frac{\partial 
x}{\partial t}}]

and we could deduce

[image: \displaystyle{\frac{\partial h}{\partial 
x}}\,=\,\frac{\frac{\partial h}{\partial t}}{\frac{\partial x}{\partial t}}]
.

Sage doesn’t have a direct notation for denoting derivative with respect to 
another function ; instead, it uses the Doperator, which creates derivative 
operators :
sage: t=var("t") sage: h, x=function("h, x") sage: diff(h(x(t)), t) 
D[0](h)(x(t))*diff(x(t), t) sage: diff(h(x(t)), t)/diff(x(t), t) 
D[0](h)(x(t)) 

Eric’s notation is an elegant way to work around this problem.

The use of standard notations leads to :
sage: r, t, alpha, delta, r = var('r, t, alpha, delta, r') sage: k, x, 
lambda_ = function("k, x, lambda_") sage: h(t) = k(t)*(1-x(t))*e^(-r*t) + 
lambda_(t)*(k(t)^alpha*x(t)-delta*k(t)) sage: D[0](h)(x(t)) r*(x(x(t)) - 
1)*e^(-r*x(t))*k(x(t)) - (x(x(t)) - 1)*e^(-r*x(t))*D[0](k)(x(t)) - 
e^(-r*x(t))*k(x(t))*D[0](x)(x(t)) + (alpha*k(x(t))^(alpha - 
1)*x(x(t))*D[0](k)(x(t)) - delta*D[0](k)(x(t)) + 
k(x(t))^alpha*D[0](x)(x(t)))*lambda_(x(t)) - (delta*k(x(t)) - 
k(x(t))^alpha*x(x(t)))*D[0](lambda_)(x(t)) 

[image: \frac{\partial h(t)}{\partial x(t)}\,=\,r 
{\left(x\left(x\left(t\right)\right) - 1\right)} e^{\left(-r 
x\left(t\right)\right)} k\left(x\left(t\right)\right) - 
{\left(x\left(x\left(t\right)\right) - 1\right)} e^{\left(-r 
x\left(t\right)\right)} 
\mathrm{D}_{0}\left(k\right)\left(x\left(t\right)\right) - e^{\left(-r 
x\left(t\right)\right)} k\left(x\left(t\right)\right) 
\mathrm{D}_{0}\left(x\right)\left(x\left(t\right)\right) + {\left(\alpha 
k\left(x\left(t\right)\right)^{\alpha - 1} x\left(x\left(t\right)\right) 
\mathrm{D}_{0}\left(k\right)\left(x\left(t\right)\right) - \delta 
\mathrm{D}_{0}\left(k\right)\left(x\left(t\right)\right) + 
k\left(x\left(t\right)\right)^{\alpha} 
\mathrm{D}_{0}\left(x\right)\left(x\left(t\right)\right)\right)} 
\lambda\left(x\left(t\right)\right) - {\left(\delta 
k\left(x\left(t\right)\right) - k\left(x\left(t\right)\right)^{\alpha} 
x\left(x\left(t\right)\right)\right)} 
\mathrm{D}_{0}\left(\lambda\right)\left(x\left(t\right)\right).]
sage: D[0](h)(k(t)) r*(x(k(t)) - 1)*e^(-r*k(t))*k(k(t)) - (x(k(t)) - 
1)*e^(-r*k(t))*D[0](k)(k(t)) - e^(-r*k(t))*k(k(t))*D[0](x)(k(t)) + 
(alpha*k(k(t))^(alpha - 1)*x(k(t))*D[0](k)(k(t)) - delta*D[0](k)(k(t)) + 
k(k(t))^alpha*D[0](x)(k(t)))*lambda_(k(t)) - (delta*k(k(t)) - 
k(k(t))^alpha*x(k(t)))*D[0](lambda_)(k(t)) 

[image: \frac{\partial h(t)}{\partial k(t)}\,=\,r 
{\left(x\left(k\left(t\right)\right) - 1\right)} e^{\left(-r 
k\left(t\right)\right)} k\left(k\left(t\right)\right) - 
{\left(x\left(k\left(t\right)\right) - 1\right)} e^{\left(-r 
k\left(t\right)\right)} 
\mathrm{D}_{0}\left(k\right)\left(k\left(t\right)\right) - e^{\left(-r 
k\left(t\right)\right)} k\left(k\left(t\right)\right) 
\mathrm{D}_{0}\left(x\right)\left(k\left(t\right)\right) + {\left(\alpha 
k\left(k\left(t\right)\right)^{\alpha - 1} x\left(k\left(t\right)\right) 
\mathrm{D}_{0}\left(k\right)\left(k\left(t\right)\right) - \delta 
\mathrm{D}_{0}\left(k\right)\left(k\left(t\right)\right) + 
k\left(k\left(t\right)\right)^{\alpha} 
\mathrm{D}_{0}\left(x\right)\left(k\left(t\right)\right)\right)} 
\lambda\left(k\left(t\right)\right) - {\left(\delta 
k\left(k\left(t\right)\right) - k\left(k\left(t\right)\right)^{\alpha} 
x\left(k\left(t\right)\right)\right)} 
\mathrm{D}_{0}\left(\lambda\right)\left(k\left(t\right)\right).]

Not a pretty sight…
​
Le mardi 25 février 2025 à 11:01:27 UTC+1, egourg...@gmail.com a écrit :

> Hi, 
>
> Le lundi 24 février 2025 à 17:39:30 UTC+1, bernd.bre...@gmail.com a 
> écrit :
>
> So how should i define  H in 'In [2]',  so that 'In [4]'  shows the 
> expected result. 
>
>
> Basically you should define H as a symbolic expression, not a function, 
> and make a distinction between functions and symbolic variables. 
> A solution is 
>
> t = var('t')
> r = var('r')
> delta = var('delta')
> alpha = var('alpha')
>
> x = function('x')
> k = function('k')
> lambda_ = function('lambda_') 
>
> X = var('X')
> K = var('K')
> L = var('L')
> H = K*(1 - X)*exp(-r*t) + L*(K^alpha*X - delta*K)
>
> to_functions = {X: x(t), K: k(t), L: lambda_(t)}  # a dictionary to 
> perform substitutions
>
> eq1 = (diff(H, X).subs(to_functions) == 0)
> eq2 = (diff(H, K).subs(to_functions) == - diff(lambda_(t), t))
>
> Best wishes,
>
> Eric. . 
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/sage-support/55dbc03c-879d-4f53-92d6-b0ebd3fc4ac0n%40googlegroups.com.

Reply via email to