# Calculate scalar potential from electric field

Is it possible for Mathematica to solve an equation like $\nabla f(\mathbf{r}) = - \mathbf{E}(\mathbf{r})$ for $f(\mathbf{r})$?

I tried Reduce[Grad[f[x,y,z],{x,y,z}]== -{ex,ey,ez}, f] but that does not work.

• Could you please provide a complete code snippet ? Something that we could copy paste into our notebooks and see the actual error ? – stathisk Nov 12 '13 at 17:32
• What for? You need not playing with Mathematica for this: $$f(\mathbf{r}) = f(\mathbf{r_0})- \int \mathbf{E}(\mathbf{r}) d\mathbf{r}$$ – Artes Nov 12 '13 at 17:35
• It's a differential equation so you need to use DSolve – Simon Woods Nov 12 '13 at 18:53

I'll try to combine Artes and Simon Woods comments in an answer.

Given is the electric field as el = {ex, ey, ez} = {er, e\[Theta], ez} in Cartesian and Cylindrical coordinates, respectively.

Solving the differential equation can be done in Cartesian coordinates with

DSolve[Grad[f[x, y, z], {x, y, z}] == {ex, ey, ez},
f[x, y, z], {x, y, z}]

{{f[x, y, z] -> ex x + ey y + ez z + C[1]}}

Or simply by integration, where one has to parametrize the integration variable

Integrate[{ex, ey, ez}.{x t, y t, z t}, {t, 0, Sqrt[2]}]

ex x + ey y + ez z

Remember the constant integration factor, which is set to zero here.

Integration is also easily done in different coordinate systems, such as cylindrical ones

Integrate[{er, e\[Theta], ez}.{r t, r \[Theta] t, z t}, {t, 0,
Sqrt[2]}]

er r + ez z + e\[Theta] r \[Theta]

How to use DSolve[] in this case I don't know yet, as

DSolve[Grad[f[r, \[Theta], z], {r, \[Theta], z},
"Cylindrical"] == {er, e\[Theta], ez},
f[r, \[Theta], z], {r, \[Theta], z}]

does not work.