r 类似linux diff函数_python已知实部或虚部求解析函数

9.2更新返璞归真的版本(修补定积分的问题)

fbbaa16c096e3dabe3f56044cccc1c54.png
from sympy import *
real = True # 实部或者虚部的判定
x, y = symbols('x y')
u = exp(x)*sin(y) # 已知函数
ux = diff(u, x) # 对x的偏导
uy = diff(u, y) # 对y的偏导
if real:# 已知实部求虚部
        vx = -uy # 柯西-黎曼条件
        vy = ux
        v = integrate(limit(vx, y, 0), x) + integrate(vy,(y, 0, y)) # 曲线积分求全微分的导数
        fz = simplify(u+v*I)
else:
        vx = uy # 黎曼-柯西条件
        vy = -ux 
        v = integrate(limit(vx, y, 0), x) + integrate(vy,(y, 0, y))
        fz = simplify(v+u*I)        
print("Analytic function:",fz) #目标解析函数 

9.4 更新曲线族

2034b0a94bd8d6b237b669fcb710a0c7.png
plt.contour(x, y, Zreal(x, y), 15, colors = "r")#已知部分的曲线族
plt.colorbar()
plt.contour(x, y, Zimaginary(x, y), 15, alpha = 0.85, colors = "b")#解出部分的曲线族
plt.colorbar()
plt.show()

=======================================================

已知解析函数

的实部
或虚部
,求该解析函数。

这种题真的是世纪折磨,真的无聊又费劲。

是时候让成熟的python替我分担烦恼了。

首先试试最基础的

成熟的python很快就解出来了

2c85254f88824f83626aea23e8823406.png

d759c54f91f78beaaff9a9c99257c2a0.png
实部和虚部的三维图像

b13691373f44c97cd73769b628cdff89.png
实部和虚部的Contour

毫无水平的绘图,完全就是花里胡哨。但就是给人一种该死高端的感觉。。。所以我还是随便画了出来。

02976ac7eeb595bfe54a61deee29ac30.png
换个复杂点的也毫无压力

代码的话没啥亮点也毫无技术含量。完全就是无脑把教材的计算过程原封不动的搬到python上。顺便闲的没事折腾一些花里胡哨的玩意儿。

先把又臭又长的垃圾放上,真正有用的可能不超过10行。。。

"""
Find analytic function, given real or imaginary part
"""
import matplotlib.pyplot as plt
import numpy as np
from sympy import *
from sympy.plotting import plot3d
x, y = symbols('x y')
u = exp(x)*sin(y) # Real or imaginary part
#u = (x**2 - y**2)/(x**2 + y**2)**2
#u = y/(x**2+y**2)
real = True # False for imaginary 
# Partial derivative
ux = simplify(diff(u, x))
uy = simplify(diff(u, y))
# Harmonic Conjugate Function
uxx = diff(ux, x)
uyy = diff(uy, y)
if simplify(uxx + uyy) == 0:
   print ("Harmonic Conjugate Function")
else:
   p1 = plot3d(uxx)
   p2 = plot3d(-uyy)
   print ("If they look similar, then just treat them as Harmonic Conjugate Functions...")
print("")
print("Cauchy–Riemann equations:")
if real:
    # Cauchy–Riemann equations
    vx = -uy
    vy = ux
    print("ux =", ux)
    print("uy =", uy)
    print("vx =", vx)
    print("vy =", vy)
    print("")
    
    vxy = simplify(diff(vx, y))
    vyx = simplify(diff(vy, x))
    if simplify(vxy-vyx) == 0:
        print("path independent")
    else:
        print("maybe not path independent")
    v = simplify(integrate(integrate(vxy, y), x))
    dvdx = diff(v, x)
    if simplify(dvdx - vx) == 0:
        print("v is the total differential")
    else:
        print("Maybe not the correct result")
        print("vx =", vx)
        print("dv/dx =", dvdx)
    print("v =", v)
    print("")
    fz = simplify(u+v*I)
    print("Analytic function: f(x, y) =",fz)
    
else:
    vx = uy
    vy = -ux
    print("ux =", vx)
    print("uy =", vy)
    print("vx =", ux)
    print("vy =", uy)
    print("")
    
    vxy = simplify(diff(vx, y))
    vyx = simplify(diff(vy, x))
    if simplify(vxy-vyx) == 0:
        print("path independent")
    else:
        print("maybe not path independent")
    v = simplify(integrate(integrate(vxy, y), x))
    dudx = diff(v, x)
    if simplify(dudx - vx) == 0:
        print("u is the total differential")
    else:
        print("Maybe not the correct result")
        print("ux =", vx)
        print("du/dx =", dudx)
    print("u =", v)
    print("")
    fz = simplify(v+u*I)
    print("Analytic function: f(x, y) =",fz)

 
#3D plots  
p3 = plot3d(u)
p4 = plot3d(v)

#Contour plots
Zreal = lambdify((x, y), u, 'numpy')
Zimaginary = lambdify((x, y), v, 'numpy')
X = np.arange(-5,5,0.4)
Y = np.arange(-5,5,0.4)
x, y = np.meshgrid(X, Y)

plt.contour(x, y, Zreal(x, y), 15, colors = "r")
plt.colorbar()
plt.contour(x, y, Zimaginary(x, y), 15, alpha = 0.85, colors = "b")
plt.colorbar()
plt.show()

#Real part if real = True
plt.contourf(x, y, Zreal(x, y), 50, alpha = 0.85, cmap = plt.cm.hot)
plt.colorbar()
plt.show()

#imaginary part if real = True
plt.contourf(x, y, Zimaginary(x, y), 50, alpha = 0.85, cmap = plt.cm.hot)
plt.colorbar()
plt.show() 

   
"""
Renqing Luo
"""

想看更漂亮的图片和以复数z表达的最简化的解析函数还是得去Wolfram|Alpha: Making the world’s knowledge computable

123690d55c010ffd0c8940c177f0ab32.png
直接把结果粘过去就行了Wolfram还是很智能的

也许你们已经发现了,我这里积分后并没有常数C。我知道,但是这常数不要也罢!

但是有些题目也是对初始条件敏感的(自己做最后一步也罢,python已经尽力了)

比如:

答案是

然而我把解析函数放到Wolfram一化简居然是

但这也不算错吧,它们的实部都是
。只不过当
答案才是
。。。

11775995dd32632bc100c2205813493e.png

这个程序嘛还是很垃圾的,比如一些极坐标的题会解出鬼畜。

0f4772471d01edde2976a2870d359551.png

以下傻瓜一样的极坐标转换代码(数学里用字符串替换可还行)

##笛卡尔坐标——>极坐标
cf = "sqrt(-x+sqrt(x**2+y**2))"
p = cf.replace("x**2+y**2", "R**2").replace("x", "R*cos(t)").replace("y", "R*sin(t)")
print(p)
##极坐标——>笛卡尔坐标
pf = "sqrt(R*cos(t) + sqrt(R**2)"
c = a.replace("R*cos(t)", "x").replace("R*sin(t)", "y").replace("R", "sqrt(x**2+y**2)")
print(c)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值