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 Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

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:

dydx=f(x)ordydx=f(y)ordydx=f(x,y).\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

dydx=2xdydx=y3dydx=xy\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)y(x) which satisfy the equation.

Example 1

Consider the differential equation dydx=2x\frac{dy}{dx}=2x. We know y(x)=x2+Cy(x)=x^2+C has the correct derivative for any constant CC, 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 dydx=f(x)\frac{dy}{dx}=f(x), we find the solutions simply by finding the antiderivatives of ff. In other words the solutions are f(x)dx.\displaystyle \int f(x)\, dx.

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

Example 2

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

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

y dy=x dxy\ dy=-x\ dx

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

Now integrate both sides:

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

Integrating we get:

y2=x2+Cy^2=-x^2+C

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

Or writing it another way:

x2+y2=Cx^2+y^2=C

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

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

Separation of Variables

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

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

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

This gives yy implicitly as a function of xx. You may be able to solve the resulting equation for yy.

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

If yy is a constant function such that h(y)=0h(y)=0, then this yy 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 dydx=y\frac{dy}{dx}=y. This is separable (with g(x)=1g(x)=1 and h(y)=yh(y)=y).

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

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

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

y=±ex+Cy=\pm e^{x+C}

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

Remember that CC is an arbitrary constant, so eCe^C is an arbitrary positive constant.

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

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

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

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

Example 4

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

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

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

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

Example 5

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

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

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

12e2y=ex+C\frac{1}{2}e^{2y}=e^x+C

e2y=2ex+Ce^{2y}=2e^x+C

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

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

Example 6

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

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

xy+3x2y6=x(y+3)2(y+3)=(x2)(y+3)xy+3x-2y-6=x(y+3)-2(y+3)=(x-2)(y+3)

Therefore, the solutions may be found by integrating:

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

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

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

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

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

These solutions may be written , where KK 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+C."

Example 7

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

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

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

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

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

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

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

12=10+CC=2\frac{1}{2}=\frac{1}{0+C}\Rightarrow C=2


Therefore, the (unique) solution to the inital value problem is y=1x+2\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 yy is a function of xx. 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

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

y=function('y')(x)
desolve(derivative(y,x)==y,y)
show(_)
_C*e^x
Cex\displaystyle C e^{x}

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

Example 4 again

2xydydx=12\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
2(2xy(x)y(x)3x)3x=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 yy. 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))]
[y(x)=3(Cx+2x)4xy(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: y=(32x+C)2/3\displaystyle y=\left(\frac{3}{2}\sqrt{x}+C\right)^{2/3}

Example 5 again

dydx=e2xyex+y\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
ex+12e(2y(x))=C\displaystyle -e^{x} + \frac{1}{2} \, e^{\left(2 \, y\left(x\right)\right)} = C

We have to solve for yy 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)]
[y(x)=log(2C+2ex)\displaystyle y\left(x\right) = \log\left(-\sqrt{2 \, C + 2 \, e^{x}}\right), y(x)=12log(2C+2ex)\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: y=12ln(2ex+C)\displaystyle y=\frac{1}{2}\ln\left(2e^x+C\right)

Example 6 again

dydx=xy+3x2y6\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)
(C3e(12x2+2x))e(12x22x)\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

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 [x0,y0][x_0,y_0] at the end of desolve, where y(x0)=y0y(x_0)=y_0 is given.

Example 7 again

dydx=y2,y(0)=12\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
1y(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))]
[y(x)=1x+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.