# Exact Differentials

I just started using Mathematica so my apologies if this is a very basic question. I tried to find related questions on this forum but couldn't, so here goes. I have an equation,

$\quad \quad dp =\dfrac{\partial p}{\partial x}dx + \dfrac{\partial p}{\partial y}dy = P(x,y)dx + Q(x,y)dy$

where I know $P(x,y)$ and $Q(x,y)$. Then, is there a way that I could solve for $p$? I found some information on this when $P(x,y)dx + Q(x,y)dy = 0$, but this is not the case in which I am interested.

• Please post at least some minimal but complete definitions. – ciao Apr 4 '15 at 22:30
• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey Apr 4 '15 at 22:50
• The mathematical procedure for solving this exact differential equation is given, for instance, here, and in general it can be solved the same way in Mathematica. – bbgodfrey Apr 4 '15 at 23:15
• While a line integral is one way to solve this problem, it is not the only way. The other ways are not ways to answer the question cited as a duplicate. So I do not think this is a duplicate of the other, although the other method of solution ought to be pointed out. – Michael E2 Apr 6 '15 at 0:17

At a simple algebraic calculus level, one could do this:

exactQ[{P_, Q_}, {x_, y_}] := D[P, y] == D[Q, x] // Simplify;
exactQ[{P_, Q_, R_}, vars_] := Curl[{P, Q, R}, vars] == {0, 0, 0} // Simplify;
exactSolve[vf_, vars_] /; exactQ[vf, vars] :=
Fold[#1 + Integrate[#2[] - D[#1, #2[]], #2[]] &,
0, Transpose[{vf, vars}]]


Example:

p = Exp[x] Cos[y] + Exp[y] + Sin[x] + y + 2 x
PQ = D[p, {{x, y}}]
(*
E^y + 2 x + y + E^x Cos[y] + Sin[x]
{2 + Cos[x] + E^x Cos[y], 1 + E^y - E^x Sin[y]}
*)

exactSolve[PQ, {x, y}]
(*
E^y + 2 x + y + E^x Cos[y] + Sin[x]
*)


Three-dimensional:

p2 = Exp[x y] Cos[z] + Sin[x z] - y^2 z
PQR = D[p2, {{x, y, z}}]
(*
-y^2 z + E^(x y) Cos[z] + Sin[x z]
{ E^(x y) y Cos[z] + z Cos[x z],
-2 y z + E^(x y) x Cos[z],
-y^2 + x Cos[x z] - E^(x y) Sin[z]}
*)

exactSolve[PQR, {x, y, z}]
(*
-y^2 z + E^(x y) Cos[z] + Sin[x z]
*)


Update - Extension to arbitrary dimensions.

Clear[exactQ];
exactQ[vf_List, vars_List] /; Length[vf] == Length[vars] :=
And @@ Map[Simplify[Curl @@ Transpose[#] == 0] &,
Subsets[Transpose[{vf, vars}], {2}]]

vf4 = D[w x y z + Exp[w^2 + x y] z, {{w, x, y, z}}];
exactSolve[vf4, {w, x, y, z}]
(*  (E^(w^2 + x y) + w x y) z  *)


The typical solution of such expressions proceeds as in the following example. Suppose

f[x, y] = Exp[x^2 + y^2] Cos[x + y] + x + y^3
(* x + y^3 + E^(x^2 + y^2)*Cos[x + y] *)


From these we construct p and q.

p[x, y] = D[f[x, y], x]
(* 1 + 2*E^(x^2 + y^2)*x*Cos[x + y] - E^(x^2 + y^2)*Sin[x + y] *)

q[x, y] = D[f[x, y], y]
(* 3*y^2 + 2*E^(x^2 + y^2)*y*Cos[x + y] - E^(x^2 + y^2)*Sin[x + y] *)


From p and q, we now wish to compute f. This is possible, only if

D[p[x, y], y] == D[q[x, y], x]
True


and it is. To proceed, we compute the integrals

fx[x, y] = FullSimplify[Integrate[p[x, y], x]] + cy[y]
(* x + E^(x^2 + y^2)*Cos[x + y] + cy[y] *)


where cy[y], an arbitrary function of y, has been added, because Integrate does not do so. Similarly,

fy[x, y] = FullSimplify[Integrate[q[x, y], y]] + cx[x]
(* y^3 + E^(x^2 + y^2)*Cos[x + y] + cx[x] *)


To determine cx[x] and cy[y], fx and fy are equated.

FullSimplify[fx[x, y] - fy[x, y]] == 0
(* x - y^3 - cx[x] + cy[y] == 0 *)


Hence, cx[x] = x + c and cy[y] = y^3 + c, with c an arbitrary constant. Substituting these into fx and fy give for both

f[x, y] = Exp[x^2 + y^2]*Cos[x + y] + x + y^3 + c


as desired. A more formal but equivalent approach is available here.

DSolve has become more effective since this question first was posted and now can solve it directly. For instance, with the sample problem used earlier in this answer,

DSolveValue[
{D[f[x, y], x] == 1 + 2 E^(x^2 + y^2) x Cos[x + y] - E^(x^2 + y^2) Sin[x + y],
D[f[x, y], y] ==  3 y^2 + 2 E^(x^2 + y^2) y Cos[x + y] - E^(x^2 + y^2) Sin[x + y]},
f[x, y], {x, y}] // FullSimplify

(* x + y^3 + C + E^(x^2 + y^2) Cos[x + y] *)


as desired.

This should be able to be solved with the DifferentialForms.m package https://library.wolfram.com/infocenter/MathSource/482/. Here is a sample script using one of the above examples:

Clear["Global*"]
<< DifferentialForms
p1 = Exp[x] Cos[y] + Exp[y] + Sin[x] + y + 2 x
q = d[p1] (*Take exterior derivative*)
p2 = Simplify[HomotopyOperator[q]] (*Find antiderivative*)
r = Simplify[p1 - p2] (*Check p2 is antiderivative for q*)
s = d[r] (*Check r is closed and therefore exact*)


We get

q = (2 + Cos[x] + E^x Cos[y])dx + (1 + E^y - E^x Sin[y])dy
p2 = -2 + E^y + 2 x + y + E^x Cos[y] + Sin[x]
r = 2
s = 0

• HomotopyOperator is almost worth the price of admission for DifferentialForms.m by itself. Integral together with HomotopyOperator definitely are, IMHO. – Jeffrey Rolland May 11 at 12:08