我正在使用python作为我的数值方法项目,此时我遇到的问题主要与我用python编码的方式有关 .

所以我在Python 2.7.5上编写了一个代码,用数字方法求解一个常微分方程(1D扩散方程 - 数据 - 恒定扩散系数= 1,第二个成员用于将sin(pi * x)作为解) .

基本上,数值方案将方程离散化,并且通过固定步骤 dx 分离1D点的离散化解 .

一个简单的理论研究表明,如果 dx 倾向于0,则diff方程的构造解与精确解之间的差异(称为 Err )变为0 .

运行我的代码时,我发现对于非常小的 dxErr 不会因机器精度错误(预期的行为)而停滞不前,但会增加几个数量级的订单 .

对于非常小的数字,它是Python的精确/圆形管理中的问题吗?

我尝试以不同的方式编写问题(矩阵,循环,矩阵反演技术),但同样的现象发生了 .

import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse as sp
from scipy.sparse.linalg.dsolve import spsolve

Err=[]

N=np.array([10,20,40,80,100,800,2000,5000,10000,50000,100000,600000,900000])
#Loop on the number of points used for the discretization of the 1D domain   

for Nx in N:

    dx=(1.0)/Nx# fixed distance between two consecutive points in the domain
    x = np.linspace(dx,1.0-dx,Nx-1)# points of the domains


    #Matrix contruction used for the resolution 
    data = [np.ones(Nx-1), -2*np.ones(Nx-1), np.ones(Nx-1)]# Diagonal terms
    offsets = np.array([-1, 0, 1])# Their positions
    LAP = sp.dia_matrix((data, offsets), shape=(Nx-1, Nx-1))#
    f = -np.pi**2*np.sin(np.pi*x)*dx**2#constant Second member 
    sol_num = spsolve(LAP,f)#Resolution of the matrix system 

    sol_ana=np.sin(np.pi*x)#The exact solution

    Err.append((np.absolute((sol_ana-sol_num))).max())

plt.figure()
plt.plot(N,Err,N,N**float(-2),'--')
plt.legend(['Err','Theoretical asymptotic behaviour'])
plt.xlabel('dx')
plt.ylabel('Err')
plt.xscale('log')
plt.yscale('log')
plt.show()