I’m trying to reformat a python code that plots a phase portrait with vector field. It seems that I did everything right, but the error is in the quiver () function. What did I write wrong?
Python code (working correctly):
# Phase portrait with vector field. Check two systems are the same.
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
import pylab as pl
# The 2-dimensional linear system.
a, b, c, d = 2, 1, 1, 2
def dx_dt(x, t):
return [a*x[0] + b*x[1], c*x[0] + d*x[1]]
# Trajectories in forward time.
ts = np.linspace(0, 4, 100)
ic = np.linspace(-1, 1, 5)
for r in ic:
for s in ic:
x0 = [r, s]
xs = odeint(dx_dt, x0, ts)
plt.plot(xs[:,0], xs[:,1], "r-")
# Trajectories in backward time.
ts = np.linspace(0, -4, 100)
ic = np.linspace(-1, 1, 5)
for r in ic:
for s in ic:
x0 = [r, s]
xs = odeint(dx_dt, x0, ts)
plt.plot(xs[:,0], xs[:,1], "r-")
# Label the axes and set fontsizes.
plt.xlabel("x", fontsize=15)
plt.ylabel("y", fontsize=15)
plt.tick_params(labelsize=15)
plt.xlim(-1, 1)
plt.ylim(-1, 1);
# Plot the vectorfield.
X,Y = np.mgrid[-1:1:10j, -1:1:10j]
u = a*X + b*Y
v = c*X + d*Y
pl.quiver(X, Y, u, v, color = "b")
plt.show()
Output which I want to get:
My Julia code:
using Plots
using SciPy
using LinearAlgebra
using DifferentialEquations
# The 2-dimensional linear system.
a, b, c, d = 2, 1, 1, 2
function dx_dt(du, u, p, t)
du[1] = a*u[1] + b*u[2]
du[2] = c*u[1] + d*u[2]
end
# Trajectories in forward time.
ts = LinRange(0, 4, 100)
ic = LinRange(-1, 1, 5)
tspan = (0.0, 4.0)
for r in ic
for s in ic
x0 = [r, s]
xs = solve(ODEProblem(dx_dt, x0, tspan))
Plots.plot(xs, color="red", linestyle="-")
end
end
# Trajectories in backward time.
ts = LinRange(0, -4, 100)
ic = LinRange(-1, 1, 5)
tspan = (-4.0, 0.0)
for r in ic
for s in ic
x0 = [r, s]
xs = solve(ODEProblem(dx_dt, x0, tspan))
Plots.plot(xs, color="red", linestyle="-")
end
end
# Label the axes and set fontsizes.
xlabel!("x")
ylabel!("y")
#fontfamily("sans-serif")
#fontsize!(15)
xlims!(-1, 1)
ylims!(-1, 1)
# Plot the vectorfield.
function meshgrid(x, y)
X = repeat(x', length(y), 1)
Y = repeat(y, 1, length(x))
return (X, Y)
end
x = range(-1, 1, length=10)
y = range(-1, 1, length=10)
a, b, c, d = 1, 1, -1, 1
X, Y = meshgrid(x, y)
u = a .* X .+ b .* Y
v = c .* X .+ d .* Y
quiver(X, Y, u, v, color="blue", alpha=0.5, aspect_ratio=:equal, framestyle=:box)```
My error:
Couldn’t process recipe args: (Matrix{Float64}, Matrix{Float64}, Matrix{Float64}, Matrix{Float64})