# 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?

edit retag close merge delete

Please indent code blocks by 4 spaces so they display as code blocks.

Your "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}}$$

Yes. 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.

Sort by » oldest newest most voted

I 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,

more