Shared11 - Differential Equations Assignment / Differential Equations Notes.sagewsOpen in CoCalc
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is

### Prerequisites:

• Intro to Sage
• Differentiation

# Differential Equations

A differential equation is an equation that contains one or more derivatives. In this lab, we will consider differential equations with only the first derivative, but in general, higher order derivatives may appear as well.

The simplest differential equations may be written in the form:

$\frac{dy}{dx}=f(x) \quad \mathrm{or} \quad \frac{dy}{dx}=f(y)\quad \mathrm{or} \quad \frac{dy}{dx}=f(x,y).$

## Examples

$\frac{dy}{dx}=2x\quad\quad\frac{dy}{dx}=y^3\quad\quad\frac{dy}{dx}=-\frac{x}{y}$

## Solving Differential Equations

To solve a differential equation, we must find the differentiable functions $y(x)$ which satisfy the equation.

## Example 1

Consider the differential equation $\frac{dy}{dx}=2x$. We know $y(x)=x^2+C$ has the correct derivative for any constant $C$, so these functions are solutions. In fact, these functions are the only solutions (this is a corollary of the Mean Value Theorem).

For any differential equation of the form $\frac{dy}{dx}=f(x)$, we find the solutions simply by finding the antiderivatives of $f$. In other words the solutions are $\displaystyle \int f(x)\, dx.$

Differential equations of the form $\frac{dy}{dx}=f(y)$ or $\frac{dy}{dx}=f(x,y)$ may sometimes be solved using a method called separation of variables.

## Example 2

Consider the differential equation $\displaystyle\frac{dy}{dx}=-\frac{x}{y}$.

We are going to pretend that $\frac{dy}{dx}$ is a fraction, and we are going to gather $x$ and $y$ terms on opposite sides of the equation (cross multiply):

$y\ dy=-x\ dx$

[This is nonsense, but it will work out in the end!]

Now integrate both sides:

$\int y\ dy=\int -x\ dx$

Integrating we get:

$y^2=-x^2+C$

Solving for $y$ gives: $y=\pm\sqrt{C-x^2}$

Or writing it another way:

$x^2+y^2=C$

For $C>0$, this is the equation of a circle centered at the origin of radius $\sqrt{C}$.

The slope of a circle at a point $(x,y)$ on the circle is $\displaystyle -\frac{x}{y}$.

## Separation of Variables

In general, a differential equation is separable if it can be written in the form $\displaystyle\frac{dy}{dx}=g(x)\cdot h(y)$.

In this case, the solutions may be found by solving:

$\int \frac{1}{h(y)}\, dy=\int g(x)\, dx$

This gives $y$ implicitly as a function of $x$. You may be able to solve the resulting equation for $y$.

Note: This technique does not work if $h(y)=0$ (i.e., $h(y)$ is the zero function), since the left side of the integral equation is never defined. Remember, we're thinking of $y$ as a function of $x$, so $h(y)$ is a composition. It's possible that $h(y)=0$ even though neither $h$ nor $y$ is the zero function.

If $y$ is a constant function such that $h(y)=0$, then this $y$ is a solution to the differential equation. However, this solution is eliminated in the above technique. Thus, you have to consider this possibility separately.

This is a subtle point, and since we do not usually care very much about constant solutions, I will ignore this in most of the examples below.

## Example 3

Consider the differential equation $\frac{dy}{dx}=y$. This is separable (with $g(x)=1$ and $h(y)=y$).

As long as $y$ is not the zero function, the solution is given by the equation $\int\frac{1}{y}\,dy=\int 1\,dx$

Integrating we get $\ln(|y|)=x+C$

Now solve for $y$: $|y|=e^{x+C}$

$y=\pm e^{x+C}$

These solutions may be written $y=\pm e^x\cdot e^C$

Remember that $C$ is an arbitrary constant, so $e^C$ is an arbitrary positive constant.

Thus, $\pm e^C$ is an arbitrary non-zero constant.

