Here we are again with the second part of the series dedicated to Neural ODEs. If the first article was a mental beating this one will be a bit less so. But don’t get your hopes up: it’s still no walk in the park. We’ve already sunk our hands into the mire of derivative, integral, and differential equation. And now, since we’ve already ruined our manicure, we might as well go all the way and answer one of the unresolved questions from the first article. So without further ado (or bloodhound), let’s begin and see how to solve an ODE.

ODE To The Method

I know, I can feel it, you couldn’t wait to find out how to solve an ODE and finally, here we are. How do you solve an ODE? The short answer is: it depends. As always in life, there are two options: one elegant and one brutal.

On one side we have the analytical methods. They’re classy and well-dressed, like an intern at a Big 4 firm. They’re the methods that give you an exact formula for the solution. Now a question arises. If they’re so good and perfect, why do we need alternatives? Because, just like an intern at a Big 4, when the going gets tough these methods aren’t enough and you need to ask for help from others.

And here come the numerical methods. The bold ones in the room. Let’s just say it, they’re like junior trainees at a Big 4 (Big 4 friends, of course this is just a joke, peace and love.). They don’t care about elegance or exact formulas, head down and they get the job done. They move forward step by step, estimating the solution point by point. They’re not exact, that’s true. But sometimes, they’re the only viable way.

In short, choosing the right method depends on two things:

  • On you: maybe you don’t feel like sweating over an analytical solution or simply don’t need it.
  • On the problem: because some ODEs simply don’t have an analytical solution. And when that happens, what you want is worth about as much as a two of clubs when spades are trump.

Analytical Methods

Let’s start with the pretty boys of the bunch. Analytical methods are able, at least when everything goes smoothly, to extract the closed-form expression of the function that solves the equation. These methods are only applicable under specific, often ideal, conditions that you’ll typically find only in university textbooks. There’s no single method available instead, depending on the structure of the ODE, we have several at our disposal:

  • Separation of Variables method
  • Integrating Factor method
  • Similarity method
  • Laplace Transform method
  • ...and many more, but trust me, the list is long

Will we look at all of them? Absolutely not. We’ll go over just the first two to show I’m not making this stuff up. But if you want a complete and detailed overview, I recommend reading this.

Separation of Variables

The separation of variables method is one of those techniques that make you go “okay, that’s cool, but when would I ever use it?”. In fact, it’s applicable only when an ODE can be written like this:

$$ \frac{dy}{dt} = f(y) \cdot g(t) $$

Having this structure allows us to do the following:

$$ \frac{dy}{f(y)} = g(t) \cdot dt $$

That is, we multiply both sides of the equation by \(dt\) and divide both by \(f(y)\). Basically, we’ve separated the equation, putting all \(y\)-related terms on one side and all \(t\)-related terms on the other. To get a better feel for this, try playing with the following ODE:

$$ \frac{dy}{dt} = 2yt + t $$

Swear, curse, and bang your head against the wall, you’ll never be able to separate \(y\) from \(t\). In at least one side of the equation, there will always be an intruder, whether it’s related to \(y\) or to \(t\).

At this point, all that’s left is to integrate both sides of the equation, solving the ODE:

$$ \int \frac{dy}{f(y)} = \int g(t) \cdot dt $$
Example

Great, we can finally look at an example. In this paragraph I casually dropped an ODE along with its solution, without telling you where it came from. Let’s pick it up again:

$$ \frac{dy}{dt} = 3y(t) $$

This fits very well with separation of variables, it’s already written in the form we need:

$$ \frac{dy}{dt} = f(y) \cdot g(t) = y(t) \cdot 3 $$

where:

  • \(f(y) = y(t)\)
  • \(g(t) = 3\)

So we do our little magic trick to separate the \(y\) terms from the \(t\) terms:

$$ \frac{dy}{y(t)} = 3 \cdot dt $$

Now that we’re wizards with derivatives, when we see \(d-something\), we know it’s an infinitesimal, and we need to integrate to find the antiderivative. So we integrate both sides, indefinitely:

$$ \int \frac{1}{y(t)} \cdot dy = \int 3 \cdot dt $$

Using the table of notable integrals, we can easily solve these:

$$ \ln \left | y(t) \right | + C_1 = 3t + C_2 $$

from which:

$$ \ln \left | y(t) \right | = 3t + (C_2 - C_1) $$

Since \(C_2\) and \(C_1\) are arbitrary constants, which can take any value (reflect on this point here) we can, with a light heart, set:

$$ (C_2 - C_1) = C $$

So the solution becomes:

$$ \ln \left | y(t) \right | = 3t + C $$

To isolate \(y\), we need to invert the natural logarithm, so we raise both sides as powers of \(e\) and get:

$$ y(t) = e^{3t + C} = e^{3t} \cdot e^C $$

Following the same principle as before, \(e^C\) is an arbitrary constant, so we can simply rename it:

