溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python/Matplotlib如何繪制復(fù)變函數(shù)圖像

發(fā)布時間:2021-05-19 11:06:34 來源:億速云 閱讀:611 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了python/Matplotlib如何繪制復(fù)變函數(shù)圖像,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

今天發(fā)現(xiàn)sympy依賴的庫mpmath里也有很多數(shù)學(xué)函數(shù),其中也有在復(fù)平面繪制二維圖的函數(shù)cplot,具體例子如下

from mpmath import *

def f1(z):
 return z

def f2(z):
 return z**3

def f3(z):
 return (z**4-1)**(1/4)

def f4(z):
 return 1/z

def f5(z):
 return atan(z)

def f6(z):
 return sqrt(z)

cplot(f1)
cplot(f2)
cplot(f3)
cplot(f4)
cplot(f5)
cplot(f6)

python/Matplotlib如何繪制復(fù)變函數(shù)圖像

參照matlab繪制復(fù)變函數(shù)的例子,使用python實現(xiàn)繪制復(fù)變函數(shù)圖像,網(wǎng)上還沒搜到相關(guān)的文章,在這里分享出來供大家學(xué)習(xí)。

'''
參照matlab繪制復(fù)變函數(shù)的例子,創(chuàng)建函數(shù)cplxgrid,cplxmap,cplxroot
'''
# 1.導(dǎo)入相關(guān)庫
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import *

# 2.創(chuàng)建函數(shù)
def cplxgrid(m):
 '''Return polar coordinate complex grid.

 Parameters
 ----------
 m: int

 Returns
 ----------
 z: ndarray,with shape (m+1)-by-(2*(m+1))
 '''
 m = m
 r = np.arange(0,m).reshape(m,1) / m
 theta = np.pi * np.arange(-m,m) / m
 z = r * np.exp(1j * theta)

 return z

def cplxroot(n=3,m=20):
 '''
 cplxroot(n): renders the Riemann surface for the n-th root
 cplxroot(): renders the Riemann surface for the cube root.
 cplxroot(n,m): uses an m-by-m grid. Default m = 20.

 Use polar coordinates, (r,theta).
 Use polar coordinates, (r,theta).

 Parameters
 ----------
 n: n-th root
 m: int

 Returns
 ----------
 None: Plot the Riemann surface
 '''
 m = m+1
 r = np.arange(0,m).reshape(m,1) / m
 theta = np.pi * np.arange(-n * m, n * m) / m
 z = r * np.exp(1j * theta)
 s = r * (1/n) * np.exp(1j * theta / n)
 fig = plt.figure()
 ax = fig.add_subplot(111,projection='3d')
 # ax.plot_surface(np.real(z),np.imag(z),np.real(s),color = np.imag(s))
 ax.plot_surface(np.real(z),np.imag(z),np.real(s),cmap = plt.cm.hsv)
 ax.set_xlim((-1,1))
 ax.set_ylim((-1,1))
 ax.set_xlabel('Real')
 ax.set_ylabel('Imag')
 ax.set_xticks([])
 ax.set_yticks([])
 ax.set_zticks([])
 ax.set_autoscalez_on(True)#z軸自動縮放 
 ax.grid('on')
 plt.show()

def cplxmap(z,cfun):
 '''
 Plot a function of a complex variable.

 Parameters
 ----------
 z: complex plane
 cfun: complex function to plot

 Returns
 ----------
 None: Plot the surface of complex function
 '''
 blue = 0.2
 x = np.real(z)
 y = np.imag(z)
 u = np.real(cfun)
 v = np.imag(cfun)
 M = np.max(np.max(u))#復(fù)變函數(shù)實部最大值
 m = np.min(np.min(u))#復(fù)變函數(shù)實部最大值
 s = np.ones(z.shape)
 fig = plt.figure()
 ax = fig.add_subplot(111,projection='3d')
 # 投影部分用線框圖
 surf1 = ax.plot_wireframe(x,y,m*s,cmap=plt.cm.hsv)
 surf2 = ax.plot_surface(x,y,u,cmap=plt.cm.hsv)

 #繪制復(fù)變函數(shù)1/z時會出錯,ValueError: Axis limits cannot be NaN or Inf
 # ax.set_zlim(m, M) 
 ax.set_xlim((-1,1))
 ax.set_ylim((-1,1))
 ax.set_xlabel('Real')
 ax.set_ylabel('Imag')
 ax.set_xticks([])
 ax.set_yticks([])
 ax.set_zticks([])
 ax.set_autoscalez_on(True)#z軸自動縮放

 ax.grid('on')
 plt.show()

def _test_cplxmap():
 '''測試cplxmap函數(shù)'''
 z = cplxgrid(30)
 w1 = z
 w2 = z**3
 w3 = (z**4-1)**(1/4)
 w4 = 1/z
 w5 = np.arctan(2*z)
 w6 = np.sqrt(z)
 w = [w1,w2,w3,w4,w5,w6]
 for i in w:
 cplxmap(z,i)

def _test_cplxroot():
 '''測試cplxroot函數(shù)'''
 cplxroot(n=2)
 cplxroot(n=3)
 cplxroot(n=4)
 cplxroot(n=5)

if __name__ == '__main__':
 _test_cplxmap()
 _test_cplxroot()

python/Matplotlib如何繪制復(fù)變函數(shù)圖像

python/Matplotlib如何繪制復(fù)變函數(shù)圖像

python有哪些常用庫

python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“python/Matplotlib如何繪制復(fù)變函數(shù)圖像”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI