In [1]:
using Plots; plotly(size=(200,200)); plot(Plots.fakedata(5,10)); # fully load plots
; # hide output
[Plots.jl] Initializing backend: plotly
In [2]:
# Define the ODE for a simulation of Hooke's law.
k = 0.5
s = [0; 1.0]
f = (t,y) -> [0 1.0; -k 0.0]*y

# Do the simulation!
h = 0.01
Tmax = 100
tsteps = collect(0:h:Tmax)
nsteps = length(tsteps)
y = s
yhist = zeros(nsteps,2)
yhist[1,:] = s
for i=2:nsteps
    t = tsteps[i]
    y = y + h*f(t,y)
    yhist[i,:] = y
end
plot(tsteps,yhist[:,1])
Out[2]:
In [3]:
# Define the ODE for a simulation of Hooke's law.
k = 0.5
s = [0; 1.0]
# f = (t,y) -> [0 1.0; -k 0.0]*y
A = [0 1.0; -k 0.0] # why won't f work here?

# Do the simulation!
h = 0.01
Tmax = 100
tsteps = collect(0:h:Tmax)
nsteps = length(tsteps)
y = s
yhist = zeros(nsteps,2)
yhist[1,:] = s
for i=2:nsteps
    t = tsteps[i]
    y = (eye(2) - h*A)\y
    yhist[i,:] = y
end
plot(tsteps,yhist[:,1])
Out[3]:
In [5]:
using ODE
tout,yout = ode45(f,s,[0.,100])
yhist = hcat(yout...)' # convert to matrix
plot(tout,yhist[:,1])
Out[5]: