Skip to main content
Logo image

Introduction to Differential Equations

Section 1.4 Linear equations and the integrating factor

In section Section 1.2 we saw existence and uniqueness for a completely general first order ODE. In Section 1.3 last class we saw separable differential equations which is a specific class of first order ODEs. Now in section 1.4 we are seeing a different class of ODEs called linear ODEs, and they are particularly nice because they come with a method that always works (provided you can evaluate the integrals) to solve the ODE. We will also see that this comes with a particularly nice Existence & Uniqueness theorem that is stronger than the one for generic ODEs.
One of the most important types of equations we will learn how to solve are the so-called linear equations. In fact, the majority of the course is about linear equations. In this section we focus on the first order linear equation. A first order equation is linear if we can put it into the form:
\begin{equation} y' + p(x) y = f(x) .\tag{1.3} \end{equation}
The word “linear” means linear in \(y\) and \(y'\text{;}\) no higher powers nor functions of \(y\) or \(y'\) appear. The dependence on \(x\) can be more complicated.
Solutions of linear equations have nice properties. For example, the solution exists wherever \(p(x)\) and \(f(x)\) are defined, and has the same regularity (read: it is just as nice). But most importantly for us right now, there is a method for solving linear first order equations.
The trick is to rewrite the left-hand side of (1.3) as a derivative of a product of \(y\) with another function. To this end we find a function \(r(x)\) such that
\begin{equation*} r(x) y' + r(x) p(x) y = \frac{d}{dx}\Bigl[ r(x) y \Bigr] . \end{equation*}
This is the left-hand side of (1.3) multiplied by \(r(x)\text{.}\) So if we multiply (1.3) by \(r(x)\text{,}\) we obtain
\begin{equation*} \frac{d}{dx}\Bigl[ r(x) y \Bigr] = r(x)f(x) . \end{equation*}
Now we integrate both sides. The right-hand side does not depend on \(y\) and the left-hand side is written as a derivative of a function. Afterwards, we solve for \(y\text{.}\) The function \(r(x)\) is called the integrating factor and the method is called the integrating factor method.
We are looking for a function \(r(x)\text{,}\) such that if we differentiate it, we get the same function back multiplied by \(p(x)\text{.}\) That seems like a job for the exponential function! Let
\begin{equation*} r(x) = e^{\int p(x) \,dx} . \end{equation*}
We compute:
\begin{equation*} \begin{aligned} y' + p(x) y &= f(x) , \\ e^{\int p(x) \,dx} y' + e^{\int p(x) \,dx} p(x) y & = e^{\int p(x) \,dx} f(x) , \\ \frac{d}{dx}\left[ e^{\int p(x) \,dx} y \right] & = e^{\int p(x) \,dx} f(x) , \\ e^{\int p(x) \,dx} y & = \int e^{\int p(x) \,dx} f(x) \,dx + C , \\ y & = e^{-\int p(x) \,dx} \left( \int e^{\int p(x) \,dx} f(x) \,dx + C \right) . \end{aligned} \end{equation*}
Of course, to get a closed form formula for \(y\text{,}\) we need to be able to find a closed form formula for the integrals appearing above.

Example 1.4.1.

