Showing posts with label differential equation. Show all posts
Showing posts with label differential equation. Show all posts

Thursday, April 10, 2014

The Euler's Method

Given a differential equation of the form  eq1, a curious mind (the kind of mind that has nothing better to do in life) may wonder how one can go about solving such a DE to produce a variety of colorful numerical results. A simple closed-form analytical solution would obviously be ideal to make things simple. However, let us assume for the moment that an analytical solution either does not exist, or cannot be determined. In most real world cases, this is a perfectly valid assumption. So, how does one go about numerically solving the above differential equation?


Perhaps the simplest of numerical integration techniques is the Euler’s method. The premise of the Euler method is based on the definition of the slope of a curve at a given point from basic calculus.

eq2


 

Rearranging the above eqn., one can obtain


 eq3

The newly rearranged equation above represents a method for marching from stage ‘x’ to ‘x+h’, given a value for y’(x) at x=0. In other words, given f(0), one can march along from y(0) with incrementing values of h to obtain a numerical solution for the 1st order ODE. Thus,


Step 0: f(0) = initial value
Step 1: f(step=1) = f(step=0) + hf’(step=0)
Step 2: f(step=2) = f(step=1) + hf’(step=1)
...

The recursive statements are usually represented in the following format:

 eq4

The algorithm for Euler's method of numerical integration can be summarized as follows:


1) % Define step size (h), initial function y(0), initial time t0, final time tf
2) nosteps = (tf – t0)/h
3) for i ← 1 to nosteps

- Calculate derivative function f[t0,y[i]]
- y[i] ← y[i-1] + h*f[x,y]
- t0 ← t0 + h


4) % Display results

To illustrate the procedure, the Euler’s method was applied to the following initial value problem (Butcher, Numerical methods for ordinary differential equations, 201(a)) with a step size of 0.001.

 eq5

 The result of the integrated function is plotted in fig. (1).

 New Picture

 

Tuesday, April 8, 2014

Visualizing differential equations

The solution of a differential equation (DE) can be obtained either analytically or numerically. Although analytical closed-form solutions are straight-forward and easy to comprehend, there exist a large variety of DE’s that cannot be solved analytically. One has to thus resort to numerical techniques to solve these problems.

Direction Fields serve as a valuable tool to visualize differential equations. In the simplest form, the derivative of a function at a specific location, say (x,y), is the slope of the function at that point. Consider, for example, the simple DE

 y’ = y (t), with boundary condition of y(0) = 1


 The above equation can be easily solved using well known techniques to obtain an analytical solution. Instead, let us attempt to plot the slope (i.e., y’) at various instances of time t, for various values of y. One such plot is shown below in Fig. (1). The direction fields provide a visual clue to the form of the solution for the initial value problem. Also plotted in Fig. 1 is the analytical solution (solid brown line).

 

Image

Fig 1: Direction fields for y’ = y, with analytical solution y(t) = exp(t)

A variety of curves (i.e., solutions to the DE) can be visualized from Fig. 1. Each of these curves corresponds to their equivalent initial value problems. The analytical solution shown in Fig. 1 is one case of the initial value problem, where y(0)=1. Using the direction fields, one can visualize the form of the solution for various initial values. For a negative intercept on the ordinate (i.e., initial value problems with y(0) < 0), the form of the solution would be a mirror image of the solid brown line about the abscissa. This is visually evident from the direction fields shown for times t < 0 in Fig. 1.

Fig. 2 shows another example of a direction field plotted for the following 1st order DE (Atkinson et al, Numerical Solution of Ordinary Differential Equations, problem 1(a))


y’ = -y(t) + sin(t) + cos(t), y(0) = 1


 Image

Fig. 2: Direction fields for y’ = -y + sin(t) + cos(t), with analytical solution