Finding scalar potential function [duplicate]

Consider the vector field defined by: $$\vec F(x,y)=\langle 2xy-\sin x,x^2+e^{3y}\rangle$$ We can check to see if the vector field is conservative with the following calculations: \begin{align*} \frac{\partial}{\partial x}(x^2+e^{3y})=2x\\ \frac{\partial}{\partial y}(2xy-\sin x)=2x\\ \end{align*} Now, I am interested in looking at several different procedures for finding a scalar function $f(x,y)$ such that $\vec F(x,y)=\nabla f(x,y)$. Can folks share their ideas?

Update: Sure, students need to attack this by hand in class in the following manner.

\begin{align*} \vec F(x,y)&=\nabla f(x,y)\\ \langle 3+2xy, x^2-3y^2\rangle&=\langle\partial f/\partial x, \partial f/\partial y\rangle \end{align*}

They start by setting $$\frac{\partial f}{\partial x}=3+2xy,$$ then integrate with respect to $x$. $$f(x,y)=3x+x^2y+h(y)$$ The second step is to set: \begin{align*} \frac{\partial f}{\partial y}&=x^2-3y^2\\ \frac{\partial}{\partial y}(3x+x^2y+h(y))&=x^2-3y^2\\ x^2+h'(y)&=x^2-3y^2 \end{align*} The last line gives us $$h'(y)=-3y^2,$$ then integrating gives us $$h(y)=-y^3.$$ Subbing this into $f(x,y)=3x+x^2y+h(y)$ gives the final answer. $$f(x,y)=3x+x^2y-y^3$$

So I am trying:

Clear[f, h, x, y, p, q]
p = 3 + 2 x y;
q = x^2 - 3 y^2;


Then I perform the conservative test:

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


Which gives:

(* 2x *)

(* 2x *)


So we do have a conservative vector field. Next:

f=Integrate[p,x]+h[y]


Which gives:

(* 3 x + x^2 y + h[y] *)


Next, I run:

Solve[D[f, y] == q, h'[y]]


Which gives:

(* {{Derivative[1][h][y] -> -3 y^2}} *)


Then I do this:

Integrate[-3 y^2, y]


Which gives:

(* -y^3 *)


Then I do this:

f = f /. h[y] -> -y^3


(* 3 x + x^2 y - y^3 *)


I love the answers I have received thus far, but would also like to see what folks do to purify my attempt.

• The approach in your update seems quite reasonable to me. – bbgodfrey Nov 30 '15 at 5:16

The typical approach is

DSolve[{D[f[x, y], x] == 2 x y - Sin[x], D[f[x, y], y] == x^2 + Exp[3 y]}, f[x, y], {x, y}]
(* {{f[x, y] -> E^(3 y)/3 + x^2 y + C[1] + Cos[x]}} *)


Note that, if the vector field were not conservative, DSolve would return unevaluated.

• Short and simple; it gets my upvote. (But I am a little concerned that DSolve might be checking the political views of its arguments..) – Daniel Lichtblau Nov 29 '15 at 17:53
• @bbgodfrey A very elegant approach. This is definitely an approach I will use in class tomorrow with my students. It will also give those students who will take differential equations next semester a nice introductory look to the DSolve command. – David Nov 29 '15 at 19:53

Very elegant bbgodfrey; here is another method:

eq1 = Integrate[2 x y - Sin[x], x] // Expand;
eq2 = Integrate[x^2 + Exp[3 y], y] // Expand;
DeleteDuplicates@Flatten[List @@@ {eq1, eq2}] // Total

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

• Another very interesting approach. This again helps me understand the @@@, the Flatten, and DeleteDuplicates commands. Extremely helpful. Thanks. – David Nov 29 '15 at 19:54