Preface


This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0330. It is primarily for students who have very little experience or have never used Mathematica before and would like to learn more of the basics for this computer algebra system. As a friendly reminder, don't forget to clear variables in use and/or the kernel.

Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in normal font. This means that you can copy and paste all commands into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts for your needs to learn the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.

Return to computing page for the first course APMA0330
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the second course APMA0330
Return to Mathematica tutorial for the first course APMA0340
Return to the main page for the course APMA0340
Return to the main page for the course APMA0330
Return to Part II of the course APMA0330

Exact Equations


Recall the total differential of a function ψ(x,y) of two variables, denoted by dψ, is given by the expression

\[ {\text d}\psi (x,y) = \frac{\partial \psi}{\partial x} \,{\text d}x + \frac{\partial \psi}{\partial y} \, {\text d}y . \]

Let M(x,y) and N(x,y) be two smooth functions having continuous partial derivatives in some domain \( \Omega \subset \mathbb{R}^2 \) without holes. A differential equation, written in differentials

\[ M(x,y)\,{\text d}x + N(x,y)\, {\text d}y =0 \]
is called exact if and only if
\[ \frac{\partial M(x,y)}{\partial y} = \frac{\partial N(x,y)}{\partial x} \]
or there exists a smooth function \( \psi (x,y) ,\) called the potential function, such that its total differential
\[ {\text d}\psi = M(x,y)\,{\text d}x + N(x,y)\, {\text d}y =0. \]
A potential function is not unique to which arbitrary constant can be added. Once it is known, the general solution is obtained immediately:
\[ \psi (x,y) = C , \qquad \mbox{a constant}. \]
Since Mathematica uses the command "N" for numerical evaluation, we change notations and use MM(x,y) and NN(x,y) instead of M(x,y) and N(x,y), respectively.

There are two approaches to find a potential function corresponding to an exact equation. The first one is based on integration of

\[ M(x,y) = \frac{\partial \psi}{\partial x} , \qquad N(x,y) = \frac{\partial \psi}{\partial y} \qquad \mbox{or using shortcut}\qquad M = \psi_x , \quad N= \psi_y \]
because the total differential of ψ is \( {\text d} \psi = \psi_x {\text d}x + \psi_y {\text d} y \) for all \( x \in \Omega . \) The second method utilizes line integration, which is prefered for solving initial value problems. Indeed, suppose we are given an initial value problem for an exact equation:
\[ M(x,y)\,{\text d}x + N(x,y)\, {\text d}y =0 , \qquad y(x_0) = y_0 . \]
Let L be arbitrary curve starting at \( (x_0 ,y_0 ) \) and ending at arbitrary point \( (x,y) . \) This curve or line should be without self-intersections and belong to the domain Ω where functions M(x,y) and N(x,y) possess continuous derivatives. Then the potential function can be obtained as
\[ \psi (x,y) = \int_L M(x,y)\,{\text d}x + N(x,y)\, {\text d}y . \]
In practice, we choose L as a semi-linear line going parallel to axes from initial point \( (x_0 ,y_0 ) \) and finishing at arbitrary point \( (x,y) \in \mathbb{R}^2 . \) In particular, we can integrate first along vertical axis and then horizontally, or we can integrate horizontally and then vertically.
line1 = Line[{{1, 0.5}, {4, 0.5}, {4, 3}}];
line2 = Line[{{1, 0.5}, {1, 3}, {4, 3}}];
a = {Graphics[{Thick, Dashed, Blue, line1}], Graphics[{Thick, line2}]}
b = Graphics[Text[Style["(x,y)", FontSize -> 14, Red], {4.0, 3.2}]]
b0 = Graphics[ Text[Style["(x0,y0)", FontSize -> 14, Blue], {1.0, 0.3}]]
aa1 = Graphics[Arrow[{{1, 1}, {1, 2}}]]
aa2 = Graphics[Arrow[{{2, 3}, {3, 3}}]]
aa3 = Graphics[{Blue, Arrow[{{1, 0.5}, {3, 0.5}}]}]
aa4 = Graphics[{Blue, Arrow[{{4, 1}, {4, 2}}]}]
Show[aa1, aa2, aa3, aa4, a, b, b0, Axes -> True, AxesOrigin -> {0, 0},
PlotRange -> {{-0.5, 4.5}, {-0.5, 3.5}}, AxesLabel -> {x, y}, TicksStyle -> Directive[FontOpacity -> 0, FontSize -> 0]]
Two lines of integration.

