数值计算非均匀二维数据的一阶导数
Numerically finding first derivative of non-uniform 2D data
我最初有 2 个数组 p 和角度,并将它们代入方程 fh 以获得第三个数组 z。获得这些数据后,我将数组 p 和角度更改为 p_perpendicular 和 p_parallel,其中 p_perpendicular = p* sin(angle) 和 p_parallel = p*cos(角度)。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from numpy import histogram2d
def func(p,alpha):
fh = (1/((1+((p**(1/2))))**(3/2)))*(1-np.tanh((np.cos(alpha*np.pi/180))**(1/2))+(1+np.tanh(alpha)**(1/2)))
return (fh)
p=[]
angle=[]
z=[]
for p_loop in np.arange(1e-23,4e-22,1e-23):
for alpha in np.arange(1,90,3):
p.append(p_loop)
angle.append(alpha)
z.append(func(p_loop,alpha))
p_parallel = (np.array(p)*np.cos(np.array(angle)*np.pi/180)) #array of parallel p
p_perpendicular = (np.array(p)*np.sin(np.array(angle)*np.pi/180)) #array of perpendicular p
我知道我可以从初始数据中找到 dz/dp 和 dz/d(angle) 的近似导数,但我不确定如何找到 dz/dp_parallel 的导数或 dz/dp_perpendicular.
我的第一个想法是使用 2d 直方图进行分箱,但这会导致很大的不准确性。是否有一些插值方法或极坐标合并方法或其他方法最好将数据放在 p_parallel 和 p_perpendicular space 中?
您已完成change of variables。来自链式法则:
dz/dp = dz/dp_par dp_par/dp + dz/dp_perp dp_perp/dp
dz/dangle = dz/dp_par dp_par/dangle + dz/dp_perp dp_perp/dangle
考虑到你的转变:
# the Jacobian of polar coordinates with respect to cartesian
# both `p` and `angle` are numpy arrays
J = np.array(
[
[np.cos(angle * np.pi / 180), -np.pi * p * np.sin(angle * np.pi / 180) / 180],
[np.sin(angle * np.pi / 180), np.pi * p * np.cos(angle * np.pi / 180) / 180],
]
)
# J.shape = (2, 2, 1170).
# Transpose it such that the longest dim is first
J = np.transpose(J, axes=(2, 0, 1))
dz = <<your approximation of derivative [dz/dp, dz/dangle]>>
# dz.shape = (1170, 2)
然后 dz/dp_par、dz/dp_perp 的近似值由 solving
给出
np.linalg.solve(J, dz)
我最初有 2 个数组 p 和角度,并将它们代入方程 fh 以获得第三个数组 z。获得这些数据后,我将数组 p 和角度更改为 p_perpendicular 和 p_parallel,其中 p_perpendicular = p* sin(angle) 和 p_parallel = p*cos(角度)。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from numpy import histogram2d
def func(p,alpha):
fh = (1/((1+((p**(1/2))))**(3/2)))*(1-np.tanh((np.cos(alpha*np.pi/180))**(1/2))+(1+np.tanh(alpha)**(1/2)))
return (fh)
p=[]
angle=[]
z=[]
for p_loop in np.arange(1e-23,4e-22,1e-23):
for alpha in np.arange(1,90,3):
p.append(p_loop)
angle.append(alpha)
z.append(func(p_loop,alpha))
p_parallel = (np.array(p)*np.cos(np.array(angle)*np.pi/180)) #array of parallel p
p_perpendicular = (np.array(p)*np.sin(np.array(angle)*np.pi/180)) #array of perpendicular p
我知道我可以从初始数据中找到 dz/dp 和 dz/d(angle) 的近似导数,但我不确定如何找到 dz/dp_parallel 的导数或 dz/dp_perpendicular.
我的第一个想法是使用 2d 直方图进行分箱,但这会导致很大的不准确性。是否有一些插值方法或极坐标合并方法或其他方法最好将数据放在 p_parallel 和 p_perpendicular space 中?
您已完成change of variables。来自链式法则:
dz/dp = dz/dp_par dp_par/dp + dz/dp_perp dp_perp/dp
dz/dangle = dz/dp_par dp_par/dangle + dz/dp_perp dp_perp/dangle
考虑到你的转变:
# the Jacobian of polar coordinates with respect to cartesian
# both `p` and `angle` are numpy arrays
J = np.array(
[
[np.cos(angle * np.pi / 180), -np.pi * p * np.sin(angle * np.pi / 180) / 180],
[np.sin(angle * np.pi / 180), np.pi * p * np.cos(angle * np.pi / 180) / 180],
]
)
# J.shape = (2, 2, 1170).
# Transpose it such that the longest dim is first
J = np.transpose(J, axes=(2, 0, 1))
dz = <<your approximation of derivative [dz/dp, dz/dangle]>>
# dz.shape = (1170, 2)
然后 dz/dp_par、dz/dp_perp 的近似值由 solving
给出np.linalg.solve(J, dz)