So we can write this as $y=Ke^x$, where $K$ is an arbitrary non-zero constant.

In fact, if $K=0$, we get $y=0$ (the zero function). This is actually a solution to the differential equation.

Therefore, the most general solution is $y=Ke^x$ for any constant $K$.

## Example 4

Consider the differential equation $2\sqrt{xy}\displaystyle\frac{dy}{dx}=1$.

This is separable and may be solved by integrating: $\int \sqrt{y}\, dy=\int \frac{1}{2\sqrt{x}}\,dx$

$\frac{2}{3}y^{3/2}=\sqrt{x}+C$

$y=\left(\frac{3}{2}\sqrt{x}+C\right)^{2/3}$

## Example 5

Consider the differential equation $\displaystyle\frac{dy}{dx}=\frac{e^{2x-y}}{e^{x+y}}$.

First, we may simplify the exponents: $\displaystyle\frac{dy}{dx}=\frac{e^{x}}{e^{2y}}$.

This is separable and may be solved by integrating: $\int e^{2y}\,dy=\int e^x\,dx$

$\frac{1}{2}e^{2y}=e^x+C$

$e^{2y}=2e^x+C$

$2y=\ln\left(2e^x+C\right)$

$y=\frac{1}{2}\ln\left(2e^x+C\right)$

## Example 6

Consider the differential equation $\displaystyle\frac{dy}{dx}=xy+3x-2y-6$.

First, we can factor the right-hand side to see that this is separable:

$xy+3x-2y-6=x(y+3)-2(y+3)=(x-2)(y+3)$

Therefore, the solutions may be found by integrating:

$\int \frac{1}{y+3}\,dy=\int x-2\,dx$

$\ln(|y+3|)=\frac{x^2}{2}-2x+C$

$|y+3|=e^{\left(\frac{x^2}{2}-2x+C\right)}$

$y+3=\pm e^{\left(\frac{x^2}{2}-2x+C\right)}$

$y=-3\pm e^{\left(\frac{x^2}{2}-2x+C\right)}$