Solve
\begin{equation*} y' + 2xy = e^{x-x^2}, \qquad y(0) = -1 . \end{equation*}
First note that \(p(x) = 2x\) and \(f(x) = e^{x-x^2}\text{.}\) The integrating factor is \(r(x) = e^{\int p(x)\, dx} = e^{x^2}\text{.}\) We multiply both sides of the equation by \(r(x)\) to get
\begin{equation*} \begin{aligned} e^{x^2} y' + 2xe^{x^2}y & = e^{x-x^2} e^{x^2} , \\ \frac{d}{dx} \left[ e^{x^2} y \right] &= e^x . \end{aligned} \end{equation*}
We integrate
\begin{equation*} \begin{aligned} e^{x^2} y &= e^x +C , \\ y &= e^{x-x^2} + C e^{-x^2} . \end{aligned} \end{equation*}
Next, we solve for the initial condition \(-1 = y(0) = 1 + C\text{,}\) so \(C=-2\text{.}\) The solution is
\begin{equation*} y = e^{x-x^2} - 2 e^{-x^2} . \end{equation*}
Note that we do not care which antiderivative we take when computing \(e^{\int p(x) dx}\text{.}\) You can always add a constant of integration, but those constants will not matter in the end.
Exercise: Try it! Add a constant of integration to the integral in the integrating factor and show that the solution you get in the end is the same as what we got above.
Advice: Do not try to remember the formula itself, that is way too hard. It is easier to remember the process and repeat it.
Since we cannot always evaluate the integrals in closed form, it is useful to know how to write the solution in definite integral form. A definite integral is something that you can plug into a computer or a calculator. Suppose we are given
\begin{equation*} y' + p(x) y = f(x) , \qquad y(x_0) = y_0 . \end{equation*}
Look at the solution and write the integrals as definite integrals.
\begin{equation} \mybxbg{ ~~ y(x) = e^{-\int_{x_0}^x p(s)\, ds} \left( \int_{x_0}^x e^{\int_{x_0}^t p(s)\, ds} f(t) \,dt + y_0 \right). ~~ }\tag{1.4} \end{equation}
You should be careful to properly use dummy variables here. If you now plug such a formula into a computer or a calculator, it will be happy to give you numerical answers.
Let’s work through a full example.
Exercise: Check that \(y(x_0) = y_0\) in formula (1.4).
Exercise: Write the solution of the following problem as a definite integral, but try to simplify as far as you can. You will not be able to find the solution in closed form.
\begin{equation*} y' + y = e^{x^2-x}, \qquad y(0) = 10 . \end{equation*}

Remark 1.4.1.

Before we move on, we should note some interesting properties of linear equations. First, for the linear initial value problem \(y' + p(x) y = f(x)\text{,}\) \(y(x_0) = y_0\text{,}\) there is always an explicit formula (1.4) for the solution. Second, it follows from the formula (1.4) that if \(p(x)\) and \(f(x)\) are continuous on some interval \((a,b)\text{,}\) then the solution \(y(x)\) exists and is differentiable on \((a,b)\text{.}\) Compare with the simple nonlinear example we have seen previously, \(y'=y^2\text{,}\) and compare to Theorem 1.2.1.

Example 1.4.2.

Let us discuss a common simple application of linear equations. This type of problem is used often in real life. For example, linear equations are used in figuring out the concentration of chemicals in bodies of water (rivers and lakes).
A 100 liter tank contains 10 kilograms of salt dissolved in 60 liters of water. Solution of water and salt (brine) with concentration of 0.1 kilograms per liter is flowing in at the rate of 5 liters a minute. The solution in the tank is well stirred and flows out at a rate of 3 liters a minute. How much salt is in the tank when the tank is full?
Let us come up with the equation. Let \(x\) denote the kilograms of salt in the tank, let \(t\) denote the time in minutes. For a small change \(\Delta t\) in time, the change in \(x\) (denoted \(\Delta x\)) is approximately
\begin{equation*} \Delta x \approx (\text{rate in} \times \text{concentration in}) \Delta t - (\text{rate out} \times \text{concentration out}) \Delta t . \end{equation*}
Dividing through by \(\Delta t\) and taking the limit \(\Delta t \to 0\text{,}\) we see that
\begin{equation*} \frac{dx}{dt} = (\text{rate in} \times \text{concentration in}) - (\text{rate out} \times \text{concentration out}) . \end{equation*}
In our example,
\begin{equation*} \begin{aligned} \text{rate in} &= 5 , \\ \text{concentration in} &= 0.1 , \\ \text{rate out} &= 3 , \\ \text{concentration out} &= \frac{x}{\text{volume}} = \frac{x}{60+(5-3)t} . \end{aligned} \end{equation*}
Our equation is, therefore,
\begin{equation*} \frac{dx}{dt} = (5 \times 0.1) - \left(3 \frac{x}{60+2t}\right) . \end{equation*}
Or in the form (1.3)
\begin{equation*} \frac{dx}{dt} + \frac{3}{60+2t} x = 0.5 . \end{equation*}
Let us solve. The integrating factor is
\begin{equation*} r(t) = \exp \left( \int \frac{3}{60+2t} dt \right) = \exp \left( \frac{3}{2} \ln (60+2t) \right) = {(60+2t)}^{3/2} . \end{equation*}
We multiply both sides of the equation to get
\begin{equation*} \begin{aligned} {(60+2t)}^{3/2} \frac{dx}{dt} + {(60+2t)}^{3/2} \frac{3}{60+2t} x & = 0.5{(60+2t)}^{3/2} ,\\ \frac{d}{dt}\left[ {(60+2t)}^{3/2} x \right] & = 0.5{(60+2t)}^{3/2} ,\\ {(60+2t)}^{3/2} x & = \int 0.5{(60+2t)}^{3/2} dt +C ,\\ x & = {(60+2t)}^{-3/2} \int \frac{ {(60+2t)}^{3/2} }{2} dt +C{(60+2t)}^{-3/2} ,\\ x & = {(60+2t)}^{-3/2} \frac{1}{10}{(60+2t)}^{5/2} +C{(60+2t)}^{-3/2} ,\\ x & = \frac{60+2t}{10} +C{(60+2t)}^{-3/2} . \end{aligned} \end{equation*}
We need to find \(C\text{.}\) We know that at \(t=0\text{,}\) \(x=10\text{.}\) So
\begin{equation*} 10 = x(0) = \frac{60}{10} +C{(60)}^{-3/2} = 6 +C{(60)}^{-3/2} , \end{equation*}
or
\begin{equation*} C=4 ({60}^{3/2}) \approx 1859.03 . \end{equation*}
We are interested in \(x\) when the tank is full. The tank is full when \(60+2t = 100\text{,}\) or when \(t=20\text{.}\) So
\begin{equation*} \begin{split} x(20) & = \frac{60+40}{10} +C{(60+40)}^{-3/2} \\ & \approx 10 +1859.03 {(100)}^{-3/2} \approx 11.86 . \end{split} \end{equation*}
See Figure 1.10 for the graph of \(x\) over \(t\text{.}\)
The concentration when the tank is full is approximately 0.1186 \(\nicefrac{\text{kg}}{\text{liter}}\text{,}\) and we started with \(\nicefrac{1}{6}\) or 0.167 \(\nicefrac{\text{kg}}{\text{liter}}\text{.}\)
Figure 1.10. Graph of the solution \(x\) kilograms of salt in the tank at time \(t\text{.}\)

