Good day everyone! I solve the boundary value problem on python by the finite difference method, that is, by replacing the derivatives. Initial task:

mu * y '' + A (x) * y'-c (x) * y = f (x)

in the interval [0,1]

I replace the second and first derivatives and make up the system of equations that I solve using the sweep method.

I rechecked all the coefficients, but for some reason the error is too big. Do not tell me what am I wrong?

import math import matplotlib.pyplot as plt import numpy as np def f(x): return -2*math.exp(x) def A(x): return np.cos(x) def c(x): return x**2 def y(x): return x*math.exp(x) a = 0 b = 1 N = 10 mu=1 h = (ba) / (N - 1) ya = y(a) yb = y(b) x = np.zeros(N) for i in range(N): x[i] = a + i*h #Коэфиценты левой и правой частей уравнения k = np.zeros((N-2,4)) k[0][0] = 0 k[0][1] = -c(x[1]) - 2*mu / math.pow(h, 2) k[0][2] = mu / math.pow(h, 2)+A(x[1])/(2*h) k[0][3] = f(a + h) - ya*(mu/ math.pow(h, 2) - A(x[1]) / (2*h)) for i in range(1, N-3): k[i][0] = mu / math.pow(h, 2)-A(x[i+1])/(2*h) k[i][1] = -c(x[i+1]) - (2*mu / math.pow(h, 2)) k[i][2] = mu / math.pow(h, 2)+A(x[i+1])/(2*h) k[i][3] = f(a + (i+1) * h) k[N - 3][0] = mu / math.pow(h, 2) - A(x[N-2])/(2*h) k[N - 3][1] = -c(x[N-2]) - (2 *mu/ math.pow(h, 2)) k[N - 3][2] = 0 k[N - 3][3] = f(a + (N - 2)*h) - yb*(mu/math.pow(h,2)+A(x[N-2]) / (2*h)) #Метод прогонки al = np.zeros(N-3) bet = np.zeros(N-3) al[N - 4] = -k[N - 3][0] / k[N - 3][1]; bet[N - 4] = k[N - 3][3] / k[N - 3][1]; for i in range(N - 4, 0, -1): al[i - 1] = -k[i][0] / (k[i][1] + k[i][2] * al[i]); bet[i - 1] = (k[i][3] - k[i][2] * bet[i]) / (k[i][1] + k[i][2] * al[i]); u = np.zeros(N) x = np.zeros(N) u[0] = ya u[1] = (k[0][3] - k[0][2] * bet[0]) / (k[0][1] + k[0][2] * al[0]) u[N-1] = yb for i in range(N-3): u[i+2] = al[i]*u[i+1]+bet[i] for i in range(N): x[i] = a + i*h rx = np.zeros(N*10) ry = np.zeros(N*10) rh = (ba)/(N*10-1) for i in range(N*10): rx[i] = a + i*rh ry[i] = y(rx[i]) fig = plt.figure() fig.set_size_inches(18.5, 10.5) plt.plot(rx, ry,label='Точное') plt.plot(x, u,label='Метод прогонки') plt.legend(loc='best') plt.grid(True) plt.show() 
  • The function y(x) = x*exp(x) not an exact solution for the equation y''+cos(x)y'-x^2*y =-2*exp(x) , which you solve. Error somewhere in the problem statement. - Chorkov

0