# Choosing a stepsize for Euler’s method – Mathematical Modelling – Mathematics – TU Delft

Hello there! In this video you will learn how to choose

an appropriate stepsize when you implement Euler’s Method. Here we have a general differential equation:

the derivative of y is equal to a function f of t and y. We will approximate the solution with a numerical

method, with Euler’s method. Time is discretised with a stepsize of Delta

t, so we will not calculate the values of y for all times, as could be done with an analytical method. We estimate y only at multiples of Delta t. We start with the initial condition and use

the formula for n equal to zero to approximate the value of y at time Delta t. Then we can use the same formula for n equal to one to approximate the value of y at time two times Delta t. And so on, until we have covered the time span

that we are interested in. We want to estimate the number of rainbowfish

in our aquarium after a few months. We start with 30 fish and the mathematical model for the population size is this differential equation. The growth term is bounded, and we have seen that the equilibrium value of approximately 720 fish is stable. When you compare the differential equation

for the rainbowfish with the general one, you see that our function f is now a function

of t and P, and that f equals the right hand side of the differential equation. To apply Euler’s method, we also need a stepsize

Delta t. Now we are going to experiment to see what

is an appropriate stepsize. First, we try a stepsize of a week: Delta

t equals 7 days. We start at time 0 with 30 rainbowfish. With the differential equation, we calculate the derivative: dPdt equals 0.7 times P times etcetera, etcetera.. So when you substitute P equal to

30, dPdt becomes 0.16. This means that the line tangent to the solution

curve of P has a slope of 0.16. To be precise, the slope also has a unit:

it is 0.16 per day. Now we estimate the value of P after a week

as the old P plus the stepsize times the derivative, so P(7) is approximated by 30 plus 7 times 0.16 which

equals 31.1. And then we calculate the derivative at t equal to 7, by substituting P equals 31.1 in the formula for the derivative: now the

derivative is 0.88 per day. The tangent line is still almost flat. Now we can estimate the value of P at t equal

to 14. After two weeks P is estimated as 37.3 fish. Another derivative: the slope is increasing

a little. P after three weeks becomes 70.9. The slope is rapidly increasing. It is now 24.9 per day. And then P rises to 245. The slope becomes very steep! P becomes 914 fish and.. ..oops, the slope is negative all of a sudden! The reason for that is, that a P of 914 fish is way over the equilibrium value of approximately 720 fish. The equilibrium is stable, so when P is larger,

the derivative is negative. P should return to its equilibrium value. However, the next step shoots again through

the equilibrium value, and the whole population size becomes negative. This is nonsense of course. So with this stepsize of seven days, the numerical solution is clearly not valid after the fourth step. We will have to try a smaller stepsize. So the results for a stepsize of seven were not ok. Let us now try a Dt of four days. We start again for t equal to zero with 30 rainbowfish. Now, with the smaller stepsize, the value

of P starts to increase a little earlier but… Oh oh, it shoots through the equilibrium value

again becomes reasonable again, but the step of four days is too large! And the numerical solution is still erratic. Let’s again half the stepsize: Now take Dt two days. Five steps at once. Yes! Only a little bit through the equilibrium

value! The solution is still rather angular around

20 days, so now a stepsize of just one day. After 10 iterations, 20 days, and after that

the population gets into its equilibrium state. So now you have seen that for our problem, stepsizes that are larger than 1 day are clearly too large. The question is now, is the stepsize of one day small enough, or should we refine further? Luckily this rainbowfish calculation is so

tiny, that even for a very small stepsize, your computer will do the calculation in a wink and the results can be stored in a few kilobytes. Only when you use a very, very, very small

stepsize, you will run into problems because of the machine precision: your computer only

calculates with a finite number of digits. So you could use a stepsize much smaller than

a day. The question remains, why should you? What is an appropriate stepsize for our rainbowfish

problem? We have approximated the solutions of the differential equation for the rainbowfish population with Euler’s method. Stepsizes that are larger than a day give

obviously wrong results. How much smaller than a day is appropriate? You have to think of how accurate do you want

the results to be. What was the problem we are trying to solve? Is it useful to calculate up to 0.001 fish? And you also have to consider the accuracy

of the model. How precise is the differential equation that

we derived? In the assignments that follow this video, you will simulate the results, and explore these question yourself!