简体   繁体   English

解析解与松弛法解的差异

[英]Discrepancy between analytic solution and solution by relaxation method

So I am trying to solve the differential equation $\frac{d^2y}{dx^2} = -y(x)$ subject to boundary conditions y(0) = 0 and y(1) = 1,the analytic solution is y(x) = sin(x)/sin(1).所以我试图求解微分方程 $\frac{d^2y}{dx^2} = -y(x)$ 受边界条件 y(0) = 0 和 y(1) = 1 的解析解是 y(x) = sin(x)/sin(1)。

I am using three point stencil to approximate the double derivative.我正在使用三点模板来近似二重导数。 The curves obtained through these ways should match at least at the boundaries,but my solutions have small differences even at the boundaries.通过这些方式得到的曲线应该至少在边界处匹配,但我的解决方案即使在边界处也有很小的差异。 I am attaching the code, Please tell me what is wrong.我附上代码,请告诉我哪里出了问题。

import numpy as np
import scipy.linalg as lg
from scipy.sparse.linalg import eigs
from scipy.sparse.linalg import inv
from scipy import sparse
import matplotlib.pyplot as plt

a = 0
b = 1
N = 1000
h = (b-a)/N
r = np.arange(a,b+h,h)

y_a = 0
y_b = 1

def lap_three(r):
    h = r[1]-r[0]
    n = len(r)
   
    
    M_d = -2*np.ones(n)
   #M_d = M_d + B_d
    O_d = np.ones(n-1)
    mat = sparse.diags([M_d,O_d,O_d],offsets=(0,+1,-1))
   #print(mat)
    return mat


def f(r):
    h = r[1]-r[0]
    n = len(r)
    return -1*np.ones(len(r))*(h**2)

def R_mat(f,r):
    r_d = f(r)
    R_mat = sparse.diags([r_d],offsets=[0])
   #print(R_mat)
    return R_mat

#def R_mat(r):
 #   M_d = -1*np.ones(len(r))
    
    
def make_mat(r):
    main = lap_three(r) - R_mat(f,r)
    return main

main = make_mat(r)
main_mat = main.toarray()
print(main_mat)
'''
eig_val , eig_vec = eigs(main, k = 20,which = 'SM')

#print(eig_val)
Val = eig_vec.T
plt.plot(r,Val[0])
'''
main_inv = inv(main)
inv_mat = main_inv.toarray()
#print(inv_mat)
#print(np.dot(main_mat,inv_mat))
n = len(r)
B_d = np.zeros(n)
B_d[0] = 0
B_d[-1] = 1
#print(B_d)
#from scipy.sparse.linalg import spsolve
A = np.abs(np.dot(inv_mat,B_d))
plt.plot(r[0:10],A[0:10],label='calculated solution')

real = np.sin(r)/np.sin(1)

plt.plot(r[0:10],real[0:10],label='analytic solution')
plt.legend()
#plt.plot(r,real)
#plt.plot(r,A)

'''diff = A-real
plt.plot(r,diff)'''

There is no guarantee of what the last point in arange(a,b+h,h) will be, it will mostly be b , but could in some cases also be b+h .无法保证arange(a,b+h,h)的最后一点是什么,它主要是b ,但在某些情况下也可能是b+h Better use更好用

r,h = np.linspace(a,b,N+1,retstep=True)

The linear system consists of the equations for the middle positions r[1],...,r[N-1] .线性系统由中间位置r[1],...,r[N-1]的方程组成。 These are N-1 equations, thus your matrix size is one too large.这些是N-1方程,因此您的矩阵大小太大了。

You could keep the matrix construction shorter by including the h^2 term already in M_d .您可以通过在M_d中包含h^2项来缩短矩阵构造。

If you use sparse matrices, you can also use the sparse solver A = spsolve(main, B_d) .如果您使用稀疏矩阵,您还可以使用稀疏求解器A = spsolve(main, B_d)

The equations that make up the system are组成系统的方程是

A[k-1] + (-2+h^2)*A[k] + A[k+1] = 0

The vector on the right side thus needs to contain the values -A[0] and -A[N] .因此,右侧的向量需要包含值-A[0]-A[N] This should clear up the sign problem, no need to cheat with the absolute value.这应该可以解决符号问题,不需要用绝对值作弊。

The solution vector A corresponds, as constructed from the start, to r[1:-1] .从一开始构造的解向量A对应于r[1:-1] As there are no values for postitions 0 and N inside, there can also be no difference.由于内部没有位置0N的值,因此也没有区别。


PS: There is no relaxation involved here, foremost because this is no iterative method. PS:这里不涉及松弛,主要是因为这不是迭代方法。 Perhaps you meant a finite difference method.也许你的意思是有限差分法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM