使用`fsolve`绘制隐式函数。

huangapple go评论66阅读模式
英文:

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数组(XY)。在调用f()之前尝试对XY进行重新整形。

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值,而它应该具有与XY相同的形状。以下是一个示例(如果希望,可以将其扁平化为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线框图,您需要使XYZ输入成为2D数组。因此,步骤如下:

  • keep the 2D shape of either X or Y (same ones) outputted by the meshgrid function,
  • 保留由meshgrid函数输出的XY(相同的那个)的2D形状,
  • flatten X and Y to get all positions and compute Z for each position.
  • XY展平以获取所有位置,并计算每个位置的Z值。
  • reshape X, Y, and Z to the initial 2D shape
  • XYZ重塑为最初的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 or Y (same ones) outputted by the meshgrid function,
  • flatten X and Y to get all positions and compute Z for each position.
  • reshape X, Y and Z 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 :

使用`fsolve`绘制隐式函数。

huangapple
  • 本文由 发表于 2023年3月4日 00:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定