我打算繪制以下函式:
X = [x, y]
f = c'X norm(c)* (sum(cos^2(X)) - sum(log(b-aX))
c = [7,2]
a = [2,3;4,6]
b = [10, 50]
所以我想得到這個
7x 2y 7.2*(cos^2(pi*x) cos^2(pi*y))-(log(10-2x-3y) log(50-4x-6y) log(x) log(y))
如何繪制上述函式,比方說,什么時候-5<=x<=5-3<=y<=2?
我嘗試了以下方法:
import numpy as np
import matplotlib.pyplot as plt
from sympy import symbols
x,y = symbols(['x','y'])
c = np.array([7,2])
A = np.array([[2,3],[4,6]])
b = np.array([10,50])
那接下來怎么辦??不知道要不要用linspace??
plt.plot(A*x norm*(np.sum(np.cos^2(pi*x))-np.sum(log10(b-A*x))
uj5u.com熱心網友回復:
您可以使用 sympy 函式sympy.Function('cos')(x)和('log')(x)然后lambdify。我沒有寫下太長的確切功能,但簡化版本的作業原理是這樣的
import sympy
from sympy import *
from sympy.utilities.lambdify import lambdify, implemented_function
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
if __name__=="__main__":
x, y = symbols('x y')
X = np.array([x,y])
a = np.array([[2,3],[4,6]])
b = np.array([10, 50]) # np.tile(b, (2,1))
c = np.array([7, 2])
fPart1 = sum(c*np.array( X ) )
fPart2 = sum(np.array( c*np.array([sympy.Function('cos')(x) for x in X])))
fPart3a = sum(c*np.array( sympy.Function('log')(sum(b - a[0,:]*X) )))
fPart3b = sum(c*np.array( sympy.Function('log')(sum(b - a[1,:]*X) )))
fPart3 = fPart3a fPart3b
zFunction = lambdify([x, y], fPart1 fPart2*fPart2 fPart3)
xValues = np.linspace(-5, 5, 100)
yValues = np.linspace(-3, 2, 100)
X, Y = np.meshgrid(xValues, yValues)
Z = zFunction(X, Y)
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_xlabel('X', fontsize=20, rotation=150)
ax.set_ylabel('Y', fontsize=20)
ax.set_zlabel('Z', fontsize=20)
plt.show()
列印功能以了解您想要的功能
In [4]: fPart1
Out[4]: 7*x 2*y
In [5]: fPart2
Out[5]: 7*cos(x) 2*cos(y)
In [6]: fPart3
Out[6]: 9*log(-4*x - 6*y 60) 9*log(-2*x - 3*y 60)

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/316996.html
標籤:Python 麻木的 matplotlib 同情
下一篇:繪制趨勢線Python
