Hey everyone,
I'm learning this language, and doing a couple of simple examples to see if
I understand everything.
I want to solve the first order differential equation u' = f(u, t), with
initial condition u(0) = ui. Discretizing u' as (u(t) - u(t-dt)) / dt, and
rearranging, means I have to solve the equation:
u(t) - u(t-dt) - dt * f(u, t). To solve the equation, I'm calling the
function fzero.
Can anyone please comment on this implementation? Maybe post some
pathological case where it wouldn't work?
using Roots
function beuler(f, dt, ti, tf, ui)
N=int((tf-ti)/dt)+1
t=ti:dt:tf
u=zeros(N)
u[1]=ui
for i=2:N
u[i]=fzero(u1->u1-u[i-1]-dt*f(u1, t[i]), u[i-1])
end
return t, u
end
Cheers,
João Paquim