# Euler, ODE1 | Solving ODEs in MATLAB

From the series: Solving ODEs in MATLAB

ODE1 implements Euler's method. It provides an introduction to numerical methods for ODEs and to the MATLAB suite of ODE solvers. Exponential growth and compound interest are used as examples.

Related MATLAB code files can be downloaded from MATLAB Central

**Published:
21 Jan 2016**

Hello. I'm Cleve Moler, one of the founders and chief mathematician at The MathWorks. This series of videos is about solving ordinary differential equations in MATLAB. We can begin by recalling the definition of derivative. The derivative of a function at a point is the slope of the tangent line to the graph of the function at that point.

Our numerical approximations will rely upon the slope of the secant to the graph. That's a line through two points, separated by a distance h. We'll have a lot to say about the step size h as we go along. What's important to realize is that as h goes to 0, the slope of the secant approaches the slope of the tangent.

The wiggly equal sign means approximately equal to. t0 is the point where we're finding the approximation. The value of the derivative at t0 is approximately equal to the slope of the secant. The slope of the secant is the change in the y value over the change in the t-value. The change in the y value is the difference between the two values of y. The change in the t-value is the step size h.

If we rewrite this, we get the value of y at the point t0 plus h is approximately equal to the value of y at t0 plus h times the value of y prime at t0. This is the basis for our first numerical method, Euler's method.

Leonhard Euler was a 18th century Swiss mathematician, probably the most influential mathematician of his era. He made important contributions to a wide range of fields in mathematics, physics, and astronomy. He invented the notion of function, for example.

The differential equation is given by this function f of two variables, t and y. And the task, in general, is to find a function y whose derivative is equal to f. Now, there's lots of functions y whose derivative is equal to f. And so where there's a initial condition, a point t naught, or t0, and a value y0, and the initial condition is that y at t0 should be equal to y0.

Here's some examples. The compound interest problem is just the interest rate times y. Here the function of t and y doesn't actually depend upon t, and it's linear in y. The initial condition is at time 0. There's a specified value of y, like $100. That's the compound interest problem.

Here's the logistic equation. Nonlinear equation, here f of t and y again doesn't depend upon t. And it's a constant times y minus another constant times y squared. That's the logistic equation. And again, the value is specified at 0. Let's say that y at 0 is equal to 1.

Here's another nonlinear equation. f of t and y is t squared plus y squared. It's not possible to find an analytic solution to this equation. We'll use these numerical methods to find some solution to this equation. Initial condition, y at 0 is equal to 0. That's an example of a function of t and y.

Euler's method actually isn't a practical numerical method, in general. We're just using it to get us started thinking about the ideas underlying numerical methods. Euler's method involves a sequence of points t sub n, separated by a fixed step size h. And then y sub n is the approximation to the value of the solution at t sub n.

The approximation comes from the slope of the secant, the ratio of the difference of the values of y and to the step size h. The differential equation says that this ratio should be the value of the function at t sub n. And if we rearrange this equation, we get Euler's method, that yn plus 1 is yn plus h times the function f evaluated at d sub n and y sub n. This is Euler's method.

We're now ready for our first MATLAB program, ODE1. It's called ODE1 because it's our first program and because it evaluates the function f that defines the differential equation once per step. There are five input arguments. The first is f, a function that defines the differential equation. This is something called an anonymous function. I'll talk more about that in a moment.

The other four are scalar numerical values. The first three define the interval of integration. We're going from t0 in steps of h to tfinal. The fifth input argument is y0, the initial value. The output is a vector. Vector yout is the values of the solution at the points in the interval.

We start by putting y0, the initial value, into y and then putting y into the output vector. The body of the function is a for loop. t goes from t0 in steps of h up to one step short of tfinal, and then the final passage through the body of the code takes t up to tfinal.

We evaluate the function f at t and y. That gives us a slope s. s is for slope. Here's the Euler's step. Take the current value of y. Add h times the slope. That gives us a new value of y. And then y is appended to yout. This MATLAB construction with the square brackets takes a vector y, adds another value to it, making it one element longer, and puts the result back in yout. This is the entire code. This is it. This is ODE1 that implements Euler's method.

The first argument to any of the MATLAB ODE solvers is the name of a function that specifies the differential equation. This is known as a function handle. The easiest way to get a function handle is to make use of an anonymous function created with the ampersand or at sign. All of the differential equations involve anonymous functions of two variables, t and y. And so we have f equals at parentheses t comma y closed parentheses. This is followed by any expression involving either t or y. And many of them don't depend upon t.

So here's an anonymous function defining our interest problem. And we can just evaluate this like any ordinary function. When y is equal to 1, f of 1 is 0.06. Here's an example of a function that depends upon both t and y. The functions can involve constants that have values. So here we can define two constants. And then we can use those two constants to define the logistic equation, f of a times y minus b times y squared. Again, this is an autonomous equation that doesn't actually depend upon t.

Let's see how Euler's method and ODE1 work on this simple example, y prime equals 2y with the initial condition y of 0 equals 10 on the interval t between 0 and 3. We define the anonymous function, f of t and y is equal to 2y. The initial condition is t0 equals 0. We're going to take a step size of 1. Go to tfinal equals 3, starting at y0 equals 10. And here's our call to ODE1.

We have an animation that show these steps. Start at t0 equals 0 and y0 equals 10. Here's our first point. We evaluate the function there. We get a slope of 20. That's 2 times 10. We take an Euler step of length 1 across the first step. That brings us to the second, 0.30. Evaluate the function there. 2 times 30 is 60. That's our slope.

Take the second step to get to y2. y2 is 90. Evaluate the function there. Get 2 times 90 is 180. That gives us the slope. Take a step across the interval with that slope and get us to a third point. The third point is 270. And that's the end of the integration. So that's three Euler steps to get from t0 to tfinal.

Euler's method is actually the same as computing compound interest. So let's do a compound interest problem. Define the interest rate. Define our anonymous function using that interest rate. Start at time 0. Go in steps of a month. Go for 10 years. Start with $100. And here's our result using ODE1 to compute compound interest. That's 121 numbers.

MATLAB actually has a format for looking at dollars and cents. And so here they are as dollars and cents. Starting with $100 and compounding every month, we get up to just over $180. I'm going to plot that. So I want a time vector of months.

And I actually want to compare it with simple interest. So here's how you compute simple interest, $0.50 a month. And then now let's plot those two. So the straight line is simple interest, getting up to $160. And the blue line is the compound interest. There's a slight upward curvature, getting us up to $180. There's a dot every month here, as we show the results of Euler's method, which, as I said, is the same thing as computing compound interest.

Finally, here's an exercise. Find the differential equation that produces linear growth. And rerun this example using ODE1 twice, once to compute the compound interest and once to compute the simple interest.

#### Download Code and Files

#### Related Products

#### Learn More

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)