Course 2.2Lesson 4 of 5
A differential equation gives the rate of change of a quantity, for example the acceleration of a falling ball as g minus k times the speed. Euler's method follows it in small steps: the new value is the old value plus the rate of change times a small step dt, and the new position uses the old speed. For a ball that falls freely, 3 seconds in steps of 0.1 give the exact speed but a position that is too small, 42.63 metres against 44.1. With air drag the speed grows towards the terminal speed g over k. Euler's method is first order: half the step makes the error half, and a big step makes the curve wrong. scipy.integrate.solve_ivp solves the same equation with steps that it chooses, for a function of the time t and the state y that returns the rates of change.
Try the code and the simulations beside the lesson.
After this lesson you can
solve_ivp of SciPy.In physics, most laws tell you how fast something changes, not how big it is. Newton's second law says that the acceleration is the force divided by the mass. The acceleration is the rate of change of the speed. So the law is an equation about how the speed changes:
Such an equation is a differential equation. To find the speed itself at a later time, we need to solve it. For simple forces, you do it with calculus. For many forces, there is no formula for the answer, and the computer solves it, in a way that follows the story of the motion step by step.
Imagine that you are riding a bicycle with your eyes closed. You know your speed now. In a short time , you will go about forward. And you know how your speed is changing, so in the same short time your speed will change by . After the step, you look again, and repeat.
The rule, for a position and a speed :
This is Euler's method. It is a loop that moves forward in time in small steps.
Predict first. A stone falls for 3 seconds. Will Euler's answer for the distance be exact, too big or too small?
The output:
Euler's method is not exact. How good is it?
A falling ball feels the force of the air, which grows with the speed. In a simple model, it gives the acceleration , so
with m/s and per second. This equation has the exact solution . At first, the ball speeds up as in free fall. As the speed grows, the drag grows, and the speed approaches the terminal speed, where , so .
Predict first. When the step is halved, by how much will the error change?
The picture shows the same thing. The orange line (a step of 0.1) follows the exact black curve closely. The blue line (a step of 1) leaves the curve: it overshoots, because each step uses the rate of change at the start of the step, and the rate is falling.
Both settle at the terminal speed 9.8 / k, where the drag balances the weight and the slope becomes 0.
For real work, use solve_ivp ("solve an initial value problem") from SciPy. It chooses the steps by itself, small where the solution changes fast, big where it is smooth, and uses a much better method than Euler's.
So Euler's method is for understanding, and solve_ivp is for work. Understanding Euler's method tells you what solve_ivp does behind the scenes: it follows the rates of change, step by step.
rate gets t first and y second.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
solve_ivp(rate, (t0, t1), start) solves it with steps that it chooses. rate(t, y) returns a list of rates.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
rate(t, y) return?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.