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.
Math 157: Intro to Numerical Solutions to ODE
Yiwei Sang, Winter 2018
Ordinary Differential Equation (ODE)
An ordinary differential equation (ODE) is a differential equation that contains one or more functions of one independent variable and its derivatives.
The term ordinary is used in contrast with the term partial differential equation which may be with respect to more than one independent variable.
The solutions to an ODE are functions that satisfy the equation.
The order of an ODE is the number of the highest derivatives in the ODE
Linear ODE: A linear ODE having independent variable t and the dependent variable y is an ODE of the form
Examples of ODE:
Solving ODE using SageMath
Example - Initial Value Problem (IVP)
Sage's desolve may not be able to solve some difficult ODE.
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-10-5527f271bbaa> in <module>()
----> 1 desolve(sqrt(y)*diff(y,x)+e**(y)+cos(x)-sin(x+y)==Integer(0),y) # This will cause an error
/ext/sage/sage-8.1/local/lib/python2.7/site-packages/sage/functions/other.pyc in sqrt(x, *args, **kwds)
1995 return sqrt(x)
1996 try:
-> 1997 return x.sqrt(*args, **kwds)
1998 # The following includes TypeError to catch cases where sqrt
1999 # is called with a "prec" keyword, for example, but the sqrt
/ext/sage/sage-8.1/src/sage/structure/element.pyx in sage.structure.element.CommutativeRingElement.sqrt (build/cythonized/sage/structure/element.c:19663)()
2925 from sage.rings.ring import IntegralDomain
2926 P = self._parent
-> 2927 is_sqr, sq_rt = self.is_square(root=True)
2928 if is_sqr:
2929 if all:
/ext/sage/sage-8.1/src/sage/structure/element.pyx in sage.structure.element.CommutativeRingElement.is_square (build/cythonized/sage/structure/element.c:19483)()
2830 framework.
2831 """
-> 2832 raise NotImplementedError("is_square() not implemented for elements of %s" % self.parent())
2833
2834 def sqrt(self, extend=True, all=False, name=None):
NotImplementedError: is_square() not implemented for elements of Multivariate Polynomial Ring in x, y over Rational Field
Application: Falling Body
Consider a mass m falling due to gravity. We orient coordinates to that downward is positive. Let denote the distance the mass has fallen at time t and is the velocity at time t. Let assume only two forces act: the force due to gravity , and the force due to air resistance . So . We assume that air resistance is proportional to velocity: , where is a constant. By Newton's second law: . Putting these all together gives , or
Example: Suppose we have a mass 100kgs(with chute). The chute is released 30 secods after the jump from a height of 2000m. The force due to air resistence is giving by , where when chute closed and when chute open. Suppose g = 9.8. Find
(a) The velocity functions during the time when the chute is closed (i.e seconds)
The velocity at t = 30 is
(b) The velocity functions during the time when the chute is open (i.e., seconds)
Let's plot the results for both two periods
Numerical solutions - Euler's method
Euler's Method is intended for approximating solutions to differential equations that cannot be solved with a nice formula.
Say we have a generic initial value problem with . We are not able to find a nice formula for the solution . We know if is a continuous function, there exist a solution. Our goal is to say as much as we can about this solution, even though we may not be able to write down a formula for it.
Let's think about tangent line of the solution curve, since the tangent line is a good approximation to a curve near the point . The equation of the tangent line to the solution at the point is . Let's move a short distance along the tangent line by increasing x by some small amount h, thwn we arrive at a new point, which we will call . We can say the point is a almost on the solution curve . We can now find the tangent line to the solution at this new point , and then we increase x by the small amount h again, and come up with a new point . Continuing with this method, we use the value of y found at each step to calculate the next value of y. This process can be stated by Euler's Formula:
These codes use euler's method to find the numerical solution of the ODE , with initial condition , with step size 0.1 for x from 0 to 1
Plot of euler's method for the ODE , with intial condition , with step size 1/3, 0.1, for x from 0 to 1
Remark: With smaller stepsize, the graph tends to be more smooth. The curve approximated will be more like the actual solution curve.
Homogeneous Systems of Linear ODE
Consider the system of differential equations
The general solution of this system
Now give some initial condition
scipy.integrate.ode
scipy.integrate.ode is a generic interface class to numeric integrators
Example:
Exercise 1
Consider a tank with 200 liters of salt-water solution, 30 grams of which is salt. Pouring into the tank is a brine solution at a rate of 4 liters/minute and with a concentration of 1 grams per liter. The “well-mixed” solution pours out at a rate of 5 liters/minute. Find the amount at time t, and plot the amount for t in [0, 200]
You can learn more about the mixing problem in https://docs.google.com/document/d/1aLDQwp9zQ0KL90y7C8S5HbuYKDITEq2X_qUMK7w2Pkk/edit
Solution:
Exercise 2
Consider:
Sage cannot solve this (implicit) solution for x(t). Show the implicit plot.