Euler's method

The **Euler's method** exercise appears under the Differential equations Math section of Khan Academy . This exercise shows how to use numerical methods to approximate a solution to a differential equation.

## Types of Problems

There are five types of problems in this exercise:

1. *Given all values of

f
″

{\displaystyle f''}

, estimate

f
(
a
)

{\displaystyle f(a)}

:* The student is asked to estimate the value of

f
(
a
)

{\displaystyle f(a)}

 using the table of derivatives, the step-size, and the point.
2. *Get from a to b in n equal steps:* The student is asked to estimate the value of

f
(
a
)

{\displaystyle f(a)}

 using the solution to the differential equation and initial condition.
3. *Solve for initial condition:* The student is asked to find the value of k using the differential equation, the solution to the differential equation, initial condition, and step-size.
4. *Fill in the blanks in the Euler table below:* The student is asked to fill in the blanks of the Euler table using the initial value problem, initial condition, and step-size.

## Strategies

Knowledge of derivatives, implicit differentiation, differential equations, and Euler's method are encouraged to ensure success on this exercise.

1. Euler's method:

y

n
1

=

y

n

Δ

y

n

{\displaystyle y\_{n+1} = y\_{n} + \Delta y\_{n}}
2. Euler's method is used to approximate values of differential equations that can't be solved by normal methods.
3. The smaller the step size, the better approximation the graph will have.
4. User needs the initial point, delta x, and the differential equation to be able to use Euler's method.

## Program

```
/**************************************************************
 * Approximating visual solutions to first-order differential *
 * Equations with Euler's Method                              *
 * ***********************************************************/

//You'll get a better approximation the smaller the xStep (although it will require more computation)
var xStep = 0.1;

//Initial conditions
var initX = 0;
var initY = 8;

//This is where you should define the first-order differential equation
//Assumes it can be expressed as y' = f(x, y(x))
var slopeAtPoint = function(x, y) {
    return -x/y ;
};

//Variables for graph paper
var xMin = 0;
var yMin = 0;

var xMax = 10;
var yMax = 10;

var xOffset = 21;
var yOffset = 22;

background(58, 66, 74);

var xScale = 400/xMax;
var yScale = 400/yMax;

//Maps an x coordinate to the appropriate pixeldistance on canvas
var xCoordinate = function(x) {
    return xOffset + x*xScale;
};

//Maps a y coordinate to the appropriate pixel distance on canvas
var yCoordinate = function (y) {
    return  400-(y*yScale+ yOffset);
};

stroke(250, 250, 250);
//Draw the verticals lines in grid and labels x-axis with numbers
for(var i=xMin; i<=xMax; i++) {
    line(xCoordinate(i), yCoordinate(yMax), xCoordinate(i), yCoordinate(yMin));
    text(i, xCoordinate(i), yCoordinate(yMin)+15);
}

//Draw the horizontal lines in grid and labels y-axis with numbers
for(var i=yMin; i<=yMax; i++) {
    line(xCoordinate(xMin), yCoordinate(i), xCoordinate(xMax), yCoordinate(i));
    text(i, xOffset/2, 400-(i*yScale+ yOffset));
}

//This actually draws the lines starting with initX and incrementing by xStep until you get
//to xMax
var drawApproximation = function(deltaX) {
    var x = initX;
    var y = initY;
    while (x<=xMax) {
        point(xCoordinate(x), yCoordinate(y));
        var slope = slopeAtPoint(x,y);
        var xNext = x +deltaX;
        var yNext = y + slope*deltaX;
        line(xCoordinate(x), yCoordinate(y), xCoordinate(xNext), yCoordinate(yNext));
        x = xNext;
        y = yNext;
    }
};

//Change color and make the function calls to actually draw approximations
stroke(252, 136, 136);
strokeWeight(2);
drawApproximation(xStep);

stroke(106, 245, 106);
drawApproximation(xStep*2);

```

Created by Salman Khan

Walk-through: https://www.khanacademy.org/math/differential-equations/first-order-differential-equations/eulers-method-tutorial/v/eulers-method-program-code

Located: https://www.khanacademy.org/math/differential-equations/first-order-differential-equations/eulers-method-tutorial/p/eulers-method-program-code

**☀ All Khan Academy content is available for free at: www.khanacademy.org**