$$ e^C = K $$

and therefore

$$ y(t) = K \cdot e^{3t} $$

This proves that I wasn’t talking nonsense in part 1. Everything checks out, and the world can resume spinning. Now we’re left with the bitter taste of “okay, but what’s the value of K?” The problem doesn’t provide enough information, so as posed we can’t know it. To determine \(K\) we’d need a Cauchy problem. What’s left...? Ah yes, to show the graphical behavior. Here it is

Plot Sostituzione Figure 1. Plot of \(y=K \cdot e^{3t}\)

Let’s comment on this graph. As you know, or at least should from part one, it’s a direction field graph. Even though we know the general form of the solution, without knowing the value of \(K\) we can’t trace the exact curve. However, the direction field helps us: it shows how solutions behave for different values of \(K\), even without knowing them explicitly. What we notice is that there are three families of solutions:

  • The increasing family: arrows for \(y>0\) diverge upwards.
  • The decreasing family: arrows for \(y<0\) diverge downwards.
  • The constant family: consisting of a single lonely member, always zero, corresponding to \(y=0\).

Integrating Factor

Remember that little ODElette (who’s not the wife of ODEman... ok, I disgust myself), which in the previous paragraph couldn’t be solved? I mean this one:

$$ y' = 2yt + t $$

Quick note: \(y\) is always a function of time, so you might read \(y\) instead of \(y(t)\), but they’re entirely equivalent.

Well, with the integrating factor method we can go where separation of variables couldn’t. This method requires the ODE to be first-order linear (read more here), and it must be written in the form:

$$y' + p(t) \cdot y = q(t)$$

Formula 1. Base Form for Integrating Factor

At this point, the method requires us to define the integrating factor as:

$$ \mu (t) = e^{\int p(t) dt} $$

and we multiply every term of the ODE by \(\mu (t)\):

$$y' \cdot e^{\int p(t) dt} + y \cdot p(t) \cdot e^{\int p(t) dt} = e^{\int p(t) dt} \cdot q(t)$$

Formula 2. Integrating Factor

Now we use a math trick. Let’s take the product rule for derivatives:

$$\frac{d}{dt} \left( f(t) \cdot g(t) \right) = f'(t) \cdot g(t) + f(t) \cdot g'(t)$$

Formula 3: Product Rule

Since we’re dealing with a first-order ODE, we have:

$$ f'(t) = y' $$
$$ f(t) = y $$
$$ g(t) = \mu (t) = e^{\int p(t) dt} $$
$$ g'(t) = \mu ' (t) = e^{\int p(t) dt} \left[ \frac{d}{dt} \left( \int p(t) dt \right) \cdot \ln(e) \right] $$

so that:

$$ \frac{d}{dt} \left( \int p(t) dt \right) = p(t) $$

since we’re differentiating an integral, and

$$ \ln(e) = 1 $$

because \(e\) is the base of the natural logarithm. Therefore:

$$ \mu'(t) = p(t) \cdot e^{\int p(t) dt} $$

Plugging this into Formula 3, we get:

$$ \frac{d}{dt} \left( f(t) \cdot g(t) \right) = \frac{d}{dt} \left( y \cdot \mu(t) \right) $$
$$ \frac{d}{dt} \left( y \cdot \mu(t) \right) = y' \cdot \mu (t) + y \cdot \mu'(t) $$
$$ y' \cdot \mu (t) + y \cdot \mu'(t) = y' \cdot e^{\int p(t) dt} + y \cdot p(t) \cdot e^{\int p(t) dt} $$
$$ \Rightarrow \frac{d}{dt} \left( y \cdot \mu(t) \right) = y' \cdot e^{\int p(t) dt} + y \cdot p(t) \cdot e^{\int p(t) dt} $$

Since last time I checked, Formula 2 still holds, we can write:

$$ \frac{d}{dt} \left( y \cdot e^{\int p(t) dt} \right) = e^{\int p(t) dt} \cdot q(t) $$

Integrating both sides gives:

$$ y \cdot e^{\int p(t) dt} = \int q(t) \cdot e^{\int p(t) dt} dt + C $$

Finally, ladies and gentlemen, dividing by \(\mu(t)\) we get the final formulation of \(y\):

$$y = e^{-\int p(t) dt} \cdot \int q(t) \cdot e^{\int p(t) dt} dt + e^{-\int p(t) dt} \cdot C$$

Formula 4. Integrating Factor Solution

As we’ve seen, with not a few hurdles let’s be honest, when we can apply the integrating factor, we have a nice little “formula” ready to explicitly solve the elusive \(y\). I’d say it’s time to look at an example.

Example

The ODE we wanted to solve was this:

$$ y' = 2yt + t $$

Right!? Great, even better! This one lends itself to the integrating factor method. So let’s revisit the ODE, but this time we solve a Cauchy problem, and assume we not only have the ODE, but also the value of the solution \(y(t)\) at \(t=0\), which we’ll assume to be \(0\) (but it could also have been \(y(t)=5\) at \(t=214\), we just need to know at least one value of the function at a specific time):

