Skip to content

Instantly share code, notes, and snippets.

@SzShow
Last active October 19, 2019 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SzShow/dacdee8659e70cfebc6a2ed55ba1effa to your computer and use it in GitHub Desktop.
Save SzShow/dacdee8659e70cfebc6a2ed55ba1effa to your computer and use it in GitHub Desktop.
Jacobian -Introduce-
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as Axes3D
import matplotlib.animation as anim
from matplotlib.animation import PillowWriter
import numpy as np
from numpy import sin, cos, pi
def ExFunc(v):
A = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 0]])
v_out = A @ v
return v_out
def Pole2Car(r, theta, phi):
x = r*cos(theta)*cos(phi)
y = r*sin(theta)*cos(phi)
z = r*sin(phi)
return np.array([[x], [y], [z]])
r = 1
phi = pi/6
T = np.linspace(0, 2, 120)
X_zero = (0, 0)
Y_zero = (0, 0)
Z_zero = (0, 0)
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
ax.set_title("Projection Func")
ax.set_xlabel("x", size=14)
ax.set_ylabel("y", size=14)
ax.set_zlabel("z", size=14)
ax.set_xlim(-1.1, 1.1)
ax.set_ylim(-1.1, 1.1)
ax.set_zlim(-0.1, 1.1)
imgs = []
for t in T:
theta = 2*pi*(t/2)
V_in = Pole2Car(r, theta, phi)
v_in = V_in.flatten()
V_out = ExFunc(V_in)
v_out = V_out.flatten()
X = (v_in[0], v_out[0])
Y = (v_in[1], v_out[1])
Z = (v_in[2], v_out[2])
im = ax.quiver(X_zero, Y_zero, Z_zero, X, Y, Z,
length=r, arrow_length_ratio=0.1,
color=('blue', 'red'))
imgs.append([im])
ani = anim.ArtistAnimation(fig, imgs, interval=66)
plt.show()
ani.save("PeojectionFuncEx.gif", writer="pillow")
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as Axes3D
import matplotlib.animation as anim
from matplotlib.animation import PillowWriter
import numpy as np
from numpy import sin, cos, pi
def ExFunc(t):
return p_x(t), p_y(t), p_z(t)
def p_x(t):
return cos(2*pi*0.2*t)
def p_y(t):
return sin(2*pi*0.2*t)
def p_z(t):
return t
T_end = np.linspace(0, 10, 100)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_title("Spiral Func")
ax.set_xlabel("x", size=14)
ax.set_ylabel("y", size=14)
ax.set_zlabel("z", size=14)
imgs = []
for t_end in T_end:
t = np.linspace(0, t_end, t_end*10)
im = ax.plot(*ExFunc(t), color="blue")
imgs.append(im)
ani = anim.ArtistAnimation(fig, imgs, interval=100)
# plt.show()
# 保存用に使用
#ani.save("VectorFuncEx.gif", writer="pillow")
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
def ExFunc(x, y):
return -x ** 2-y ** 2+1
def FuncPlot2(f, x, y):
out = np.zeros((len(x), len(y)))
for i_y, p_y in enumerate(y):
for i_x, p_x in enumerate(x):
out[i_x, i_y] = f(p_x, p_y)
return out
Interval = np.array([-5, 5])
Nsamp = 100
x = np.linspace(-5, 5, Nsamp)
y = np.linspace(-5, 5, Nsamp)
h = FuncPlot2(ExFunc, x, y)
X, Y = np.meshgrid(x, y)
figure = plt.figure()
ax = figure.add_subplot(111, projection='3d')
ax.set_xlabel("x", size=14)
ax.set_ylabel("y", size=14)
ax.set_zlabel("h", size=14)
surf = ax.plot_surface(X, Y, h, cmap="bwr", linewidth=0)
figure.colorbar(surf)
ax.set_title("TwoVariableFunc")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment