python/Matplotlib绘制复变函数图像教程

作者:落叶_小唱 时间:2023-08-03 07:36:43 

今天发现sympy依赖的库mpmath里也有很多数学函数,其中也有在复平面绘制二维图的函数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绘制复变函数图像教程

参照matlab绘制复变函数的例子,使用python实现绘制复变函数图像,网上还没搜到相关的文章,在这里分享出来供大家学习。


'''
参照matlab绘制复变函数的例子,创建函数cplxgrid,cplxmap,cplxroot
'''
# 1.导入相关库
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import *

# 2.创建函数
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))#复变函数实部最大值
m = np.min(np.min(u))#复变函数实部最大值
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)

#绘制复变函数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函数'''
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函数'''
cplxroot(n=2)
cplxroot(n=3)
cplxroot(n=4)
cplxroot(n=5)

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

python/Matplotlib绘制复变函数图像教程

python/Matplotlib绘制复变函数图像教程

来源:https://blog.csdn.net/ouening/article/details/82349372

标签:python,Matplotlib,复变函数
0
投稿

猜你喜欢

  • MySQL 集群配置

    2009-04-20 14:15:00
  • Python3 replace()函数使用方法

    2023-07-21 22:25:13
  • 对Python中的@classmethod用法详解

    2023-07-22 17:55:54
  • ThinkPHP框架实现用户信息查询更新及删除功能示例

    2024-06-07 15:34:11
  • Python变量及数据类型用法原理汇总

    2022-12-04 11:11:41
  • Python列表解析操作实例总结

    2022-10-10 09:59:38
  • python 实现全球IP归属地查询工具

    2023-10-05 16:31:33
  • 解决golang.org不能访问的问题(推荐)

    2024-05-08 10:53:22
  • 带你学习MySQL执行计划

    2024-01-18 12:34:37
  • vue自定义filters过滤器

    2024-04-30 08:45:12
  • python 实现有道翻译功能

    2022-03-25 17:30:55
  • 浅谈vuex的基本用法和mapaction传值问题

    2024-06-05 09:18:21
  • 对Python Pexpect 模块的使用说明详解

    2022-10-24 02:14:09
  • Python实现简单多线程任务队列

    2022-07-29 13:21:43
  • Blazor Server 应用程序中进行 HTTP 请求

    2024-01-24 07:56:38
  • python异步任务队列示例

    2021-05-04 03:09:07
  • python 机器学习之支持向量机非线性回归SVR模型

    2022-06-17 20:23:55
  • Oracle 数据库中创建合理的数据库索引

    2024-01-24 12:37:05
  • MySQL优化之数据表的处理

    2008-12-22 14:45:00
  • PHP Session条件竞争超详细讲解

    2023-06-03 12:49:00
  • asp之家 网络编程 m.aspxhome.com