$$ \begin{cases} y' = 2yt + t \\ y(0) = 0 \end{cases} $$

Let’s start from the ODE:

$$ y' - 2yt = t $$

So:

  • \(p(t) = -2t\)
  • \(q(t) = t\)

From the integrating factor solution formula, we need to compute:

$$ \int p(t) = \int -2t = -t^2 + C_1 $$

Which gives:

$$ y = e^{t^2 - C_1} \cdot \int t \cdot e^{-t^2 + C_1} dt + e^{t^2 - C_1} \cdot C $$
$$ y = e^{t^2} \cdot \int t \cdot e^{-t^2} dt + e^{t^2} e^{-C_1}\cdot C $$

Now we need to solve:

$$ \int t \cdot e^{-t^2} $$

I can feel it, the terror gripping the hearts of anyone who took Calculus 1, because solving this integral requires the substitution method, which I won’t explain here but I’ll leave you with a source to dig deeper. So let’s set:

$$ u = -t^2 \Rightarrow du = -2t dt \Rightarrow tdt = -\frac{du}{2} $$
$$ \Rightarrow \int t \cdot e^{-t^2} dt = -\frac{1}{2} \int e^u du = -\frac{e^u}{2} + K $$

Back to original variables:

$$ -t^2 = u \Rightarrow -\frac{e^u}{2} + K = -\frac{e^{-t^2}}{2} + K $$

and so:

$$ \int t \cdot e^{-t^2} dt = -\frac{e^{-t^2}}{2} + K $$

Returning to our \(y\):

$$ y = e^{t^2} \cdot \left[ -\frac{e^{-t^2}}{2} + K \right] + e^{t^2} e^{-C_1}\cdot C $$
$$ y = -\frac{1}{2} + K_1 \cdot e^{t^2} + K_2 \cdot e^{t^2} $$

Where, being arbitrary constants, we set:

$$ K_1 = K \text{ and } K_2 = Ce^{-C_1} $$

Thus:

$$ y = -\frac{1}{2} + e^{t^2} \left( K_1 + K_2 \right) = -\frac{1}{2} + K \cdot e^{t^2} $$

Remember we’re solving a Cauchy problem where at \(t=0\), \(y=0\), so:

$$ -\frac{1}{2} + K \cdot e^{(0)^2} = K -\frac{1}{2} = 0 $$
$$ \Rightarrow K = \frac{1}{2} \Rightarrow y = \frac{e^{t^2} - 1}{2} $$

Thanks to the initial condition, we’ve found the value of \(K\), which lets us compute \(y\) at any point in time. If you were paying attention, you know the value of \(y\) at \(t=0\). So let’s now compute it for \(t=1\):

$$ y = \frac{e - 1}{2} \approx 0.859 $$

Exactly as I hinted, having an initial condition allows us to determine the value of the ODE at any moment in time \(t\). Of course, the initial condition can alter the ODE’s behavior. Let’s take a look at the direction field and phase diagram.

Plot Fattore Integrante Figure 2. Plot of \(y=-\frac{1}{2} + K \cdot e^{t^2}\)

Remember when I said in part one that phase diagrams are used for systems of ODEs? Well, that’s not entirely true, because even a single ODE, if it comes with an initial condition, produces a well-defined curve in the \((y,t)\) plane. That’s exactly what I’m doing here. But why is it possible in this case? Simply because now we have a Cauchy problem, not just a plain ODE. So with an initial condition, I can compute \(K\) and see the behavior of \(y\) over time with \(K\) known. Thus, the direction field shows us all possible solutions: the initial value determines which of these, our

$$y(t)$$

will follow. In our case, the initial condition is \(y(0) = 0\) which implies \(K=\frac{1}{2}\). In the plot, this specific case is shown by the green line (and no, don’t switch to RAI 1). Here too, just like before, we have three distinct families:

  • \(y > -\frac{1}{2}\): Field lines decrease rapidly (see the steepness of the arrows) and then start to increase again.
  • \(y < -\frac{1}{2}\): Field lines increase rapidly and then start to decrease again.
  • \(y = -\frac{1}{2}\): The function remains constant and zero-valued

If you want to verify for yourself, try setting the initial condition \(y(0) = -\frac{1}{2}\) and go through the math. You’ll find that it implies \(K=0\).

Conclusions

I swear I started with the best intentions, but I got a bit carried away. So here we are, at the end of a second part that was mathematically pretty intense. As for the third part, well I can’t promise it’ll be any easier, but you’ve started dancing, so now KEEP DANCING! As always, here’s the link to the repo.
Oh, and in case it wasn’t obvious, the next article in our beloved series will be about numerical methods.

Until next time.


Related Posts


Published

Category

Teoria & Matematica

Tags

Contacts