# Ordinary Differential Equations/First Order Linear 1

First Order Differential Equations

## What is a linear first order equation?

A linear first order equation is an equation in the form

$\frac{dy}{dx}+P(x)y=Q(x)$.

The simpliest case of which is shown below in Example 1 where $P(x)$ and $Q(x)$ are not functions but simple constants. Linear first order equations are important because they show up frequently in nature and physics, and can be solved by a fairly straight forward method.

We first begin by motivating the method. First recall that the product rule states that $[f(x)\cdot g(x)]'=f'(x)g(x)+f(x)g'(x)$. The key observation is that the left hand side of the first order ODE appears to be fairly similar to the product rule. There is one term with a derivative of $y$, and there is one term without a derivative of $y$.

Let's compare the left hand side

$\frac{dy}{dx}+P(x)y$

to the product rule applied to the product involving $y$. Notice that $\frac{d}{dx}$ is equal to the prime $'$ operator.

$\frac{d}{dx}\Big[\mu(x)y(x)\Big]=\mu(x)\frac{dy}{dx}+\frac{d\mu}{dx}y(x)$.

The first fact we notice is that on the left hand side of our first order ODE there is no $\mu(x)$ in front of the term $\frac{dy}{dx}$. We can try to correct that by multiplying through by $\mu(x)$. Then the ODE would become:

$\mu(x)\frac{dy}{dx}+\mu(x)P(x)y=\mu(x)Q(x).$

But now this would only look like the product rule if $\frac{d\mu}{dx}=\mu(x)P(x)$ and hence $\mu(x)\frac{dy}{dx}+y(x)\frac{d\mu}{dx} = \mu(x)Q(x)$, which we do not know by now. But this simple equation can easily be solved, as it is a separable equation. Dividing both sides of it by $\mu(x)$ and integrating we get:

$\int\frac{1}{\mu(x)}\frac{d\mu}{dx}\,dx = \int P(x)\,dx$

Integrating by substitution the integral on the left hand side is simply $\ln |\mu(x)|$. Thus pulling over the natural logarithm to the right, we have:

$\mu(x)=e^{\int P(x)\,dx}.$

This is very important and is called the 'integrating factor'. So our original ODE can be rewritten as

$e^{\int P(x)\,dx}\frac{dy}{dx}+P(x)e^{\int P(x)\,dx }y=e^{\int P(x)\,dx}Q(x)$

But now the whole point of introducing $\mu(x)$ was we were trying to turn the left hand side of this ODE into $[\mu(x)y]'$ for easily integrating it, which is exactly what we have done. Thus

$[e^{\int P(x)\,dx}y]'=e^{\int P(x)\,dx}Q(x).$

So we may integrate both sides and then solve for y to get

$e^{\int P(x)\,dx}y=\int e^{\int P(x)\,dx}Q(x)\,dx.$
$y=e^{-\int P(x)\,dx}\int e^{\int P(x)\,dx}Q(x)\,dx.$

Below we provide another derivation of this formula.

### Example 1: P and Q are Constant

Let's say we have the equation
$\frac{dy}{dx}+my=n$
where n and m are constants. Solve this for y.
Step 1: Find the integrating factor, $e^{\int P(x)dx}$,
with P(x) = m and the integration of a constant being
$\int mdx=mx+C$
yielding
$e^{\int P(x)dx}=e^{mx}e^c=Ce^{mx}$
$e^c$ is collpsing into C. Letting C=1, we get emx.
Step 2: Multiply through by the integrating factor just found.
$e^{mx}\frac{dy}{dx}+e^{mx}my=ne^{mx} \,$
Step 3: Recognize that the left hand side is $\frac{d}{dx} \Big[e^{\int P(x)dx}y\Big]$,
that is subject to the product rule, giving us.
$\frac{d}{dx} \Big[e^{mx}y\Big]=ne^{mx}$
Step 4: Integrate both sides with respect to x,
with the left hand side now easily integrated.
$\int (\frac{d}{dx} e^{mx}y)dx=\int ne^{mx}dx$
$e^{mx}y=\frac{n}{m}e^{mx}+C$
Step 5: And finally solve for y
$y=\frac{n}{m}+Ce^{-mx}$

### Example 2: P and Q are x

Take the equation
$\frac{dy}{dx}+xy=x$
Solve for y.
Step 1: Find $e^{\int P(x)dx}$, P(x) = x and the integral of x being
$\int x=\frac{1}{2}x^2+C$
yielding
$e^{\int P(x)dx}=Ce^{\frac{1}{2}x^2}$
Letting C=1, we get $e^{\frac{1}{2}x^2}$
Step 2: Multiply through
$e^{\frac{1}{2}x^2}\frac{dy}{dx}+e^{\frac{1}{2}x^2}xy=e^{\frac{1}{2}x^2}x$
Step 3: Recognize that the left hand is $\frac{d}{dx} e^{\int P(x)dx}y$, giving
$\frac{d}{dx} e^{\frac{1}{2}x^2}y=e^{\frac{1}{2}x^2}x$
Step 4: Integrate
$\int (\frac{d}{dx} e^{\frac{1}{2}x^2}y)dx=\int xe^{\frac{1}{2}x^2}dx$
$e^{\frac{1}{2}x^2}y=e^{\frac{1}{2}x^2}+C$
Step 5: Solve for y
$y=1+\frac{C}{e^{\frac{1}{2}x^2}}$

