ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 06 Jul 2022 11:10:30 +0200How to differentiate a messy function of 2 variableshttps://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/I have a very involved and messy function of two variable for which I need to find 1st and 2nd derivatives. It makes more sense to break this into "functions" so that one can use the chain rule. The following is a simplified version of what I really need.
x0 = 1 + h*g^2 + h
y0 = g + h^2
f1 = A*x0 - y0
f2 = B*y0 + x0
d = sqrt(f1^2 + f2^2)
I need partial of d w.r.t. g, partial of d w.r.t. h, partial of d w.r.t. g^2, etc.
How can I do this in SageMath?Mon, 04 Jul 2022 18:45:14 +0200https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/Comment by Max Alekseyev for <p>I have a very involved and messy function of two variable for which I need to find 1st and 2nd derivatives. It makes more sense to break this into "functions" so that one can use the chain rule. The following is a simplified version of what I really need.</p>
<pre><code>x0 = 1 + h*g^2 + h
y0 = g + h^2
f1 = A*x0 - y0
f2 = B*y0 + x0
d = sqrt(f1^2 + f2^2)
</code></pre>
<p>I need partial of d w.r.t. g, partial of d w.r.t. h, partial of d w.r.t. g^2, etc.</p>
<p>How can I do this in SageMath?</p>
https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63138#post-id-63138Isn't what you need is just `diff(d,g)` etc.?Tue, 05 Jul 2022 18:30:51 +0200https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63138#post-id-63138Comment by Snake21 for <p>I have a very involved and messy function of two variable for which I need to find 1st and 2nd derivatives. It makes more sense to break this into "functions" so that one can use the chain rule. The following is a simplified version of what I really need.</p>
<pre><code>x0 = 1 + h*g^2 + h
y0 = g + h^2
f1 = A*x0 - y0
f2 = B*y0 + x0
d = sqrt(f1^2 + f2^2)
</code></pre>
<p>I need partial of d w.r.t. g, partial of d w.r.t. h, partial of d w.r.t. g^2, etc.</p>
<p>How can I do this in SageMath?</p>
https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63137#post-id-63137Yes. hg^2 should be h*g^2.
But, that is not the issue with the problem that I'm trying to solve. What I posted was only an illustration of the steps that I need to consider.Tue, 05 Jul 2022 15:44:14 +0200https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63137#post-id-63137Comment by Emmanuel Charpentier for <p>I have a very involved and messy function of two variable for which I need to find 1st and 2nd derivatives. It makes more sense to break this into "functions" so that one can use the chain rule. The following is a simplified version of what I really need.</p>
<pre><code>x0 = 1 + h*g^2 + h
y0 = g + h^2
f1 = A*x0 - y0
f2 = B*y0 + x0
d = sqrt(f1^2 + f2^2)
</code></pre>
<p>I need partial of d w.r.t. g, partial of d w.r.t. h, partial of d w.r.t. g^2, etc.</p>
<p>How can I do this in SageMath?</p>
https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63132#post-id-63132Your "code" being ill-formated (e. g., I thnk that `hg^2` means `h*g^2` but can't be sure...), can you confirm that you mean :
sage: var("h, g, A, B")
(h, g, A, B)
sage: x0 = 1 + h*g^2 + h
sage: y0 = g + h^2
sage: f1 = A*x0 - y0
sage: f2 = B*y0 + x0
sage: d = sqrt(f1^2 + f2^2)
in which case :
$$ d=\sqrt{{\left(g^{2} h + {\left(h^{2} + g\right)} B + h + 1\right)}^{2} + {\left({\left(g^{2} h + h + 1\right)} A - h^{2} - g\right)}^{2}} $$Tue, 05 Jul 2022 09:47:52 +0200https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63132#post-id-63132Comment by slelievre for <p>I have a very involved and messy function of two variable for which I need to find 1st and 2nd derivatives. It makes more sense to break this into "functions" so that one can use the chain rule. The following is a simplified version of what I really need.</p>
<pre><code>x0 = 1 + h*g^2 + h
y0 = g + h^2
f1 = A*x0 - y0
f2 = B*y0 + x0
d = sqrt(f1^2 + f2^2)
</code></pre>
<p>I need partial of d w.r.t. g, partial of d w.r.t. h, partial of d w.r.t. g^2, etc.</p>
<p>How can I do this in SageMath?</p>
https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63126#post-id-63126Please indent code blocks by 4 spaces so they display as code blocks.Mon, 04 Jul 2022 18:54:20 +0200https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63126#post-id-63126Comment by slelievre for <p>I have a very involved and messy function of two variable for which I need to find 1st and 2nd derivatives. It makes more sense to break this into "functions" so that one can use the chain rule. The following is a simplified version of what I really need.</p>
<pre><code>x0 = 1 + h*g^2 + h
y0 = g + h^2
f1 = A*x0 - y0
f2 = B*y0 + x0
d = sqrt(f1^2 + f2^2)
</code></pre>
<p>I need partial of d w.r.t. g, partial of d w.r.t. h, partial of d w.r.t. g^2, etc.</p>
<p>How can I do this in SageMath?</p>
https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63125#post-id-63125Welcome to Ask Sage! Thank you for your question.Mon, 04 Jul 2022 18:53:52 +0200https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?comment=63125#post-id-63125Answer by Emmanuel Charpentier for <p>I have a very involved and messy function of two variable for which I need to find 1st and 2nd derivatives. It makes more sense to break this into "functions" so that one can use the chain rule. The following is a simplified version of what I really need.</p>
<pre><code>x0 = 1 + h*g^2 + h
y0 = g + h^2
f1 = A*x0 - y0
f2 = B*y0 + x0
d = sqrt(f1^2 + f2^2)
</code></pre>
<p>I need partial of d w.r.t. g, partial of d w.r.t. h, partial of d w.r.t. g^2, etc.</p>
<p>How can I do this in SageMath?</p>
https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?answer=63148#post-id-63148I *think* that I understand what you aim at. Let's devine your expressions :
sage: Vars=var("h, g, A, B")
sage: x0 = 1 + h*g^2 + h
sage: y0 = g + h^2
sage: f1, f2 = function("f1, f2")
sage: d = sqrt(f1(*Vars)^2+f2(*Vars)^2)
sage: [d.diff(u) for u in Vars]
but declare `f1` and `f2` as *undefined* functions :
sage: f1, f2 = function("f1, f2")
We can not define `d` using these functions :
sage: d = sqrt(f1(*Vars)^2+f2(*Vars)^2)
Deriving these functions will be done using the chain rule explicitly :
sage: D1 = [d.diff(u) for u in Vars] ; D1
[(f1(h, g, A, B)*diff(f1(h, g, A, B), h) + f2(h, g, A, B)*diff(f2(h, g, A, B), h))/sqrt(f1(h, g, A, B)^2 + f2(h, g, A, B)^2),
(f1(h, g, A, B)*diff(f1(h, g, A, B), g) + f2(h, g, A, B)*diff(f2(h, g, A, B), g))/sqrt(f1(h, g, A, B)^2 + f2(h, g, A, B)^2),
(f1(h, g, A, B)*diff(f1(h, g, A, B), A) + f2(h, g, A, B)*diff(f2(h, g, A, B), A))/sqrt(f1(h, g, A, B)^2 + f2(h, g, A, B)^2),
(f1(h, g, A, B)*diff(f1(h, g, A, B), B) + f2(h, g, A, B)*diff(f2(h, g, A, B), B))/sqrt(f1(h, g, A, B)^2 + f2(h, g, A, B)^2)]
Now, we can substitute `f1` and `f2` by their values :
sage: D2=[u.substitute_function(f1=(A*x*0+y0).function(*Vars)).substitute_function(f2=(B*y0+x0).function(*Vars)) for u in D1] ; D2
[((g^2*h + (h^2 + g)*B + h + 1)*(g^2 + 2*B*h + 1) + 2*(h^2 + g)*h)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2),
((g^2*h + (h^2 + g)*B + h + 1)*(2*g*h + B) + h^2 + g)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2),
0,
(g^2*h + (h^2 + g)*B + h + 1)*(h^2 + g)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2)]
We can also create an expressio using these substitutions :
sage: d2=d.substitute_function(f1=(A*x*0+y0).function(*Vars)).substitute_function(f2=(B*y0+x0).function(*Vars)) ; d2
sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2)
derive it :
sage: D3 = [d2.diff(u) for u in Vars] ; D3
[((g^2*h + (h^2 + g)*B + h + 1)*(g^2 + 2*B*h + 1) + 2*(h^2 + g)*h)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2),
((g^2*h + (h^2 + g)*B + h + 1)*(2*g*h + B) + h^2 + g)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2),
0,
(g^2*h + (h^2 + g)*B + h + 1)*(h^2 + g)/sqrt((g^2*h + (h^2 + g)*B + h + 1)^2 + (h^2 + g)^2)]
and check the equality with the previous expressions :
sage: all(map(lambda a,b:bool(a==b), D2, D3))
True
HTH,
Wed, 06 Jul 2022 11:10:30 +0200https://ask.sagemath.org/question/63124/how-to-differentiate-a-messy-function-of-2-variables/?answer=63148#post-id-63148