英文:
Plotting implicit function using fsolve
问题
This is the translated code part without the error message:
我尝试使用scipy.fsolve绘制一个隐式函数,但似乎无法让它正常工作。
我想绘制函数z(x, y),其中x + y + z + sin(z) = 0,使用scipy.fsolve。
import numpy as np
import scipy
import matplotlib.pyplot as plt
f = lambda x, y: scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=0).tolist()[0]
x = np.linspace(-1, 1, 50)
y = np.linspace(-1, 1, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
plt.show()
以下是你的第二部分代码的翻译:
def z_func(x, y):
z_solve = scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=np.zeros_like(x))
return z_solve.tolist()[0]
x = np.linspace(-1, 1, 50)
y = np.linspace(-1, 1, 40)
X, Y = np.meshgrid(x, y)
X = X.reshape(-1)
Y = Y.reshape(-1)
Z = z_func(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='red')
plt.show()
希望这对你有所帮助!
英文:
I am trying to plot an implicit function using scipy.fsolve but cant seem to get it to work.
I want to plot the function z(x,y) where x + y + z + sin(z) = 0 using scipy.fsolve.
import numpy as np
import scipy
import matplotlib.pyplot as plt
f = lambda x,y: scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0 = 0).tolist()[0]
x = np.linspace(-1,1,50)
y = np.linspace(-1,1,40)
X,Y = np.meshgrid(x,y)
Z = f(X,Y)
fig =plt.figure()
plt.show()
`
This gives me "_minpack.error: Result from function call is not a proper array of floats".
I dont see why this would be since I can call the function on individual values and its return type is float. It seems like the controversial line is Z = f(X,Y).
Thankful for any help!
EDIT
The original error is fixed for the most part however
def z_func(x, y): z_solve = scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=np.zeros_like(x))
return z_solve.tolist()[0]
x = np.linspace(-1,1,50)
y = np.linspace(-1,1,40)
X,Y = np.meshgrid(x,y)
X = X.reshape(-1)
Y = Y.reshape(-1)
Z = z_func(X,Y)
fig =plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color = 'red')
plt.show()
now gives me AttributeError: 'float' object has no attribute 'ndim'
答案1
得分: 0
This gives me "minpack.error: Result from function call is not a proper array of floats".
这给我返回了“minpack.error: 函数调用结果不是合适的浮点数数组”,
This is because you're sending 2D arrays (X
and Y
) in the fsolve
function. Try reshaping X
and Y
before the f()
call.
这是因为您在fsolve
函数中发送了2D数组(X
和Y
)。在调用f()
之前尝试对X
和Y
进行重新整形。
X = X.reshape(-1)
Y = Y.reshape(-1)
You'll come across a second error because the x0
is set to a unique 0
value while it should have the same shape as X
and Y
. Here is an example (you can flat it as a lambda function if you want):
您将遇到第二个错误,因为x0
设置为唯一的0
值,而它应该具有与X
和Y
相同的形状。以下是一个示例(如果希望,可以将其扁平化为lambda函数):
def z_func(x, y):
z_solve = scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=np.zeros_like(x))
return z_solve.tolist()[0]
To plot a 3D wireframe, you will need X
, Y
, and Z
inputs to be 2D arrays. So the steps would be:
要绘制3D线框图,您需要使X
,Y
和Z
输入成为2D数组。因此,步骤如下:
- keep the 2D shape of either
X
orY
(same ones) outputted by themeshgrid
function, - 保留由
meshgrid
函数输出的X
或Y
(相同的那个)的2D形状, - flatten
X
andY
to get all positions and compute Z for each position. - 将
X
和Y
展平以获取所有位置,并计算每个位置的Z值。 - reshape
X
,Y
, andZ
to the initial 2D shape - 将
X
,Y
和Z
重塑为最初的2D形状 - call
ax.plot_wireframe(X, Y, Z)
- 调用
ax.plot_wireframe(X, Y, Z)
# [...]
X, Y = np.meshgrid(x, y)
grid_shape = X.shape # same as Y.shape
# Flatten X and Y to get all possible positions
X = X.reshape(-1)
Y = Y.reshape(-1)
positions = np.dstack([X, Y])[0]
# Compute Z values for each position
Z = np.array([f(xpos, ypos) for xpos, ypos in positions])
# Reshape everything to be plotted in 3D axis
X = X.reshape(grid_shape)
Y = Y.reshape(grid_shape)
Z = Z.reshape(grid_shape)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='red')
You should get the following figure:
您应该会得到以下图像:
英文:
> ` This gives me "_minpack.error: Result from function call is not a proper array of floats".
This is because you're sending 2D arrays (X
and Y
) in the fsolve
function. Try reshaping X
and Y
before the f()
call.
X = X.reshape(-1)
Y = Y.reshape(-1)
You'll come across a second error because the x0
is set to a unique 0
value while it should have the same shape as X
and Y
. Here is an example (you can flat it as a lambda function if you want):
def z_func(x, y):
z_solve = scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=np.zeros_like(x))
return z_solve.tolist()[0]
EDIT following the additional information provided in the comments section
To plot a 3D wireframe, you will X
, Y
and Z
inputs to be 2D arrays. So the steps would be:
- keep the 2D shape of either
X
orY
(same ones) outputted by themeshgrid
function, - flatten
X
andY
to get all positions and compute Z for each position. - reshape
X
,Y
andZ
to the initial 2D shape - call
ax.plot_wireframe(X, Y, Z)
# [...]
X, Y = np.meshgrid(x,y)
grid_shape = X.shape # same has Y.shape
# Flatten X and Y to get all possible positions
X = X.reshape(-1)
Y = Y.reshape(-1)
positions = np.dstack([X, Y])[0]
# Compute Z values for each position
Z = np.array([f(xpos, ypos) for xpos, ypos in positions])
# Reshape everything to be plotted in 3D axis
X = X.reshape(grid_shape)
Y = Y.reshape(grid_shape)
Z = Z.reshape(grid_shape)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color = 'red')
You should get the following figure :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论