import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy.sparse as ss
from scipy.sparse.linalg import spsolve
class PDE2DModel:
#均应该传入一个一维二元数组,表示起止值
def __init__(self,x,y):
assert len(x)==len(y)==2,"ERROR:UNEXPECTED SV and EV!!"
self.x = x
self.y = y
#hx表示X上的步长
#hy表示Y上的步长
def space_grid(self,hx,hy):
M = int(round((self.x[1]-self.x[0])/hx,0))
N = int(round((self.y[1]-self.y[0])/hy,0))
assert M==N>=3,"至少网格数是合理的"
X = np.linspace(self.x[0],self.x[1],M+1)
Y = np.linspace(self.y[0],self.y[1],N+1)
return M,N,X,Y
def f(self,X,Y):
return 6*X*Y**3+6*X**3*Y+np.e**X*np.sin(Y)-np.e**X*np.sin(Y)
def solution(self,X,Y):
return np.e**X*np.sin(Y)-X**3*Y**3
#左边界
def left(self,Y):
return np.sin(Y)
#右边界
def right(self,Y):
return np.e**3*np.sin(Y)-27*Y**3
#上边界
def up(self,X):
return np.sin(1)*np.e**X-X**3
#下边界
def down(self,X):
return 0*X
#解算核心
def NDM5_2D(PDE2DModel,hx,hy):
M,N,X0,Y0 = PDE2DModel.space_grid(hx,hy)
Y,X = np.meshgrid(Y0,X0)
## print("X0",X0)
## print("Y0",Y0)
## #数值结果保存在U中 从0到N共N+1个
## print("M",M)
## print("N",N)
U = np.zeros((M+1,N+1))
U[0,:] = PDE2DModel.left(Y0)
U[-1,:] = PDE2DModel.right(Y0)
U[:,0] = PDE2DModel.down(X0)
U[:,-1] = PDE2DModel.up(X0)
D = np.diag([-1/(hy**2) for i in range(M-1)])
C = np.zeros((N-1,N-1),dtype="float64")
for i in range(N-1):
C[i][i] = 2*(1/hx**2+1/hy**2)
if i
很好,接下去接着讲
很好,结束了.
Y,X = np.meshgrid(Y0,X0)
import numpy as np
x = np.array([1,2,3,4,5,6,7,8,9,10])
y = np.array([11,12,13,14,15,16,17,18,19,20])
Y,X = np.meshgrid(y,x)
所以,优化之后!
import numpy as np
import scipy as sp
from scipy.sparse.linalg import spsolve
from scipy.sparse import csr_matrix,csc_matrix
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy.sparse as ss
from scipy.sparse.linalg import spsolve
import time
class PDE2DModel:
def __init__(self):
self.x = np.array([0,3])
self.y = np.array([0,1])
def space_grid(self,hx,hy):
M = int(round((self.x[1]-self.x[0])/hx,0))
N = int(round((self.y[1]-self.y[0])/hy,0))
assert M==N>=3,"ERROR:UNECPECTED GRIDS M:"+str(M)+" N:"+str(N)
X = np.linspace(self.x[0],self.x[1],M+1)
Y = np.linspace(self.y[0],self.y[1],N+1)
return M,N,X,Y
def f(self,X,Y):
return 6*X*Y**3+6*X**3*Y+np.e**X*np.sin(Y)-np.e**X*np.sin(Y)
#左边界
def left(self,Y):
return np.sin(Y)
#右边界
def right(self,Y):
return np.e**3*np.sin(Y)-27*Y**3
#上边界
def up(self,X):
return np.sin(1)*np.e**X-X**3
#下边界
def down(self,X):
return 0*X
def NDM5_2D(PDE2DModel,hx,hy):
M,N,X0,Y0 = PDE2DModel.space_grid(hx,hy)
Y,X = np.meshgrid(Y0,X0)
U = np.zeros((M+1,N+1))
U[0,:] = PDE2DModel.left(Y0)
U[-1,:] = PDE2DModel.right(Y0)
U[:,0] = PDE2DModel.down(X0)
U[:,-1] = PDE2DModel.up(X0)
D = np.diag([-1/(hy**2) for i in range(M-1)])
C = np.zeros((N-1,N-1),dtype="float64")
for i in range(N-1):
C[i][i] = 2*(1/hx**2+1/hy**2)
if i
f = lambda x:x+5
X = [1,2,3,4,5]
map(f,X)
import warnings
a = 0
if a==0:
warnings.warn("IGNORE!")
print(a+3)
Warning (from warnings module):
File "C:\Users\LX\Desktop\新建 文本文档.py", line 34
warnings.warn("IGNORE!")
UserWarning: IGNORE THIS WARNING!
3
##import matplotlib
##import matplotlib.pyplot as plt
##from mpl_toolkits.mplot3d import Axes3D
##matplotlib.rcParams["font.sans-serif"] = ["SimHei"]
##matplotlib.rcParams["axes.unicode_minus"] = False
##
##import numpy as np
##
##x = np.linspace(1,2,6)
##y = np.linspace(3,9,13)
##Y,X = np.meshgrid(y,x)
##X2,Y2 = np.meshgrid(x,y)
##
##plt.figure(figsize=(15,5))
##ax1 = plt.subplot(121,projection="3d")
##ax2 = plt.subplot(122,projection="3d")
##
##ax1.set_title("Inverse order")
##ax2.set_title("Ordinary order")
##ax1.set_xlabel("x")
##ax2.set_xlabel("x")
##
##f = lambda x,y:x*2+y*3
##ax1.plot_surface(X,Y,f(X,Y))
##
##ax2.plot_surface(X2,Y2,f(X2,Y2))
##
##plt.show()