Subsection 1.4.1 Exercises

In the exercises, feel free to leave answer as a definite integral if a closed form solution cannot be found. If you can find a closed form solution, you should give that.

Exercise 1.4.1.

Solve \(y' + xy = x\text{.}\)
Solution.
\begin{equation*} \begin{aligned} p(x)=x,\quad f(x)=x,\quad r(x)=e^{\int x dx}=e^{x^2/2} \end{aligned} \end{equation*}
so we get
\begin{equation*} \begin{aligned} \frac{d}{dx}\left[e^{x^2/2}y\right]=xe^{x^2/2} \end{aligned} \end{equation*}
Integrating
\begin{equation*} \begin{aligned} e^{x^2/2}y=e^{x^2/2}+c \rightarrow y=ce^{-x^2/2}+1 \end{aligned} \end{equation*}

Exercise 1.4.2.

Solve \(y' + 6y = e^x\text{.}\)
Answer.
\(y=\frac{e^x}{7}+ce^{-6x}\)

Exercise 1.4.3.

Solve \(y' + 3x^2y = \sin(x) \, e^{-x^3}\text{,}\) with \(y(0) = 1\text{.}\)
Solution.
\begin{equation*} \begin{aligned} p(x)=3x^2,\quad f(x)=\sin(x)e^{-x^3},\quad r(x)=e^{\int 3x^2dx}=e^{x^3} \end{aligned} \end{equation*}
Multiplying by the integrating factor and integrating
\begin{equation*} \begin{aligned} e^{x^3}y=-\cos x +c \end{aligned} \end{equation*}
Applying the initial condition \(y(0)=1\) we get \(c=2\text{,}\) therefore
\begin{equation*} \begin{aligned} y=e^{-x^3}(2-\cos x) \end{aligned} \end{equation*}

Exercise 1.4.4.

Solve \(y' + \cos (x) y = \cos(x)\text{.}\)
Answer.
\(y=1+ce^{-\sin x}\)

Exercise 1.4.5.

Solve \(\frac{1}{x^2+1} \, y' + x y = 3\text{,}\) with \(y(0) = 0\text{.}\)
Answer.
\begin{equation*} \begin{aligned} p(x)=x^3+x,\quad f(x)=3(x^2+1),\quad r(x)=e^{x^4/4+x^2/2} \end{aligned} \end{equation*}
Multiplying by the integrating factor and integrating
\begin{equation*} \begin{aligned} e^{x^4/4+x^2/2}y=\int 3(x^2+1)e^{x^4/4+x^2/2}dx \end{aligned} \end{equation*}
This integral doesn’t have a closed form, so it is left as a definite integral
\begin{equation*} \begin{aligned} y=e^{x^4/4+x^2/2}\int_0^x3(t^2+1)e^{t^4/4+t^2/2} dt +y_0 \end{aligned} \end{equation*}
Applying the initial condition \(y(0)=0\) we get \(y_0=0\) so finally
\begin{equation*} \begin{aligned} y=e^{x^4/4+x^2/2}\int_0^x3(t^2+1)e^{t^4/4+t^2/2} dt \end{aligned} \end{equation*}

Exercise 1.4.6.

Suppose there are two lakes located on a stream. Clean water flows into the first lake, then the water from the first lake flows into the second lake, and then water from the second lake flows further downstream. The in and out flow from each lake is 500 liters per hour. The first lake contains 100 thousand liters of water and the second lake contains 200 thousand liters of water. A truck with 500 kg of toxic substance crashes into the first lake. Assume that the water is being continually mixed perfectly by the stream.
  1. Find the mass of toxic substance as a function of time in both lakes.
  2. When will the concentration in the first lake be below 0.001 kg per liter?
  3. When will the concentration in the second lake be maximal?
Solution.
(a) Lake 1:
\begin{equation*} \begin{aligned} x_1 &= \textrm{kg of toxic material in Lake 1} \\ \textrm{rate in} &=\textrm{rate out}=500L/h \\ \textrm{concentration in} &=0 \\ \textrm{concentration out} &= \frac{x_1}{V_1}=\frac{x_1}{10^5} \end{aligned} \end{equation*}
\begin{equation*} \begin{aligned} \frac{dx_1}{dt}=-500\frac{x_1}{10^5}\rightarrow \ln x_1=-\frac{t}{200} \end{aligned} \end{equation*}
\begin{equation*} \begin{aligned} x_1=x(0)e^{-t/200} \end{aligned} \end{equation*}
Applying the initial condition \(x(0)=500\textrm{kg}\) the equation becomes \[ x_1(t)=500e^{-t/200} \]
Lake 2:
\begin{equation*} \begin{aligned} \frac{dx_2}{dt}& =\frac{x_1(t)}{200}-\frac{x_2(t)}{400} \\ \frac{dx_2}{dt}& =\frac{5e^{-t/200}}{2}-\frac{x_2(t)}{400} \\ \end{aligned} \end{equation*}
Where \[ p(t)=\frac{1}{400},    f(t)=\frac{5e^{-t/200}}{2},    r(t)=e^{t/400} \] Multiplying and integrating \[ e^{t/400}x_2=-1000e^{-t/400}+C \] Applying the initial condition \(x_2(0)=0\) we find \(C=1000\) so \[ x_2=1000(e^{-t/400}-e^{-t/200}) \] (b) At concentration \(=0.001\rightarrow x=100\) kg. Plugging into the solution for the first lake \[ 100=500e^{-t_f/200}\rightarrow t_f\approx 321.89 \rm hours \] (c) The critical point can be obtained by setting the first derivative to zero \[ \frac{dx_2}{dt}=1000(-\frac{1}{400}e^{-t/400}+\frac{1}{200}e^{-t/200})=0 \] Solving for \(t\) we find \(t=-400\ln\frac{1}{2}\approx 277.26\) hours.

Exercise 1.4.7.

Newton’s law of cooling states that \(\frac{dx}{dt} = -k(x-A)\) where \(x\) is the temperature, \(t\) is time, \(A\) is the ambient temperature, and \(k > 0\) is a constant. Suppose that \(A = A_0 \cos (\omega t)\) for some constants \(A_0\) and \(\omega\text{.}\) That is, the ambient temperature oscillates (for example night and day temperatures).
  1. Find the general solution.
  2. In the long term, will the initial conditions make much of a difference? Why or why not?
Solution.
(a) \[ x’=-kx+kA_0\cos(\omega t) \] \[ p(t)=k,    f(t)=kA_0\cos(\omega t),    r(t)=e^{kt} \] Multiplying and integrating
\begin{equation*} e^{kt}x=kA_0\int e^{kt}\cos(\omega t)dt=kA_0 e^{kt}\left(\frac{k\cos(\omega t)+\omega\cos(\omega t)}{k^2+\omega^2}\right)+C \end{equation*}
\begin{equation*} x(t)=kA_0\left(\frac{k\cos(\omega t)+\omega \sin(\omega t)}{k^2+\omega^2}\right)+Ce^{-kt} \end{equation*}
(b) In the long term, \(t\) becomes large, so the \(e^{-kt}\) term vanishes. The initial condition is
\begin{equation*} x(0)=x_0=\frac{k^2A_0}{k^2+\omega^2}+C \end{equation*}
So we can write the solution as
\begin{equation*} x(t)=kA_0\left(\frac{k\cos(\omega t)+\omega \sin(\omega t)}{k^2+\omega^2}\right)+\left(x_0-\frac{k^2A_0}{k^2+\omega^2}\right)e^{-kt} \end{equation*}
Where the last term vanishes at large \(t\text{,}\) and so the initial condition \(x_0\) doesn’t matter.

Exercise 1.4.8.

Initially 5 grams of salt are dissolved in 20 liters of water. Brine with concentration of salt 2 grams of salt per liter is added at a rate of 3 liters a minute. The tank is mixed well and is drained at 3 liters a minute. How long does the process have to continue until there are 20 grams of salt in the tank?
Answer.
The solution is \(x(t)=40-35e^{-3t/20}\text{,}\) plugging in the values given, we get \(t\approx 3.73\) minutes.

Exercise 1.4.9.

Initially a tank contains 10 liters of pure water. Brine of unknown (but constant) concentration of salt is flowing in at 1 liter per minute. The water is mixed well and drained at 1 liter per minute. In 20 minutes there are 15 grams of salt in the tank. What is the concentration of salt in the incoming brine?
Answer.
The solution is \(x(t)=10k+ce^{-t/10}\text{,}\) where \(k\) is the concentration and \(c\) is the integration constant. Plugging in the given values we find \(k\approx 1.735\) g/L.

Exercise 1.4.10.

Solve \(y'+3 x^2 y = x^2\text{.}\)
Answer.
\(y = C e^{-x^3} + \nicefrac{1}{3}\)

Exercise 1.4.11.

Solve \(y'+ 2\sin(2x) y = 2\sin(2x)\text{,}\) \(y(\nicefrac{\pi}{2}) = 3\text{.}\)
Answer.
\(y = 2 e^{\cos(2x)+1} + 1\)

Exercise 1.4.12.

Suppose a water tank is being pumped out at 3 \(\nicefrac{\text{L}}{\text{min}}\text{.}\) The water tank starts at 10 L of clean water. Water with toxic substance is flowing into the tank at 2 \(\nicefrac{\text{L}}{\text{min}}\text{,}\) with concentration \(20t\) \(\nicefrac{\text{g}}{\text{L}}\) at time \(t\text{.}\) When the tank is half empty, how many grams of toxic substance are in the tank (assuming perfect mixing)?
Answer.
\(250\) grams

Exercise 1.4.13.

Suppose we have bacteria on a plate and suppose that we are slowly adding a toxic substance such that the rate of growth is slowing down. That is, suppose that \(\frac{dP}{dt} = (2-0.1\,t)P\text{.}\) If \(P(0) = 1000\text{,}\) find the population at \(t=5\text{.}\)
Answer.
\(P(5) = 1000 e^{2 \times 5 - 0.05 \times {5}^2} = 1000 e^{8.75} \approx 6.31 \times {10}^6\)

Exercise 1.4.14.

A cylindrical water tank has water flowing in at \(I\) cubic meters per second. Let \(A\) be the area of the cross section of the tank in meters. Suppose water is flowing from the bottom of the tank at a rate proportional to the height of the water level. Set up the differential equation for \(h\text{,}\) the height of the water, introducing and naming constants that you need. You should also give the units for your constants.
Answer.
\(Ah' = I - kh\text{,}\) where \(k\) is a constant with units \(\unitfrac{m^2}{s}\text{.}\)
For a higher quality printout use the PDF version: https://www.jirka.org/diffyqs/diffyqs.pdf