# Solving ordinary differential equations (ODE)

## Introduction

In Algebra we have what is called algebraic equation that is an mathematical expression described in terms of an unknown variable x.

For instance is an algebraic equation of first order(because the highest order of the x term is 1) while is an algebraic equation of fifth order (because the highest order of the x term is 5). The solution of an algebraic equation (if it really exists) belongs to the real set or to the complex set of numbers. Note that due to the fact that we could simply say that the solution (if exists) must be a complex number (which in some particular cases could be a real number, a rational number, an whole number or an natural number).

So an algebraic equation is expressed in terms of x-variable and to find its solution(s) means to find a number for that x-variable such that it verifies the equation (when it is used the left expression is equal with the right expression).

Very similar to an algebraic equation a differential equation(DE) is also an equation that is expressed in terms of y-function (instead of x-variable) and thus the solution of a differential equation is not a number (like in the case of the algebraic equation) but a function. Moreover, a differential equation is not only expressed in terms of the y-function but also (and this is important) in terms of its derivative, i.e. . That's why it is called differential in the first place, it must include the derivative. The higher the order of derivative the higher is the order of differential equation. For instance, y''+2y'-y=0 is a DE of the 2nd order (the higher order of derivative of y is 2). Note that such equation can be expressed in the same time in terms of x. For instance y''+2y'-y=3x+7 is also a DE.

Note that while an algebraic equation might have only a solution that verifies the equation a DE might have an infinity of solutions that verify the DE. Why is that? Let's imagine the function that is associated to a algebraic equation. The solution of that equation are those x-values where the function cross the x-axis. Now let's image a differential equation. A solution of that DE is a function (whatever that means). If we shift the function (the graph of the function) just a bit to the left or to the right, or even a bit higher or lower we still have the same function just that it was a bit shifted away (from the x,y-axes). Each shifted version of that function is a (particular) solution of the DE. So by drawing the graph of the function and by shifting around we get a multitude of solutions (an infinity) although the function graph will still look the same.

An ordinary differential equation (ODE) is an differential equation expressed only in terms of only one variable (like f(x), we express f in terms of one variable x). If it is expressed in terms of many variables (like f(x1,x2,...)) then it is called partial differential equation (PDE) and that's something else which I am not going to develop here.

When talking about equations (especially about ODE) sometimes we hear "homogeneous" and "non-homogeneous" equations. "Homos genos" comes from Greek and means "of the same kind". Thus being non-homogeneous means they belongs to a distinct kind, like functions (y) and variables (x).

Now that you know what a differential equation is it would be also helpful in showing different methods that occurs in solving ordinary differential equations (ODE).

## ODE of 1st order

