Preface


This tutorial was 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 regular fonts. This means that you can copy and paste all comamnds into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts to your needs for learning how to use 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 first course APMA0330
Return to Mathematica tutorial for the second course APMA0340
Return to the main page for the course APMA0330
Return to the main page for the course APMA0340
Return to Part II of the course APMA0330

Separable Equations


A differential equation \( y' = f(x,y) \) is said to be separable if the slope function is the product of two functions depending on only one variable:  \( f(x,y) = p(x) q(y). \) Then rewriting the derivative y' in differential form  \( y' = {\text d}y / {\text d}x , \) we separate variables:

\[ \frac{{\text d}y}{q(y)} = p(x)\, {\text d}x , \]
each part can be integrated. In other words, a separable differential equation is a differential equation in which the two variables can be placed on opposite sides of the equals sign such that the dx and x terms are on one side and the dy and the y terms are on the other. The dx and dy terms need to be multiplied by the x and y terms, respectively. We show how to solve separable differential equations in the following examples.

Example: The general solution to the equation \( y' = x^2/y^2 /\sqrt{4-x^2} \) is found by separation of variables

DSolve[y'[x] == x^2 /y^2 /Sqrt[4-x^2], y, x] // Flatten
{ y -> Function[{x}, -(-3/2)^(1/3) (-x Sqrt[4-x^2] + 4 ArcSin[x/2] + 2 C[1])^(1/3)],
y -> Function[{x}, (3/2)^(1/3) (-x Sqrt[4-x^2] + 4 ArcSin[x/2] + 2 C[1])^(1/3)],
y -> Function[{x}, (-1)^(2/3) (3/2)^(1/3) (-x Sqrt[4-x^2] + 4 ArcSin[x/2] + 2 C[1])^(1/3)]}

Example: Consider separable differential equation:   \( y'= xy/(1+x^2 ). \) First, we separate variables and integrate

\[ \frac{{\text d}y}{y} = \frac{{\text d}x\,x}{1+ x^2} \qquad \Longrightarrow \qquad \ln Cy = \frac{1}{2} \, \ln (1+x^2 ) = \ln \sqrt{1+ x^2} . \]
Therefore, the general solution (which is a family of functions containing arbitrary constant C) is
\[ C\,y^2 = 1 + x^2 . \]
Then we ask Mathematica to check the answer.

DSolve[{y'[x] == x*y[x]/(x^2 + 1)}, y[x], x]
Plot[{Sqrt[1 + x*x], 4*Sqrt[1+x*x], 0.4*Sqrt[1+x*x], 8*Sqrt[1+x*x]}, {x, 0, 5}, PlotLabel -> "Solutions to Example 1.3.1 "]

In this command sequence, the first line of code asks Mathematica to find the solution to this separable differential equation.
The second line contains commands to plot different possible solutions to this separable differential equation based on the value of the integration constants. None of the subcommands of Plot are new in this command sequence and they have already been explained earlier.

We would like to use advantage of knowing Mathematica to solve the equation by direct integration and to then plot those solutions:

Clear[t,y];
t0 = 0; y0 = 2; f[t_] = t/(1 + t^2); g[y_] = 1/y;          (* define the initial values and the slope functions *)

F[t_] = Integrate[f[t], t]; G[y_] = Integrate[g[y], y];
gensol = Solve[G[y] == F[t] + c, y];
const = Solve[y == y0 /. gensol /. t -> t0, c];
sol[t_] = y /. gensol /. const[[1]]                                (* solution of the IVP *)
Out[6] = {2 Sqrt[1 + t^2]}
Plot[sol[t], {t, -2, 2}]

In this syntax, the first command, Clear, clears out any previously existing values for the two variables, allowing you to redefine them.
The second command defines the different initial conditions and the two functions that you are trying to solve.
The first command line tells Mathematica which functions you want to integrate.
The fourth and fifth lines of codes tell Mathematica how you want to define the general solution and how you want to solve for the integration constant c.
The sixth line gives the final solution to this separable differential equation (this is also an initial value problem).
The final line tells Mathematica which function to plot and the range.

In Mathematica, as this example shows, you can insert comments to the commands by adding a comment surrounded by stars as in (* solution of the IVP *) and then parentheses. Even when variables can be separated, the final solution might be accompanied by a warning message from Solve, or it might only be given as an InverseFunction object.

Example: Consider separable differential equation:    y' +ycos(x) =0

sol = DSolve[y'[x] == -Cos[x]*y[x], y[x], x]
Out[1]= {{y[x] -> E^-Sin[x] C[1]}}
toplot = Table[E^-Sin[x] C[1] /. C[1] -> i, {i, -5, 5}];
Plot[Evaluate[toplot], {x, 0, 4*Pi}]

Example: Consider separable differential equation:    x(y+1)(y-3) dx +(1+5y)(x+2) dy =0. We solve this equation by separating variables and then integrating:

Integrate[x/(x + 2), x] + Integrate[(1 + 5*y)/(y + 1)/(y - 3), y] == c
Out[1] = x - 2 Log[2 + x] + 4 Log[-3 + y] + Log[1 + y] == c
solution = E^x *(x + 2)^(-2) * (y - 3)^4 *(1 + y)
Out[2] = (E^x (-3 + y)^4 (1 + y))/(2 + x)^2
factored = Factor[Dt[solution]] /. {Dt[x] -> dx, Dt[y] -> dy}
Out[3] = (E^x (-3 + y)^3 (2 dy - 3 dx x + dy x + 10 dy y - 2 dx x y + 5 dy x y + dx x y^2))/(2 + x)^3
diffExpr = Collect[factored[[4]], {dx, dy}]
Out[4] = dy (2 + x + 10 y + 5 x y) + dx (-3 x - 2 x y + x y^2)
dxPart = Factor[Coefficient[diffExpr, dx]]
Out[5] = x (-3 + y) (1 + y)
dyPart = Factor[Coefficient[diffExpr, dy]]
Out[6] = (2 + x) (1 + 5 y)
dxPart*dx + dyPart*dy == 0
Out[7] = dx x (-3 + y) (1 + y) + dy (2 + x) (1 + 5 y) == 0

Example: (Newton's Law of Cooling) Separable equations arise in a wide range of application problems. The time of death of a murder victim is an important question on many popular movies and television programs. How does a forensic scientist or a medical examiner determine the time of death? Human beings have a temperature of 98.6F or 36.6C. If the surrounding temperature is cooler, then the body will cool down after death. Eventually, the temperature of the body will match the temperature of the environment. We should not expect the body to cool at a constant rate either. Think of how a hot cup of coffee or tea cools. The liquid will cool quite quickly during the first few minutes but will remain relatively warm for quite a long period.

The answer to our forensic question can be found by using Newton's law of cooling, which tells us that the rate of change of the temperature of a object is proportional to the difference between the temperature of the object and the temperature of the surrounding medium. Newton's law of cooling can be easily stated as a differential equation,

\[ \frac{{\text d}T}{{\text d}t} = k \left( T- T_m \right) , \]
where T is the temperature of the object, Tm is the temperature of the surrounding medium, and k is the proportionality constant.

Suppose that the temperature of the surrounding environment is 21C, and we know from experience that a body under these conditions cools off approximately 1C during the first hour after death. In order to determine a formula for the time of death, we must solve the initial value problem

\[ \frac{{\text d}T}{{\text d}t} = k \left( T- 21 \right) , \qquad T(0)= 36.6 , \]
where T(1) = 35.6. If we rewrite the equation as
\[ \frac{{\text d}T}{T-21} = k \, {\text d}t , \]
we see that this equation is separable. Integrating both sides of the last equation, we obtain \( \ln \left( T-21 \right) = kt+C , \) since we are assuming that T > 21. Therefore,
\[ T-21 = e^{kt +C} = e^C \, e^{kt} = K\, e^{kt} , \]
where \( K= e^C \) is an arbitrary constant. The initial condition T(0) = 36.6 tells us that K = 15.6. So
\[ T (t) = 15.6\, e^{kt} +21. \]
Since we know the value of the temperature after 1 minute to be T(1) = 35.6, we get the equation to determine the coefficient k:
\[ T (1) = 35.6 = 15.6\, e^{k} +21 \qquad \Longrightarrow \qquad e^k = 14.6/15.6 \approx 0.935897 . \]
Taking logarithm, we get
\[ k = \ln \frac{14.6}{15.6} \approx -0.0662494 \qquad \Longrightarrow \qquad T(t) = 21 + 15.6 \,e^{-0.0662494 \, t} . \]

Example: A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming heat is lost only due to radiation, the differential equation for the temperature of the ball is given by

\[ \frac{{\text d}\theta}{{\text d}t} = - 2.2067\times 10^{-12} \left( \theta^4 - 81\times 10^8 \right) , \qquad \theta (0) =1200 . \]

 

Example: (Mixing Problems) There is a large class of problems in modeling known as mixing problems. These problems refer to situations where two are more substances are mixed together in a container or containers. For example, we might wish to model how chemicals are mixed together in a refinery, how pollutants are mixed together in a pond or a lake, how ingredients are mixed together when brewing beer, or even how various greenhouse gases mix together across different layers of the atmosphere.

Suppose that we have a large tank containing 4000 liters of water and that water containing 0.01 kg of salt per liter flows into the tank at a rate of 4 liters per minute. If the tank is also draining at a rate of 4 liters per minute, the water level in the tank will remain constant. We will assume that the water in the tank is constantly stirred so that the mixture of salt and water is uniform in the tank.

We can model the amount of salt in the tank using differential equations. If x(t) is the amount of salt in the tank at time t, then the rate at which the salt is changing in the tank is the difference between the rate at which salt is flowing into the tank and the rate at which it is leaving the tank, or

\[ \frac{{\text d}x}{{\text d}t} = \mbox{rate in} - \mbox{rate out} . \]
Of course, the salt flows into the tank at the rate of 4x0.01 = 0.04 kg (or 40 grams) of salt per minute. However, the rate at which the salt leaves the tank depends on x(t), the amount salt in the tank at time t. At time t, there is x(t)/4000 kg of salt in one liter. Therefore, salt flows out of the tank at a rate of 4 x(t)/4000 = x(t)/1000 kg per minute. The differential equation now becomes
\[ \frac{{\text d}x}{{\text d}t} = 0.04 - \frac{x}{1000} , \qquad x(0) =0. \]
This equation is separable,
\[ \frac{{\text d}x}{40-x} = \frac{{\text d}t}{1000} . \]
Integrating both sides of the equation, we have
\[ - \ln |40-x| = \frac{t}{1000} +k . \]
Consequently,
\[ 40-x(t) = C\, e^{-0.0001\,t} , \]
where \( C= e^{-0.0001\,k} . \) From our initial condition, we can quickly determine that C = 40 and
\[ x(t) = 40 - 40\, e^{-0.0001\,t} \]
models the amount of salt in the tank at time t.

 

 

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)