Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

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:
Examples
Solving Differential Equations
To solve a differential equation, we must find the differentiable functions which satisfy the equation.
Example 1
Consider the differential equation . We know has the correct derivative for any constant , 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 , we find the solutions simply by finding the antiderivatives of . In other words the solutions are
Differential equations of the form or may sometimes be solved using a method called separation of variables.
Example 2
Consider the differential equation .
We are going to pretend that is a fraction, and we are going to gather and terms on opposite sides of the equation (cross multiply):
[This is nonsense, but it will work out in the end!]
Now integrate both sides:
Integrating we get:
Solving for gives:
Or writing it another way:
For , this is the equation of a circle centered at the origin of radius .
The slope of a circle at a point on the circle is .
Separation of Variables
In general, a differential equation is separable if it can be written in the form .
In this case, the solutions may be found by solving:
This gives implicitly as a function of . You may be able to solve the resulting equation for .
Note: This technique does not work if (i.e., is the zero function), since the left side of the integral equation is never defined. Remember, we're thinking of as a function of , so is a composition. It's possible that even though neither nor is the zero function.
If is a constant function such that , then this 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 . This is separable (with and ).
As long as is not the zero function, the solution is given by the equation
Integrating we get
Now solve for :
These solutions may be written
Remember that is an arbitrary constant, so is an arbitrary positive constant.
Thus, is an arbitrary non-zero constant.
So we can write this as , where is an arbitrary non-zero constant.
In fact, if , we get (the zero function). This is actually a solution to the differential equation.
Therefore, the most general solution is for any constant .

Example 4
Consider the differential equation .
This is separable and may be solved by integrating:
Example 5
Consider the differential equation .
First, we may simplify the exponents: .
This is separable and may be solved by integrating:
Example 6
Consider the differential equation .
First, we can factor the right-hand side to see that this is separable:
Therefore, the solutions may be found by integrating:
These solutions may be written ParseError: KaTeX parse error: Got function '\left' with no arguments as superscript at position 23: …ystyle y=-3+Ke^\̲l̲e̲f̲t̲({\frac{x^2}{2}…, where 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 "."
Example 7
Consider the initial value problem
We can solve the differential equation with separation of variables ( and ).
Now use the initial condition to solve for .
Plug in and
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 is a function of . 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
.
Recall our solution: .
Example 4 again
Notice that Sage has given an implicit solution. You still need to solve for . 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.
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:
Example 5 again
We have to solve for again.
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:
Example 6 again
Compare our solution ParseError: KaTeX parse error: Got function '\left' with no arguments as superscript at position 23: …ystyle y=-3+Ke^\̲l̲e̲f̲t̲({\frac{x^2}{2}…
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 at the end of desolve, where is given.
Example 7 again
.
Once again, Sage left us with an implicit solution. Here's the explicit solution:
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.