Julia Differential Equations
A differential equation is an equation that links a quantity to its rate of change. The Euler method steps forward in small fixed steps to build an approximate answer.
One Euler step shrinks the value
Start at 1.0 with a step of 0.1. The rate is the negative of the current value.
y = 1.0
h = 0.1
y = y + h * (-y)
println(y)Ten fixed steps land near one third
A small function repeats the same step ten times. Each step multiplies the value by 0.9.
function euler_steps(y, h, n)
result = y
for i in 1:n
result = result + h * (-result)
end
return result
end
println(round(euler_steps(1.0, 0.1, 10), digits=6))Smaller steps land closer to the true value
Twenty steps of 0.05 cover the same total time as ten steps of 0.1. The result sits closer to the exact value.
function euler_steps(y, h, n)
result = y
for i in 1:n
result = result + h * (-result)
end
return result
end
println(round(euler_steps(1.0, 0.1, 10), digits=6))
println(round(euler_steps(1.0, 0.05, 20), digits=6))
println(round(exp(-1.0), digits=6))Example
function euler_steps(y, h, n)
result = y
for i in 1:n
result = result + h * (-result)
end
return result
end
println(round(euler_steps(2.0, 0.1, 10), digits=6))
println(round(euler_steps(2.0, 0.05, 20), digits=6))
println(round(2.0 * exp(-1.0), digits=6))Key points
- Rate of change: the rate of change tells how fast a quantity grows or shrinks.
- Step size: the step size is the small fixed time jump used in each step.
- Euler method: the Euler method uses the current rate to take one small step forward.
- Fixed steps: a fixed number of steps gives a result that can be checked again.
- Smaller steps: smaller steps usually land closer to the true answer.
In this example, a function takes Euler steps for the equation dy/dt = -y. The first printed line uses ten steps of 0.1 from 2.0. The second printed line uses twenty steps of 0.05 from 2.0. The third printed line gives the exact value, and the smaller steps land closer to it.
Note
the result is an approximation, not an exact formula.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
With y at 1.0 and a step of 0.1, what is the next Euler value for dy/dt = -y?