分析解决方案与解决方案之间的差异
因此,我正在尝试求解微分方程$ \ frac {d^2y} {dx^2} = -y(x)$受边界条件y(0)= 0和y(1)= 1的约束,分析解决方案是y(x)= sin(x)/sin(1)。
我正在使用三点模板来近似双衍生物。 通过这些方式获得的曲线至少应在边界上匹配,但是即使在边界处,我的解决方案也有很小的差异。 我正在附上代码,请告诉我有什么问题。
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)'''
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).
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)'''
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法保证
arange(a,b+h,h)
的最后一点,它主要是b
,但在某些情况下也可以是B+H
。更好地使用线性系统由中间位置的方程组成
r [1],...,r [n-1]
。这些是n-1
方程,因此您的矩阵大小太大了。您可以通过在
m_d
中包含H^2
术语来保持矩阵构造的短路。如果使用稀疏矩阵,也可以使用稀疏求解器
a = spsolve(main,b_d)
。因此,组成系统的方程式是
右侧的向量,因此需要包含值
-a [0]
和-a [n]
。这应该清除标志问题,无需用绝对值作弊。解决方案向量
a
从开始构建的,r [1:-1]
对应。由于邮政没有值0
和n
内部的值,因此也没有区别。PS:这里不涉及放松,因为这不是迭代方法。也许您的意思是有限的差异方法。
There is no guarantee of what the last point in
arange(a,b+h,h)
will be, it will mostly beb
, but could in some cases also beb+h
. Better useThe linear system consists of the equations for the middle positions
r[1],...,r[N-1]
. These areN-1
equations, thus your matrix size is one too large.You could keep the matrix construction shorter by including the
h^2
term already inM_d
.If you use sparse matrices, you can also use the sparse solver
A = spsolve(main, B_d)
.The equations that make up the system are
The vector on the right side thus needs to contain the values
-A[0]
and-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, tor[1:-1]
. As there are no values for postitions0
andN
inside, there can also be no difference.PS: There is no relaxation involved here, foremost because this is no iterative method. Perhaps you meant a finite difference method.