带复数的Python interp2D

2024-04-26 20:44:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用与matlab的interp2函数相同的方法为python编写interp2

我试过使用scipy interp2d函数,和matlabs inter2一样

Matlab:interp2(x,y,yy,new\u xx,new\u yy)

x=37、39、41

y=2.5、2.75、3

yy=[[0.6+1.6j,0.6+1.6j,0.6+1.6j],[0.7+1.6j,0.7+1.6j,0.7+1.6j],[0.8+1.5j,0.8+1.5j,0.8+1.5j]-3x3阵列

新建\u xx=np.L空间(37,41401)

新建\u yy=np.L空间(0.3401)

''

func = scipy.interpolate.interp2d(x,y,yy)

arr = func(new_xx,new_yy)

''

运行func=scipy.interpolate.interp2d(x,y,yy) “ComplexWarning:将复数值强制转换为实数将丢弃虚部”

我怎样才能用复数解释?你知道吗


Tags: 方法函数newnp空间scipyfuncxx
1条回答
网友
1楼 · 发布于 2024-04-26 20:44:05

一个解决方案是执行两个不同的插值:“如果V包含复数,那么interp2分别插值实部和虚部。”。你知道吗

使用^{}

import numpy as np
from scipy.interpolate import interp2d

x = np.array([37, 39, 41])

y = np.array([2.5, 2.75, 3])

z = np.array([[0.6 + 1.6j, 0.6 + 1.6j, 0.6 + 1.6j],
     [0.7 + 1.6j, 0.7 + 1.6j, 0.7 + 1.6j],
     [0.8 + 1.5j, 0.8 + 1.5j, 0.8 + 1.5j]])

# 2D grid interpolation
interpolator_real = interp2d(x, y, np.real(z))
interpolator_imag = interp2d(x, y, np.imag(z))

def interpolator_complex(x, y):
    return interpolator_real(x, y) + 1j*interpolator_imag(x, y)

# test
new_x = np.linspace(37, 41, 6)
new_y = np.linspace(2.5, 3, 8)

interpolator_complex(new_x, new_y)

相关问题 更多 >