These solutions may be written $\displaystyle y=-3+Ke^\left({\frac{x^2}{2}-2x}\right)$, where $K$ is any constant (I'll skip the details).

## Initial Value Problems

Some differential equations come with "initial conditions," that is, values of a solution and/or its derivatives at some specific point(s).

These conditions may allow us to solve for the "$+C$."

## Example 7

Consider the initial value problem $\frac{dy}{dx}=-y^2,\quad y(0)=\frac{1}{2}$

We can solve the differential equation with separation of variables ($g(x)=1$ and $h(y)=-y^2$).

$\int\frac{1}{-y^2}\, dy=\int 1\, dx$

$\frac{1}{y}=x+C$

$y=\frac{1}{x+C}$

Now use the initial condition $y(0)=\frac{1}{2}$ to solve for $C$.

Plug in $x=0$ and $y=\frac{1}{2}$

$\frac{1}{2}=\frac{1}{0+C}\Rightarrow C=2$

Therefore, the (unique) solution to the inital value problem is $\displaystyle y=\frac{1}{x+2}$

## Solving Differential Equations in Sage

Sage has a command called "desolve" that may be able to solve a first-order differential equation (that's "d-e-solve" for "differential equation solve", not "de-solve").

To use desolve, you must tell Sage that $y$ is a function of $x$. Then desolve is similar to the regular solve command. It takes two arguments: a differential equation (with a double equal sign), and a variable to solve for.

## Example 3 again

$\frac{dy}{dx}=y$.

y=function('y')(x)
desolve(derivative(y,x)==y,y)
show(_)

_C*e^x
$\displaystyle C e^{x}$

Recall our solution: $y=Ke^x$.

## Example 4 again

$2\sqrt{xy}\displaystyle\frac{dy}{dx}=1$

y=function('y')(x)
desolve(2*sqrt(x*y)*derivative(y,x)==1,y)
show(_)

2/3*(2*sqrt(x*y(x))*y(x) - 3*x)/sqrt(x) == _C
$\displaystyle \frac{2 \, {\left(2 \, \sqrt{x y\left(x\right)} y\left(x\right) - 3 \, x\right)}}{3 \, \sqrt{x}} = C$

Notice that Sage has given an implicit solution. You still need to solve for $y$. Let's try to get Sage to solve it for us.

I will declare a variable "C," and then I will copy and paste the above solution, removing the underscore in front of the C.

%var C
solve(2/3*(2*sqrt(x*y(x))*y(x) - 3*x)/sqrt(x) == C,y)
show(_)

[y(x) == 3/4*(C*sqrt(x) + 2*x)/sqrt(x*y(x))]
[$\displaystyle y\left(x\right) = \frac{3 \, {\left(C \sqrt{x} + 2 \, x\right)}}{4 \, \sqrt{x y\left(x\right)}}$]

Sage still left us with an implicit solution (notice y(x) on the right side). I don't know why Sage won't solve this completely.

Compare with our solution: $\displaystyle y=\left(\frac{3}{2}\sqrt{x}+C\right)^{2/3}$

## Example 5 again

$\displaystyle\frac{dy}{dx}=\frac{e^{2x-y}}{e^{x+y}}$

y=function('y')(x)
desolve(derivative(y,x)==e^(2*x-y)/e^(x+y),y)
show(_)

-e^x + 1/2*e^(2*y(x)) == _C
$\displaystyle -e^{x} + \frac{1}{2} \, e^{\left(2 \, y\left(x\right)\right)} = C$

We have to solve for $y$ again.

%var C
solve(-e^x + 1/2*e^(2*y(x)) == C,y)
show(_)

[y(x) == log(-sqrt(2*C + 2*e^x)), y(x) == 1/2*log(2*C + 2*e^x)]
[$\displaystyle y\left(x\right) = \log\left(-\sqrt{2 \, C + 2 \, e^{x}}\right)$, $\displaystyle y\left(x\right) = \frac{1}{2} \, \log\left(2 \, C + 2 \, e^{x}\right)$]

Sage has given us a strange answer. Notice that the first "solution" is the log of a negative number (as long as x is a real number), so the second solution is the one we want.

Compare with our solution: $\displaystyle y=\frac{1}{2}\ln\left(2e^x+C\right)$

## Example 6 again

$\displaystyle\frac{dy}{dx}=xy+3x-2y-6$

y=function('y')(x)
desolve(derivative(y,x)==x*y+3*x-2*y-6,y)
show(_)

(_C - 3*e^(-1/2*x^2 + 2*x))*e^(1/2*x^2 - 2*x)
$\displaystyle {\left(C - 3 \, e^{\left(-\frac{1}{2} \, x^{2} + 2 \, x\right)}\right)} e^{\left(\frac{1}{2} \, x^{2} - 2 \, x\right)}$

Compare our solution $\displaystyle y=-3+Ke^\left({\frac{x^2}{2}-2x}\right)$

Although this looks different, it is the same (distribute the multiplication over the subtraction and simplify).

## Solving an Initial Value Problem with Sage

You can solve an initial value problem by adding $[x_0,y_0]$ at the end of desolve, where $y(x_0)=y_0$ is given.

## Example 7 again

$\displaystyle\frac{dy}{dx}=-y^2,\quad y(0)=\frac{1}{2}$.

y=function('y')(x)
desolve(derivative(y,x)==-y^2,y,[0,1/2]) #notice the square brackets
show(_)

(1/y(x)) == x + 2
$\displaystyle \frac{1}{y\left(x\right)} = x + 2$

Once again, Sage left us with an implicit solution. Here's the explicit solution:

solve((1/y(x)) == x + 2,y)
show(_)

[y(x) == (1/(x + 2))]
[$\displaystyle y\left(x\right) = \frac{1}{x + 2}$]

This is the same answer we got before.

There is a whole class called "Differential Equations" that will introduce you to more complex equations and solution methods.

Next time, we'll look at a method to approximate solutions numerically.