# ODEs


{{< include ../_common_code.qmd >}}

This section uses these add-on packages:


```{julia}
using CalculusWithJulia
using Plots
using SymPy
```


---


Some relationships are easiest to describe in terms of rates or derivatives. For example:


  * Knowing the speed of a car and how long it has been driving can summarize the car's location.
  * One of Newton's famous laws, $F=ma$, describes the force on an  object of mass $m$ in terms of the acceleration. The acceleration  is the derivative of velocity, which in turn is the derivative of  position. So if we know the rates of change of $v(t)$ or $x(t)$, we  can differentiate to find $F$.
  * Newton's law of [cooling](http://tinyurl.com/z4lmetp). This describes the temperature change in an object due to a difference in temperature with the object's surroundings. The formula being, $T'(t) = -r \left(T(t) - T_a \right)$, where $T(t)$ is temperature at time $t$ and $T_a$ the ambient temperature.
  * [Hooke's law](http://tinyurl.com/kbz7r8l) relates force on an object to the position on the object, through $F = k x$. This is appropriate for many systems involving springs. Combined with Newton's law $F=ma$, this leads to an equation that $x$ must satisfy: $m x''(t) = k x(t)$.


## Motion with constant acceleration


Let's consider the case of constant acceleration. This describes how nearby objects fall to earth, as the force due to gravity is assumed to be a constant, so the acceleration is the constant force divided by the constant mass.


With constant acceleration, what is the velocity?


As mentioned, we have $dv/dt = a$ for any velocity function $v(t)$, but in this case, the right hand side is assumed to be constant. How does this restrict the possible functions, $v(t)$, that the velocity can be?


Here we can integrate to find that any answer must look like the following for some constant of integration:


$$
v(t) = \int \frac{dv}{dt} dt = \int a dt = at + C.
$$

If we are given the velocity at a fixed time, say $v(t_0) = v_0$, then we can use the definite integral to get:


$$
v(t) - v(t_0) = \int_{t_0}^t a dt = at - a t_0.
$$

Solving, gives:


$$
v(t) = v_0 + a (t - t_0).
$$

This expresses the velocity at time $t$ in terms of the initial velocity, the constant acceleration and the time duration.


A natural question might be, is this the *only* possible answer? There are a few useful ways to think about this.


First, suppose there were another, say $u(t)$. Then define $w(t)$ to be the difference: $w(t) = v(t) - u(t)$. We would have that $w'(t) = v'(t) - u'(t) = a - a = 0$. But from the mean value theorem, a function whose derivative is *continuously* $0$, will necessarily be a constant. So at most, $v$ and $u$ will differ by a constant, but if both are equal at $t_0$, they will be equal for all $t$.


Second, since the derivative of any solution is a continuous function, it is true by the fundamental theorem of calculus that it *must* satisfy the form for the antiderivative. The initial condition makes the answer unique, as the indeterminate $C$ can take only one value.


Summarizing, we have


> If $v(t)$ satisfies the equation: $v'(t) = a$, $v(t_0) = v_0,$ then the unique solution will be $v(t) = v_0 + a (t - t_0)$.



Next, what about position? Here we know that the time derivative of position yields the velocity, so we should have that the unknown position function satisfies this equation and initial condition:


$$
x'(t) = v(t) = v_0 + a (t - t_0), \quad x(t_0) = x_0.
$$

Again, we can integrate to get an answer for any value $t$:


\begin{align*}
x(t) - x(t_0) &= \int_{t_0}^t \frac{dx}{dt} dt     \\
&= (v_0t + \frac{1}{2}a t^2 - at_0 t) |_{t_0}^t    \\
&= (v_0 - at_0)(t - t_0) + \frac{1}{2} a (t^2 - t_0^2).
\end{align*}

There are three constants: the initial value for the independent variable, $t_0$, and the two initial values for the velocity and position, $v_0, x_0$.  Assuming $t_0 = 0$, we can simplify the above to get a formula familiar from introductory physics:


$$
x(t) = x_0 + v_0 t + \frac{1}{2} at^2.
$$

Again, the mean value theorem can show that with the initial value specified this is the only possible solution.


## First-order initial-value problems


The two problems just looked at can be summarized by the following. We are looking for solutions to an equation of the form (taking $y$ and $x$ as the variables, in place of $x$ and $t$):


$$
y'(x) = f(x), \quad y(x_0) = y_0.
$$

This is called an *ordinary differential equation* (ODE), as it is an equation involving the ordinary derivative of an unknown function, $y$.


This is called a first-order, ordinary differential equation, as there is only the first derivative involved.


This is called an initial-value problem, as the value at the initial point $x_0$ is specified as part of the problem.


#### Examples


Let's look at a few more examples, and then generalize.


##### Example: Newton's law of cooling


Consider the ordinary differential equation given by Newton's law of cooling:


$$
T'(t) = -r (T(t) - T_a), \quad T(0) = T_0
$$

This equation is also first order, as it involves just the first derivative, but notice that on the right hand side is the function $T$, not the variable being differentiated against, $t$.


As we have a difference on the right hand side, we rename the variable through $U(t) = T(t) - T_a$. Then, as $U'(t) = T'(t)$, we have the equation:


$$
U'(t) = -r U(t), \quad U(0) = U_0.
$$

This shows that the rate of change of $U$ depends on $U$. Large positive values indicate a negative rate of change - a push back towards the origin, and large negative values of $U$ indicate a positive rate of change - again, a push back towards the origin. We shouldn't be surprised to either see a steady decay towards the origin, or oscillations about the origin.


What will we find? This equation is different from the previous two equations, as the function $U$ appears on both sides. However, we can rearrange to get:


$$
\frac{dU}{dt}\frac{1}{U(t)} = -r.
$$

This suggests integrating both sides, as before. Here we do the "$u$"-substitution $u = U(t)$, so $du = U'(t) dt$:


$$
-rt + C = \int \frac{dU}{dt}\frac{1}{U(t)} dt =
\int \frac{1}{u}du = \log(u).
$$

Solving gives: $u = U(t) = e^C e^{-rt}$. Using the initial condition forces $e^C = U(t_0) = T(0) - T_a$ and so our solution in terms of $T(t)$ is:


$$
T(t) - T_a = (T_0 - T_a) e^{-rt}.
$$

In words, the initial difference in temperature of the object and the environment exponentially decays to $0$.


That is, as $t > 0$ goes to $\infty$, the right hand will go to $0$ for $r > 0$, so $T(t) \rightarrow T_a$ - the temperature of the object will reach the ambient temperature. The rate of this is largest when the difference between $T(t)$ and $T_a$ is largest, so when objects are cooling the statement "hotter things cool faster" is appropriate.


A graph of the solution for $T_0=200$ and $T_a=72$ and $r=1/2$ is made as follows. We've added a few line segments from the defining formula, and see that they are indeed tangent to the solution found for the differential equation.


```{julia}
#| echo: false
let
    T0, Ta, r = 200, 72, 1/2
    f(u, t) = -r*(u - Ta)
    v(t) = Ta + (T0 - Ta) * exp(-r*t)
    p = plot(v, 0, 6, linewidth=4, legend=false)
    [plot!(p, x -> v(a) + f(v(a), a) * (x-a), 0, 6) for a in 1:2:5]
    p
end
```

The above is implicitly assuming that there could be no other solution, than the one we found. Is that really the case? We will see that there is a theorem that can answer this, but in this case, the trick of taking the difference of two equations satisfying the equation leads to the equation $W'(t) = r W(t), \text{ and } W(0) = 0$. This equation has a general solution of $W(t) = Ce^{rt}$ and the initial condition forces $C=0$, so $W(t) = 0$, as before. Hence, the initial-value problem for Newton's law of cooling has a unique solution.


In general, the equation could be written as (again using $y$ and $x$ as the variables):


$$
y'(x) = g(y), \quad y(x_0) = y_0
$$

This is called an *autonomous*, first-order ODE, as the right-hand side does not depend on $x$ (except through $y(x)$).


Let $F(y) = \int_{y_0}^y du/g(u)$, then a solution to the above is $F(y) = x - x_0$, assuming $1/g(u)$ is integrable.


##### Example: Toricelli's law


[Toricelli's Law](http://tinyurl.com/hxvf3qp) describes the speed a jet of water will leave a vessel through an opening below the surface of the water. The formula is $v=\sqrt{2gh}$, where $h$ is the height of the water above the hole and $g$ the gravitational constant. This arises from equating the kinetic energy gained, $1/2 mv^2$ and potential energy lost, $mgh$, for the exiting water.


An application of Torricelli's law is to describe the volume of water in a tank over time, $V(t)$. Imagine a cylinder of cross sectional area $A$ with a hole of cross sectional area $a$ at the bottom, Then $V(t) = A h(t)$, with $h$ giving the height. The change in volume over $\Delta t$ units of time must be given by the value $a v(t) \Delta t$, or


$$
V(t+\Delta t) - V(t) = -a v(t) \Delta t = -a\sqrt{2gh(t)}\Delta t
$$

This suggests the following formula, written in terms of $h(t)$ should apply:


$$
A\frac{dh}{dt} = -a \sqrt{2gh(t)}.
$$

Rearranging, this gives an equation


$$
\frac{dh}{dt} \frac{1}{\sqrt{h(t)}} = -\frac{a}{A}\sqrt{2g}.
$$

Integrating both sides yields:


$$
2\sqrt{h(t)} = -\frac{a}{A}\sqrt{2g} t + C.
$$

If $h(0) = h_0 = V(0)/A$, we can solve for $C = 2\sqrt{h_0}$, or


$$
\sqrt{h(t)} = \sqrt{h_0} -\frac{1}{2}\frac{a}{A}\sqrt{2g} t.
$$

Setting $h(t)=0$ and solving for $t$ shows that the time to drain the tank would be $(2A)/(a\sqrt{2g})\sqrt{h_0}$.


##### Example


Consider now the equation


$$
y'(x) = y(x)^2, \quad y(x_0) = y_0.
$$

This is called a *non-linear* ordinary differential equation, as the $y$ variable on the right hand side presents itself in a non-linear form (it is squared). These equations may have solutions that are not defined for all times.


This particular problem can be solved as before by moving the $y^2$ to the left hand side and integrating to yield:


$$
y(x) = - \frac{1}{C + x},
$$

and with the initial condition:


$$
y(x) = \frac{y_0}{1 - y_0(x - x_0)}.
$$

This answer can demonstrate *blow-up*. That is, in a finite range for $x$ values, the $y$ value can go to infinity. For example, if the initial conditions are $x_0=0$ and $y_0 = 1$, then $y(x) = 1/(1-x)$ is only defined for $x \geq x_0$ on $[0,1)$, as at $x=1$ there is a vertical asymptote.


## Separable equations


We've seen equations of the form $y'(x) = f(x)$ and $y'(x) = g(y)$ both solved by integrating. The same tricks will work for equations of the form $y'(x) = f(x) \cdot g(y)$. Such equations are called *separable*.


Basically, we equate up to constants


$$
\int \frac{dy}{g(y)} = \int f(x) dx.
$$

For example, suppose we have the equation


$$
\frac{dy}{dx} = x \cdot y(x), \quad y(x_0) = y_0.
$$

Then we can find a solution, $y(x)$ through:


$$
\int \frac{dy}{y} = \int x dx,
$$

or


$$
\log(y) = \frac{x^2}{2} + C
$$

Which yields:


$$
y(x) = e^C e^{\frac{1}{2}x^2}.
$$

Substituting in $x_0$ yields a value for $C$ in terms of the initial information $y_0$ and $x_0$.


## Symbolic solutions


Differential equations are classified according to their type. Different types have different methods for solution, when a solution exists.


The first-order initial value equations we have seen can be described generally by


\begin{align*}
y'(x) &= F(y,x),\\
y(x_0) &= x_0.
\end{align*}

Special cases include:


  * *linear* if the function $F$ is linear in $y$;
  * *autonomous* if $F(y,x) = G(y)$ (a function of $y$ alone);
  * *separable* if $F(y,x) = G(y)H(x)$.


As seen, separable equations are approached by moving the "$y$" terms to one side, the "$x$" terms to the other and integrating. This also applies to autonomous equations then. There are other families of equation types that have exact solutions, and techniques for solution, summarized at this [Wikipedia page](http://tinyurl.com/zywzz4q).


Rather than go over these various families, we demonstrate that `SymPy` can solve many of these equations symbolically.


The `solve` function in `SymPy` solves equations for unknown *variables*. As a differential equation involves an unknown *function* there is a different function, `dsolve`. The basic idea is to describe the differential equation using a symbolic function and then call `dsolve` to solve the expression.


Symbolic functions are defined by the  `@syms` macro (also see `?symbols`) using parentheses to distinguish a function from a variable:


```{julia}
@syms x u() # a symbolic variable and a symbolic function
```

We will solve the following, known as the *logistic equation*:


$$
u'(x) = a u(1-u), \quad a > 0
$$

Before beginning, we look at the form of the equation. When $u=0$ or $u=1$ the rate of change is $0$, so we expect the function might be bounded within that range. If not, when $u$ gets bigger than $1$, then the slope is negative and when $u$ gets less than $0$, the slope is positive, so there will at least be a drift back to the range $[0,1]$. Let's see exactly what happens. We define a parameter, restricting `a` to be positive:


```{julia}
@syms a::positive
```

To specify a derivative of `u` in our equation we can use `diff(u(x),x)` but here, for visual simplicity, use the `Differential` operator, as follows:


```{julia}
D = Differential(x)
eqn = D(u)(x) ~ a * u(x) * (1 - u(x)) # use l \Equal[tab] r, Eq(l,r), or just l - r
```

In the above, we evaluate the symbolic function at the variable `x` through the use of `u(x)` in the expression. The equation above uses `~` to combine the left- and right-hand sides as an equation in `SymPy`. (A unicode equals is also available for this task). This is a shortcut for `Eq(l,r)`, but even just using `l - r` would suffice, as the default assumption for an equation is that it is set to `0`.


The `Differential` operation is borrowed from the `ModelingToolkit` package, which will be introduced later.


To finish, we call `dsolve` to find a solution (if possible):


```{julia}
out = dsolve(eqn)
```

This answer - to a first-order equation - has one free constant, `C_1`, which can be solved for from an initial condition. We can see that when $a > 0$, as $x$ goes to positive infinity the solution goes to $1$, and when $x$ goes to negative infinity, the solution goes to $0$ and otherwise is trapped in between, as expected.


The limits are confirmed by investigating  the limits of the right-hand:


```{julia}
limit(rhs(out), x => oo), limit(rhs(out), x => -oo)
```

We can confirm that the solution is always increasing, hence trapped within $[0,1]$ by observing that the derivative is positive when `C₁` is positive:


```{juila}
diff(rhs(out),x)
```

Suppose that $u(0) = 1/2$. Can we solve for $C_1$ symbolically? We can use `solve`, but first we will need to get the symbol for `C_1`:


```{julia}
eq = rhs(out)    # just the right hand side
C1 = first(setdiff(free_symbols(eq), (x,a))) # fish out constant, it is not x or a
c1 = solve(eq(x=>0) - 1//2, C1)
```

And we plug in with:


```{julia}
eq(C1 => c1[1])
```

That's a lot of work. The `dsolve` function in `SymPy` allows initial conditions to be specified for some equations. In this case, ours is $x_0=0$ and $y_0=1/2$. The extra arguments passed in through a dictionary to the  `ics` argument:


```{julia}
x0, y0 = 0, Sym(1//2)
dsolve(eqn, u(x), ics=Dict(u(x0) => y0))
```

(The one subtlety is the need to write the rational value as a symbolic expression, as otherwise it will get converted to a floating point value prior to being passed along.)


##### Example: Hooke's law


In the first example, we solved for position, $x(t)$, from an assumption of constant acceleration in two steps. The equation relating the two is a second-order equation: $x''(t) = a$, so two constants are generated. That a second-order equation could be reduced to two first-order equations is not happy circumstance, as it can always be done. Rather than show the technique though, we demonstrate that `SymPy` can also handle some second-order ODEs.


Hooke's law relates the force on an object to its position via $F=ma = -kx$, or $x''(t) = -(k/m)x(t)$.


Suppose $k > 0$. Then we can solve, similar to the above, with:


```{julia}
@syms k::positive m::positive
D2 = D ∘ D  # takes second derivative through composition
eqnh = D2(u)(x) ~ -(k/m)*u(x)
dsolve(eqnh)
```

Here we find two constants, as anticipated, for we would guess that two integrations are needed in the solution.


Suppose the spring were started by pulling it down to a bottom and releasing. The initial position at time $0$ would be $a$, say, and initial velocity $0$. Here we get the solution specifying initial conditions on the function and its derivative (expressed through `u'`):


```{julia}
dsolve(eqnh, u(x), ics = Dict(u(0) => -a, D(u)(0) =>  0))
```

We get that the motion will follow $u(x) = -a \cos(\sqrt{k/m}x)$. This is simple oscillatory behavior. As the spring stretches, the force gets large enough to pull it back, and as it compresses the force gets large enough to push it back. The amplitude of this oscillation is $a$ and the period $2\pi/\sqrt{k/m}$. Larger $k$ values mean shorter periods; larger $m$ values mean longer periods.


##### Example: the pendulum


The simple gravity [pendulum](http://tinyurl.com/h8ys6ts) is an idealization of a physical pendulum that models a "bob" with mass $m$ swinging on a massless rod of length $l$ in a frictionless world governed only by the gravitational constant $g$. The motion can be described by this differential equation for the angle, $\theta$, made from the vertical:


$$
\theta''(t) + \frac{g}{l}\sin(\theta(t)) = 0
$$

Can this second-order equation be solved by `SymPy`?


```{julia}
@syms g::positive l::positive theta()=>"θ"
eqnp = D2(theta)(x) + g/l*sin(theta(x))
```

Trying to do so, can cause `SymPy` to hang or simply give up and repeat its input; no easy answer is forthcoming for this equation.


In general, for the first-order initial value problem characterized by $y'(x) = F(y,x)$, there are conditions ([Peano](http://tinyurl.com/h663wba) and [Picard-Lindelof](http://tinyurl.com/3rbde5e)) that can guarantee the existence (and uniqueness) of equation locally, but there may not be an accompanying method to actually find it. This particular problem has a solution, but it can not be written in terms of elementary functions.


However, as [Huygens](https://en.wikipedia.org/wiki/Christiaan_Huygens) first noted, if the angles involved are small, then we approximate the solution through the linearization $\sin(\theta(t)) \approx \theta(t)$. The resulting equation for an approximate answer is just that of Hooke:


$$
\theta''(t) + \frac{g}{l}\theta(t) = 0
$$

Here, the solution is in terms of sines and cosines, with period given by $T = 2\pi/\sqrt{k} =  2\pi\cdot\sqrt{l/g}$. The answer does not depend on the mass, $m$, of the bob nor the amplitude of the motion, provided the small-angle approximation is valid.


If we pull the bob back an angle $a$ and release it then the initial conditions are $\theta(0) = a$ and $\theta'(0) = 0$. This gives the solution:


```{julia}
eqnp₁ = D2(u)(x) + g/l * u(x)
dsolve(eqnp₁, u(x), ics=Dict(u(0) => a, D(u)(0) => 0))
```

##### Example: hanging cables


A chain hangs between two supports a distance $L$ apart. What shape will it take if there are no forces outside of gravity acting on it? What about if the force is uniform along length of the chain, like a suspension bridge? How will the shape differ then?


Let $y(x)$ describe the chain at position $x$, with $0 \leq x \leq L$, say. We consider first the case of the chain with no force save gravity. Let $w(x)$ be the density of the chain at $x$, taken below to be a constant.


The chain is in equilibrium, so tension, $T(x)$, in the chain will be in the direction of the derivative. Let $V$ be the vertical component and $H$ the horizontal component. With only gravity acting on the chain, the value of $H$ will be a constant. The value of $V$ will vary with position.


At a point $x$, there is $s(x)$ amount of chain with weight $w \cdot s(x)$. The tension is in the direction of the tangent line, so:


$$
\tan(\theta) = y'(x) = \frac{w s(x)}{H}.
$$

In terms of an increment of chain, we have:


$$
\frac{w ds}{H} = d(y'(x)).
$$

That is, the ratio of the vertical and horizontal tensions in the increment are in balance with the differential of the derivative.


But $ds = \sqrt{dx^2 + dy^2} = \sqrt{dx^2 + y'(x)^2 dx^2} = \sqrt{1 + y'(x)^2}dx$, so we can simplify to:


$$
\frac{w}{H}\sqrt{1 + y'(x)^2}dx =y''(x)dx.
$$

This yields the second-order equation:


$$
y''(x) = \frac{w}{H} \sqrt{1 + y'(x)^2}.
$$

We enter this into `Julia`:


```{julia}
@syms w::positive H::positive y()
eqnc = D2(y)(x) ~ (w/H) * sqrt(1 + D(y(x))^2)
```

Unfortunately, `SymPy` needs a bit of help with this problem, by breaking the problem into steps.


For the first step we solve for the derivative.  Let $u = y'$, then we have $u'(x) = (w/H)\sqrt{1 + u(x)^2}$:


```{julia}
eqnc₁ = subs(eqnc, D(y)(x) => u(x))
```

and can solve via:


```{julia}
outc = dsolve(eqnc₁)
```

So $y'(x) = u(x) = \sinh(C_1 + w \cdot x/H)$. This can be solved by direct integration as there is no $y(x)$ term on the right hand side.


```{julia}
D(y)(x) ~ rhs(outc)
```

We see a simple linear transformation involving the  hyperbolic sine. To avoid, `SymPy` struggling with the above equation, and knowing the hyperbolic sine is the derivative of the hyperbolic cosine, we anticipate an answer and verify it:


```{julia}
yc = (H/w)*cosh(C1 + w*x/H)
diff(yc, x) == rhs(outc) # == not \Equal[tab]
```

The shape is a hyperbolic cosine, known as the catenary.


```{julia}
#| echo: false
imgfile = "figures/verrazano-narrows-bridge-anniversary-historic-photos-2.jpeg"
caption = """
The cables of an unloaded suspension bridge have a different shape than a loaded suspension bridge. As seen, the cables in this [figure](https://www.brownstoner.com/brooklyn-life/verrazano-narrows-bridge-anniversary-historic-photos/) would be modeled by a catenary.
"""
# ImageFile(:ODEs, imgfile, caption)
nothing
```

![The cables of an unloaded suspension bridge have a different shape than a loaded suspension bridge. As seen, the cables in this [figure](https://www.brownstoner.com/brooklyn-life/verrazano-narrows-bridge-anniversary-historic-photos/) would be modeled by a catenary.](./figures/verrazano-narrows-bridge-anniversary-historic-photos-2.jpeg)
---


If the chain has a uniform load – like a suspension bridge with a deck – sufficient to make the weight of the chain negligible, then how does the above change? Then the vertical tension comes from $Udx$ and not $w ds$, so the equation becomes instead:


$$
\frac{Udx}{H} = d(y'(x)).
$$

This $y''(x) = U/H$, a constant. So it's answer will be a parabola.


##### Example: projectile motion in a medium


The first example describes projectile motion without air resistance. If we use $(x(t), y(t))$ to describe position at time $t$, the functions satisfy:


$$
x''(t) = 0, \quad y''(t) = -g.
$$

That is, the $x$ position - where no forces act - has $0$ acceleration, and the $y$ position - where the force of gravity acts - has constant acceleration, $-g$, where $g=9.8m/s^2$ is the gravitational constant. These equations can be solved to give:


$$
x(t) = x_0 + v_0 \cos(\alpha) t, \quad y(t) = y_0 + v_0\sin(\alpha)t - \frac{1}{2}g \cdot t^2.
$$

Furthermore, we can solve for $t$ from $x(t)$, to get an equation describing $y(x)$. Here are all the steps:


```{julia}
#| hold: true
@syms x0::real y0::real v0::real alpha::real g::real
@syms t::positive x u()
Dₜ = Differential(t)
D2ₜ = Dₜ ∘ Dₜ
a1 = dsolve(D2ₜ(u)(t) ~ 0, u(t), ics=Dict(u(0) => x0, Dₜ(u)(0) => v0 * cos(alpha)))
a2 = dsolve(D2ₜ(u)(t) ~ -g, u(t), ics=Dict(u(0) => y0, Dₜ(u)(0) => v0 * sin(alpha)))
ts = solve(x - rhs(a1), t)[1]
y = simplify(rhs(a2)(t => ts))
sympy.Poly(y, x).coeffs()
```

Though `y` is messy, it can be seen that the answer is a quadratic polynomial in $x$ yielding the familiar parabolic motion for a trajectory. The output shows the coefficients.


In a resistive medium, there are drag forces at play. If this force is proportional to the velocity, say, with proportion $\gamma$, then the equations become:


\begin{align*}
x''(t) &= -\gamma x'(t), & \quad y''(t) &= -\gamma y'(t) -g, \\
x(0) &= x_0, &\quad y(0) &= y_0,\\
x'(0) &= v_0\cos(\alpha),&\quad y'(0) &= v_0 \sin(\alpha).
\end{align*}

We now attempt to solve these.


```{julia}
@syms alpha::real, γ::positive, v()
@syms x_0::real y_0::real v_0::real

eq₁ = Dₜ(Dₜ(u))(t) ~    - γ * Dₜ(u)(t)
eq₂ = Dₜ(Dₜ(v))(t) ~ -g - γ * Dₜ(v)(t)

a₁ = dsolve(eq₁, ics=Dict(u(0) => x_0, Dₜ(u)(0) => v_0 * cos(alpha)))
a₂ = dsolve(eq₂, ics=Dict(v(0) => y_0, Dₜ(v)(0) => v_0 * sin(alpha)))

ts = solve(x - rhs(a₁), t)[1]
yᵣ = rhs(a₂)(t => ts)
```

This gives $y$ as a function of $x$.


There are a lot of symbols. Lets simplify by using constants $x_0=y_0=0$:


```{julia}
yᵣ₁ = yᵣ(x_0 => 0, y_0 => 0)
```

What is the trajectory? We see that the `log` function part will have issues when $-\gamma x + v_0 \cos(\alpha) = 0$.


If we fix some parameters, we can plot.


```{julia}
v₀, γ₀, α = 200, 1/2, pi/4
soln = yᵣ₁(v_0=>v₀, γ=>γ₀, alpha=>α, g=>9.8)
plot(soln, 0, v₀ * cos(α) / γ₀ - 1/10, legend=false)
```

We can see that the resistance makes the path quite non-symmetric.


## Visualizing a first-order initial value problem


The solution, $y(x)$, is known through its derivative. A useful tool to visualize the solution to a first-order differential equation is the [slope field](http://tinyurl.com/jspzfok) (or direction field) plot, which at different values of $(x,y)$, plots a vector with slope given through $y'(x)$.The `vectorfieldplot` of the `CalculusWithJulia` package can be used to produce these.


For example, in a previous example we found a solution to  $y'(x) = x\cdot y(x)$, coded as


```{julia}
F(y, x) = y*x
```

Suppose $x_0=1$ and $y_0=1$. Then a direction field plot is drawn through:


```{julia}
#| hold: true
@syms x y
x0, y0 = 1, 1

plot(legend=false)
vectorfieldplot!((x,y) -> [1, F(y,x)], xlims=(x0, 2), ylims=(y0-5, y0+5))

f(x) =  y0*exp(-x0^2/2) * exp(x^2/2)
plot!(f,  linewidth=5)
```

In general, if the first-order equation is written as $y'(x) = F(y,x)$, then we plot a "function" that takes $(x,y)$ and returns an $x$ value of $1$ and a $y$ value of $F(y,x)$, so the slope is $F(y,x)$.


:::{.callout-note}
## Note
The order of variables in $F(y,x)$ is conventional with the equation $y'(x) = F(y(x),x)$.

:::

The plots are also useful for illustrating solutions for different initial conditions:


```{julia}
#| hold: true
p = plot(legend=false)
x0, y0 = 1, 1

vectorfieldplot!((x,y) -> [1,F(y,x)], xlims=(x0, 2), ylims=(y0-5, y0+5))
for y0 in -4:4
  f(x) =  y0*exp(-x0^2/2) * exp(x^2/2)
  plot!(f, x0, 2, linewidth=5)
end
p
```

Such solutions are called [integral curves](https://en.wikipedia.org/wiki/Integral_curve). These graphs illustrate the fact that the slope field is tangent to the graph of any integral curve.


## Questions


##### Question


Using `SymPy` to solve the differential equation


$$
u' = \frac{1-x}{u}
$$

gives


```{julia}
#| hold: true
@syms x u()
dsolve(D(u)(x) - (1-x)/u(x))
```

The two answers track positive and negative solutions. For the initial condition, $u(-1)=1$, we have the second one is appropriate: $u(x) = \sqrt{C_1 - x^2 + 2x}$. At $-1$ this gives: $1 = \sqrt{C_1-3}$, so $C_1 = 4$.


This value is good for what values of $x$?


```{julia}
#| hold: true
#| echo: false
choices = [
"``[-1, \\infty)``",
"``[-1, 4]``",
"``[-1, 0]``",
"``[1-\\sqrt{5}, 1 + \\sqrt{5}]``"]
answ = 4
radioq(choices, answ)
```

##### Question


Suppose $y(x)$ satisfies


$$
y'(x) = y(x)^2, \quad y(1) = 1.
$$

What is $y(3/2)$?


```{julia}
#| hold: true
#| echo: false
@syms x u()
out = dsolve(D(u)(x) - u(x)^2, u(x), ics=Dict(u(1) => 1))
val = N(rhs(out(3/2)))
numericq(val)
```

##### Question


Solve the initial value problem


$$
y' = 1 + x^2 + y(x)^2 + x^2 y(x)^2, \quad y(0) = 1.
$$

Use your answer to find $y(1)$.


```{julia}
#| hold: true
#| echo: false
eqn = D(u)(x) - (1 + x^2 + u(x)^2 + x^2 * u(x)^2)
out = dsolve(eqn, u(x), ics=Dict(u(0) => 1))
val = N(rhs(out)(1).evalf())
numericq(val)
```

##### Question


A population is modeled by $y(x)$. The rate of population growth is generally proportional to the population ($k y(x)$), but as the population gets large, the rate is curtailed $(1 - y(x)/M)$.


Solve the initial value problem


$$
y'(x) = k\cdot y(x) \cdot (1 - \frac{y(x)}{M}),
$$

when $k=1$, $M=100$, and $y(0) = 20$. Find the value of $y(5)$.


```{julia}
#| hold: true
#| echo: false
k, M = 1, 100
eqn = D(u)(x) - k * u(x) * (1 - u(x)/M)
out = dsolve(eqn, u(x), ics=Dict(u(0) => 20))
val = N(rhs(out)(5))
numericq(val)
```

##### Question


Solve the initial value problem


$$
y'(t) = \sin(t) - \frac{y(t)}{t}, \quad y(\pi) = 1
$$

Find the value of the solution at $t=2\pi$.


```{julia}
#| hold: true
#| echo: false
eqn = D(u)(x) - (sin(x) - u(x)/x)
out = dsolve(eqn, u(x), ics=Dict(u(PI) => 1))
val = N(rhs(out(2PI)))
numericq(val)
```

##### Question


Suppose $u(x)$ satisfies:


$$
\frac{du}{dx} = e^{-x} \cdot u(x), \quad u(0) = 1.
$$

Find $u(5)$ using `SymPy`.


```{julia}
#| hold: true
#| echo: false
eqn = D(u)(x) - exp(-x)*u(x)
out = dsolve(eqn, u(x), ics=Dict(u(0) => 1))
val = N(rhs(out)(5))
numericq(val)
```

##### Question


The differential equation with boundary values


$$
\frac{d(r^2 \frac{dc}{dr})}{dr} = 0, \quad c(1)=2, c(10)=1,
$$

can be solved with `SymPy`. What is the value of $c(5)$?


```{julia}
#| hold: true
#| echo: false
@syms x u()
eqn = diff(x^2*D(u)(x), x)
out = dsolve(eqn, u(x), ics=Dict(u(1)=>2, u(10) => 1)) |> rhs
out(5)  # 10/9
choices = ["``10/9``", "``3/2``", "``9/10``", "``8/9``"]
answ = 1
radioq(choices, answ)
```

##### Question


The example with projectile motion in a medium has a parameter $\gamma$ modeling the effect of air resistance. If `y` is the answer - as would be the case if the example were copy-and-pasted in - what can be said about `limit(y, gamma=>0)`?


```{julia}
#| hold: true
#| echo: false
choices = [
"The limit is a quadratic polynomial in `x`, mirroring the first part of that example.",
"The limit does not exist, but the limit to `oo` gives a quadratic polynomial in `x`, mirroring the first part of that example.",
"The limit does not exist -- there is a singularity -- as seen by setting `gamma=0`."
]
answ = 1
radioq(choices, answ)
```