### Example 3: P and Q are Unrelated

Take the equation
$\frac{dy}{dx}+\frac{1}{x}y=x$
Solve for y.

Before we begin we notice that for the function y, we are not guaranteed a solution for all x because the coefficient is discontinuous at 0. So either we can find a solution in $x\in (0,\infty)$ or for $x\in(-\infty,0)$. For the purposes of this solution we shall assume $x\in (0,\infty)$.

Step 1: Find $e^{\int P(x)dx}$, $P(x) = 1/x$,
and the integration of 1/x being
$\int \frac{1}{x}\,dx=\ln(x)+C$
yielding
$e^{\int P(x)dx}=Ce^{\ln(x)}=Cx$
Letting C=1, we get x.
Step 2: Multiply through
$x\frac{dy}{dx}+y=x^2$
Step 3: Recognize that the left hand is $\frac{d}{dx} e^{\int P(x)dx}y$
$\frac{d}{dx} [xy]=x^2$
Step 4: Integrate both sides w.r.t.x.
$\int \frac{d}{dx} [xy]\,dx=\int x^2dx$
$xy=\frac{1}{3}x^3+C$
Step 5: Solve for y
$y=\frac{1}{3}x^2+\frac{C}{x}$

## An alternative derivation

There is a strategy for solving inhomogeneous equations called variation of parameters that proceeds as follows. First find the general solution to the homogeneous equations. Guess that the solutions of the inhomogeneous equations can be written in the same way as for the solutions of the homogeneous equations, except where the unknown constants are replaced by unknown functions. Then you plug a solution of this form back in to the equation to see if you can find what the unknown function is.

This method works well in case of first order linear equations and gives us an alternative derivation of our formula for the solution which we present below.

1. First, set Q(x) equal to 0 so that you end up with a homogeneous linear equation (the usage of this term is to be distinguished from the usage of "homogeneous" in the previous sections).

$\frac{dy}{dx}+P(x)y=0$

2. This equation is separable, so separate it:

$\frac{dy}{y}+P(x)dx=0$

3. Solve the equation to obtain the solution

$y=Ce^{-\int P(x) dx}$

4. Now let C be replaced by a variable function of x, and denote it g(x).

$y=g(x)e^{-\int P(x) dx}$

5. Substitute the previous equation into the differential equation to get

$g'(x)e^{-\int P(x) dx}=Q(x)$

6. Now solve for g(x) to get

$g(x)=C+\int Q(x)e^{\int P(x) dx}dx$

1. Now obtain the general solution by plugging in this expression in g(x):

$y=g(x)e^{-\int P(x) dx}=Ce^{-\int P(x) dx}+e^{-\int P(x) dx}\int Q(x)e^{\int P(x) dx}dx$

## Making Linear Equations from Non-Linear Equations

Sometimes a non-linear equation, which is not solvable like this, can be made linear, and more easily solvable, by applying a substitution.

### Example

$y\frac{dy}{dx}+xy^2=5x$
Let's make the following substitution:
$v=y^2 \,$
$\frac{dv}{dx}=2y\frac{dy}{dx}$
Plugging in, we get
$\frac{1}{2}\frac{dv}{dx}+xv=5x$
$\frac{dv}{dx}+2xv=10x$
We can then solve as a linear equation in v, using the step-by-step method above:
Step 1: Find the integrating factor:
$e^{\int P(x)dx}$
$e^{\int 2x dx}=e^{x^2+C}$
$e^{\int P(x)dx}=Ce^{x^2}$
Letting C=1 for convenience, we get $e^{x^2}$ as our integrating factor.
Step 2: Multiply through
$e^{x^2}\frac{dv}{dx}+e^{x^2}xv=10e^{x^2}x$
Step 3: Recognize that the left hand is $\frac{d}{dx} e^{\int P(x)dx}v$
$\frac{d}{dx} e^{x^2}v=10e^{x^2}x$
Step 4: Integrate both sides w.r.t.x.
$\int (\frac{d}{dx} e^{x^2}v)dx=\int 10e^{x^2}xdx$
$e^{x^2}v=5e^{x^2}+C$
Step 5: Solve for v.
$v=5+\frac{C}{e^{x^2}}$
Now that we have v, solve for y.
$v=y^2 \,$
$y^2=5+\frac{C}{e^{x^2}} \,$