Romeo and Juliet

This demo runs an example from Section 11.2.6 in Greenbaum & Chartier

In [1]:
using ODE
# declare the forcing function
function love(t,v) 
    dvdt = zeros(2)
    dvdt[1] = -0.2*v[2]
    dvdt[2] = 0.8*v[1]
    return dvdt
end

# ode45 - forcing, initial, time-span
T,V = ode45(love,[2.,2.],[0.,60.])
V = hcat(V...)' # convert to matrix
Out[1]:
94x2 Array{Float64,2}:
  2.0        2.0     
  1.98307    2.06632 
  1.60604    3.11169 
  1.05388    3.94428 
  0.38327    4.40595 
 -0.141322   4.46319 
 -0.657546   4.2744  
 -1.13755    3.85018 
 -1.5549     3.21389 
 -1.88661    2.40058 
 -2.1144     1.45505 
 -2.23511    0.130685
 -2.17134   -1.06818 
  ⋮                  
 -2.11247    1.46611 
 -2.23493    0.141737
 -2.1724    -1.05944 
 -1.97426   -2.09972 
 -1.59782   -3.12851 
 -1.04276   -3.95604 
 -0.371128  -4.41007 
  0.150083  -4.46201 
  0.662562  -4.27126 
  1.13903   -3.84838 
  1.5536    -3.21635 
  1.75393   -2.77391 
In [2]:
using Plots; plotly();
plot(T,V[:,1],label="Romeo")
plot!(T,V[:,2],label="Juliet")
xlabel!("time") 
ylabel!("affection")
title!("Love in time")