If we integrate along black line (vertically where dx = 0 and then horizontally where dy = 0), we get

\[ \psi (x,y) = \int_{y_0}^y N(x_0 ,y)\, {\text d}y + \int_{x_0}^x M(x ,y)\,{\text d}x . \]

Now if we integrate along blue dashed line (horizontally where dy = 0 and then vertically where dx = 0), we get

\[ \psi (x,y) = \int_{x_0}^x M(x ,y_0 )\,{\text d}x + \int_{y_0}^y N(x ,y)\, {\text d}y . \]

Example: The equation \( y \,\text{d}x + x \,\text{d}y =0 \) is exact because \( M_y =1 = N_x \) for \( M= y \quad\mbox{and} \quad N= x . \) Suppose that the initial condition \( y(2)=3 \) is given.

We type in Mathematica:

MM[x_, y_] = y; NN[x_, y_] = x;
{p1, p2} = {2, 3};
Simplify[D[MM[x, y], y] == D[NN[x, y], x]]
Out[4] = True
psi[X_, Y_] =
Integrate[MM[x, p2], {x, p1, X}] + Integrate[NN[X, y], {y, p2, Y}]
Out[22]= 3 (-2 + X) + X (-3 + Y)

The solution is psi[x,y]==0:

Define the gradient function:

grad[potential_, vars_List] := Map[Function[t, D[potential, t]], vars]

and then check our potential function:

grad[psi[x, y], {x, y}]
Out[25]= {y,x}
We check the answer with (blue) line integration:
\[ \psi (x,y) = \int_{2}^x 3\,{\text d}x + \int_{3}^y x\, {\text d}y = 3(x-2) +x( y-3) = xy-6. \]
Therefore, the solutiuon becomes ψ =0 or \( xy -6 =0 . \)

Example: Consider the differential equation

\[ \left( x+ 2y \right) {\text d}x + \left( 2x-3y \right) {\text d}y =0. \]
First, we check whether the given equation is exact.
MM=x+2 y; NN=2 x-3 y;
a=TrueQ[D[MM,y]==D[NN,x]];
If[a==True, Print["The equation is exact"], Print["The equation is not exact"]]
Out[4] = The equation is exact
F = Integrate[MM,x]
Out[5] = x^2/2 + 2 x y
derfy = D[F, y] - NN
Out[6] = 3 y
f[y] = Integrate[derfy, y]
Out[7] = (3 y^2)/2
psi = F + f[y]
Out[8] = x^2/2 + 2 x y + (3 y^2)/2
Print["psi=", psi];
Out[9] = psi=x^2/2+2 x y+(3 y^2)/2
Print["Trajectories of psi..."]
Out[10] = Trajectories of psi...
ContourPlot[F,{x,0,3},{y,0,3}]

 

 

Solving First Order ODEs

Plotting Solutions to ODEs

Direction Fields

Separable Equations

Equations Reducible to the Separable Equations

Equations with Linear Fractions

Exact Equations

Integrating Factors

Linear Equations

RC circuits

Bernoulli Equations

Riccati Equations

Existence and Uniqueness

Qualitative Analysis

Bifurcations

Orthogonal Trajectories

Population Models

Applications

 

Return to Mathematica page

Return to the main page (APMA0330)
Return to the Part 1 (Plotting)
Return to the Part 2 (First Order ODEs)
Return to the Part 3 (Numerical Methods)
Return to the Part 4 (Second and Higher Order ODEs)
Return to the Part 5 (Series and Recurrences)
Return to the Part 6 (Laplace Transform)
Return to the Part 7 (Boundary Value Problems)