Ask Your Question
1

How to differentiate a messy function of 2 variables

asked 2022-07-04 18:45:14 +0200

Snake21 gravatar image

updated 2022-07-05 18:27:23 +0200

Max Alekseyev gravatar image

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 flag offensive close merge delete

Comments

Welcome to Ask Sage! Thank you for your question.

slelievre gravatar imageslelievre ( 2022-07-04 18:53:52 +0200 )edit

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

slelievre gravatar imageslelievre ( 2022-07-04 18:54:20 +0200 )edit

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

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-07-05 09:47:52 +0200 )edit

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.

Snake21 gravatar imageSnake21 ( 2022-07-05 15:44:14 +0200 )edit

Isn't what you need is just diff(d,g) etc.?

Max Alekseyev gravatar imageMax Alekseyev ( 2022-07-05 18:30:51 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-07-06 11:10:30 +0200

Emmanuel Charpentier gravatar image

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,

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2022-07-04 18:45:14 +0200

Seen: 164 times

Last updated: Jul 06 '22