I've animated a dynamics as follows in Python. However, I'd like to have the same in Mathematica (whose graphics output, in my view, is fancier than Matplotlib).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import mpl_toolkits.mplot3d.axes3d as p3
from matplotlib import animation
plt.style.use('seaborn-pastel')
def f(p, t):
x1 = p[0]
y1 = p[1]
x2 = p[2]
y2 = p[3]
dx1dt = x2 - x1
dy1dt = y2 - y1
dx2dt = x1 - x2
dy2dt = y1 - y2
return np.array ([dx1dt, dy1dt, dx2dt, dy2dt])
ts = np.arange (0 , 20 , 0.1)
x10 = -5
y10 = -7
x20 = -5
y20 = 7
p = np.array ([x10 , y10, x20 , y20])
s = odeint (f , p , ts)
x1s = s [: , 0]
y1s = s [: , 1]
x2s = s [: , 2]
y2s = s [: , 3]
fig = plt.figure()
ax = p3.Axes3D(fig)
def update_lines(num, dataLines, lines):
for line, data in zip(lines, dataLines):
line.set_data(data[0:2, :num])
line.set_3d_properties(data[2, :num])
line.set_marker("o")
#line.set_label("p1")
#plt.legend()
return lines
data = np.array([[x1s,y1s,ts],[x2s,y2s,ts]])
lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data]
ax.set_xlim(-10.1,10.1)
ax.set_ylim(-10.1,10.1)
ax.set_zlim(0,20)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("t")
ax.legend()
anim = animation.FuncAnimation(fig, update_lines, 50, fargs=(data, lines),
interval=200, blit=True, repeat=True)
plt.rcParams['animation.ffmpeg_path'] ='C:\\...\\FFmpeg\\bin\\ffmpeg.exe'
FFwriter = animation.FFMpegWriter(fps = 100)
anim.save('test.mp4', writer = FFwriter)
plt.show()
As a newbie in using Mathematica, here is what I have planned to recreate the animation above.
1- Numerically integrating the system using DSolve[]
DSolve[{x1'[t] == -x1[t] + x2[t],
y1'[t] == -y1[t] + y2[t],
x2'[t] == x1[t] - x2[t],
y2'[t] == y1[t] - y2[t], -5, -7, -5, 7}, x1[t], y1[t],
x2[t], y2[t], t]
2- Animating the integration result using Animate[Plot3D[...]]
But, I don't know how to set variation ranges corresponding to my variables. Can you please give a clue?
Edit:
The desired script does numerically integrate the following system of equations
dx1dt = x2 - x1
dy1dt = y2 - y1
dx2dt = x1 - x2
dy2dt = y1 - y2
According to the initial conditions below
x10 = -5
y10 = -7
x20 = -5
y20 = 7
Then the overall dynamics of the system is animated by varying the time t
between $0$ to $20$ units.
t
. How do you make a 3D plot out of them? 3D plot needs one 1 function that depends on 2 parameters. $\endgroup$(x,y,t)
in a 3D space. $\endgroup$