So an ordinary differential equation where the highest derivative has the first order (i.e. y') is called ODE of 1st order.

The general form of a homogeneous ODE of 1st order is:

. This ODE is expressed only in terms of function y which is something of the same kind, thus homogeneous.

We could rewrite that equation like this: . The question is: what should be that function y such that when is derived we get a new function (y') that looks exactly like the first one (i.e. y) except that it is multiplied by a constant (eg. -a) ? Of course, it is . A good guess would be that our solution looks like this: .

To verify if our guess is valid would should find out its derivative and then we should use these in the original ODE Let's verify if the left term (LT) is equal with the right term (RT), i.e. LT=RT.

Because LT=RT we can conclude that our guess is valid, i.e. is a solution of the ODE. But this is just one solution because if we guess that our solution would be it will be also valid. So the general solution of such homogeneous ODE is:

where C is an arbitrary constant and -r is the coefficient of the y-term of the ODE.

The general form of a non-homogeneous ODE of 1st order is:

where f is any function of x (eg. f(x)=3x+1). Because this equation mixes both functions (y) and variables (x) such that we can distinguish between them it is said to be a non-homogeneous ODE.

If we assume that the solution for such an equation is a function it would be wrong because it cannot be verified. To solve such an equation means to find the solution of the homogeneous part of the ODE and also to find the solution of the f(x) part of the ODE. Thus the general solution of the ODE would be

Example:

Note that the same equation could be written as or . It is just the same equation, maybe written using a different notation. That's all.

Step 1: find the solution of the homogeneous part of the ODE: y'-2y=0.

We suppose that is the general solution of the homogeneous equation.Its derivative is . If we use these in the homogeneous part of ODE (i.e. ) we get:

<=> . The product is null if any of these terms are null, i.e. or . This second equation is also called "the characteristic equation of ODE". If we assume that then . So r-2 must be noll, i.e. r=2. Thus is the general solution for the homogeneous part of the ODE.

Step 2: find the solution of f(x) part of the ODE

As we see f(x) is the first order algebraic equation and thus it can be written like this: where a and b are two arbitrary constants that we are going to determine right now.

. If we use these in the f(x) part of the ODE (i.e. ) we get:

<=> . We equalize the non x-terms from both sides and likewise the x-terms from both sides and solve that system of equations:

=> a=-0.5 and b=-0.75. Also

Step 3: the general solution of the ODE is

where C is an arbitrary constant. For a particular value of C you'll get a particular solution of y.

## ODE of 2nd order

So an ordinary differential equation where the highest derivative has the second order (i.e. y'') is called ODE of 2st order.

Such an ODE if homogeneous looks like or if it's non-homogeneous then it looks like . The way we solve this equation is similar to that we solve the ODE of 1st order. Its characteristic equation will be which has the following solutions: where .

Depending on the value of the solution for our ODE can vary.

## 2 real solutions

When the characteristic equation has two distinct real solutions, namely and . Thus the homogeneous ODE has two distinct solutions:

and respective . Also the solution of our ODE is the sum of these two solutions, namely:

.

## 1 double solution

When the characteristic equation has one double real solution, namely . Thus the homogeneous ODE has the following solutions:

and respective . Also the solution of our ODE is the sum of these two solutions, namely:

where C=C1+C2 is just an arbitrary constant. But this is 2nd order ODE and thus it MUST have two solutions not just one. One solution is but what is the other solution?

Well, we assume that the second solution is similar to the first one multiplied by an unknown function v(x), i.e. .

If we use these two in the original ODE (i.e. ) we get:

<=>

This product is null only if v''=0. If we take integral of both sides we get that where C1 is an arbitrary constant. We take again integral of both sides and we get where C_2 is another arbitrary constant.

So our second solution is . We assume that there exist two arbitrary constant A, B such that A=C*C1 and B=C*C2, thus which is also the general case for .

So the general solution is

## 2 complex solutions

When the characteristic equation has two distinct complex solutions, namely and . Thus the homogeneous ODE has two distinct solutions:

and respective . Also the solution of our ODE is the sum of these two solutions, namely:

. But this solution is expressed in terms of complex numbers and this we don't like (it is not necessarily wrong but it would be cumbersome if we want to use this form to plot a graph). To reduce this form to something expressed only in terms of real number we could use the Euler's identity:

So if the characteristic equation has the complex solutions: the ODE's solutions would be:

We note C1=p+iq and C2 its conjugate i.e. C2=p-iq. Thus . If we note A=2p and B=-2q then our solutions becomes:

where A,B are two arbitrary constants.

## Numeric methods for ODEs

Sometimes we deal with such ODE that are difficult to solve if not impossible. In such situations we can use numerical methods to determine an approximative solution for ODE. There are many numerical methods to use but one I would prefer (due to its simplicity) is the Backward Euler method.

### Backward Euler method

This method assume that:

(i) the ODE is 1st ODE

and

(ii) we know ahead a point that lies on the graph of our solution.

What we do is to use this starting point and then find out the value of the slope/tangent to the graph at that point. Then we move an increment bit (a step) in one direction (either the positive direction of x-axis or the negative direction of x-axis, depending which part of the graph we want to depict) by using the slope we've already computed and thus we get a new point. We repeat this procedure as long as we want. When we finished the procedure we've got a visual graph of how the ODE's solution looks like.

Note: the smaller the increment (the step) the better the function approximation.

Example:

y'=1+3x-2y given that the (1,2) is on the graph. We assume a step of h=0.25

Step 1: we mark our starting point (1,2) and then find out the slope k=y'. When x=1 and y=2 the slope k=y'=1+3*1-2*2=1+3-4=0. So the graph slope at (1,2) is parallel to the x-axis. We draw a small segment with the length h and slope k.

Step 2: we compute the new point coordinates:

x=1+h=1+0.25=1.25 and y=kh+2=0*h+2=2

Step 3: the new point became (1.25, 2) and the slope at this point is k=y'. When x=1.25 and y=2 the slope k=y'=1+3*1.25-2*2=0.75. We draw a small segment with the length h and slope k.

Step 4: we compute the new point coordinates:

x=1.25+h=1.25+0.25=1.5 and y=kh+2=0.75*h+2=2.1875

Step 5: the new point became (1.5, 2.1875) and the slope at this point is k=y'. When x=1.5 and y=2.1875 the slope k=y'=1+3*1.5-2*2.1875=1.125. We draw a small segment with the length h and slope k.

Step 6: we compute the new point coordinates:

x=1.5+h=1.5+0.25=1.75 and y=kh+2=1.125*h+2.1875=2.46875

Step 7: the new point became (1.75, 2.46875) and the slope at this point is k=y'. When x=1.75 and y=2.46875 the slope k=y'=1+3*1.75-2*2.46875=1.3125. We draw a small segment with the length h and slope k.

Step 8: we compute the new point coordinates:

x=1.75+h=1.75+0.25=2 and y=kh+2=1.3125*h+2.46875=2.796875

The graph drawn by these points looks like this:

Using these points we could use a regression tool (like this) to determine the best fitting line. The greater is the determination coefficient (r) the better is the approximation.

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

The following two tabs change content below.

#### Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.