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

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

Plotting implicit function using fsolve

问题

This is the translated code part without the error message:

  1. 我尝试使用scipy.fsolve绘制一个隐式函数但似乎无法让它正常工作
  2. 我想绘制函数z(x, y)其中x + y + z + sin(z) = 0使用scipy.fsolve
  3. import numpy as np
  4. import scipy
  5. import matplotlib.pyplot as plt
  6. f = lambda x, y: scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=0).tolist()[0]
  7. x = np.linspace(-1, 1, 50)
  8. y = np.linspace(-1, 1, 40)
  9. X, Y = np.meshgrid(x, y)
  10. Z = f(X, Y)
  11. fig = plt.figure()
  12. plt.show()

以下是你的第二部分代码的翻译:

  1. def z_func(x, y):
  2. z_solve = scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=np.zeros_like(x))
  3. return z_solve.tolist()[0]
  4. x = np.linspace(-1, 1, 50)
  5. y = np.linspace(-1, 1, 40)
  6. X, Y = np.meshgrid(x, y)
  7. X = X.reshape(-1)
  8. Y = Y.reshape(-1)
  9. Z = z_func(X, Y)
  10. fig = plt.figure()
  11. ax = plt.axes(projection='3d')
  12. ax.plot_wireframe(X, Y, Z, color='red')
  13. 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.

  1. import numpy as np
  2. import scipy
  3. import matplotlib.pyplot as plt
  4. f = lambda x,y: scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0 = 0).tolist()[0]
  5. x = np.linspace(-1,1,50)
  6. y = np.linspace(-1,1,40)
  7. X,Y = np.meshgrid(x,y)
  8. Z = f(X,Y)
  9. fig =plt.figure()
  10. 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

  1. def z_func(x, y): z_solve = scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=np.zeros_like(x))
  2. return z_solve.tolist()[0]
  3. x = np.linspace(-1,1,50)
  4. y = np.linspace(-1,1,40)
  5. X,Y = np.meshgrid(x,y)
  6. X = X.reshape(-1)
  7. Y = Y.reshape(-1)
  8. Z = z_func(X,Y)
  9. fig =plt.figure()
  10. ax = plt.axes(projection='3d')
  11. ax.plot_wireframe(X, Y, Z, color = 'red')
  12. 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进行重新整形。

  1. X = X.reshape(-1)
  2. 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函数):

  1. def z_func(x, y):
  2. z_solve = scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=np.zeros_like(x))
  3. 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)
  1. # [...]
  2. X, Y = np.meshgrid(x, y)
  3. grid_shape = X.shape # same as Y.shape
  4. # Flatten X and Y to get all possible positions
  5. X = X.reshape(-1)
  6. Y = Y.reshape(-1)
  7. positions = np.dstack([X, Y])[0]
  8. # Compute Z values for each position
  9. Z = np.array([f(xpos, ypos) for xpos, ypos in positions])
  10. # Reshape everything to be plotted in 3D axis
  11. X = X.reshape(grid_shape)
  12. Y = Y.reshape(grid_shape)
  13. Z = Z.reshape(grid_shape)
  14. fig = plt.figure()
  15. ax = plt.axes(projection='3d')
  16. 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.

  1. X = X.reshape(-1)
  2. 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):

  1. def z_func(x, y):
  2. z_solve = scipy.optimize.fsolve(lambda z: x + y + z + np.sin(z), x0=np.zeros_like(x))
  3. 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)
  1. # [...]
  2. X, Y = np.meshgrid(x,y)
  3. grid_shape = X.shape # same has Y.shape
  4. # Flatten X and Y to get all possible positions
  5. X = X.reshape(-1)
  6. Y = Y.reshape(-1)
  7. positions = np.dstack([X, Y])[0]
  8. # Compute Z values for each position
  9. Z = np.array([f(xpos, ypos) for xpos, ypos in positions])
  10. # Reshape everything to be plotted in 3D axis
  11. X = X.reshape(grid_shape)
  12. Y = Y.reshape(grid_shape)
  13. Z = Z.reshape(grid_shape)
  14. fig = plt.figure()
  15. ax = plt.axes(projection='3d')
  16. 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:

确定