Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import matplotlib.pyplot as plt
- import numpy as np
- import numpy
- def realsolution(x):
- return x*(math.sin(x))+(math.cos(x))
- # Первый класс точности
- z1 = []
- z2 = []
- y = []
- x = []
- i = 0
- a = 0.0
- b = 1.0
- h = 0.05
- n = (b-a)/h
- k = 0.0
- z1.append(1.0)
- z2.append(-1.0)
- y.append(1.3818)
- x.append(0.0)
- while i < n:
- z1.append(z1[i]+h*(-(z1[i])**2-z1[i]*(math.tan(k))-k))
- z2.append(z2[i]+h*(-z2[i]*(z1[i]+math.tan(k))+(1+k)*(math.cos(k))+(k**2)*(math.sin(k))))
- i+=1
- k+=h
- x.append(k)
- x = np.array(x)
- i = 0
- while i < len(z1)-1:
- y.append(y[i]-h*(z1[len(z1)-1-i]*y[i]+z2[len(z1)-1-i]))
- i+=1
- y.reverse()
- print('Решение численным методом прогонки: ',y)
- # Второй класс точности
- r1 = []
- r2 = []
- u = []
- i = 0
- k = 0.0
- r1.append(0.98)
- r2.append(-1.03)
- u.append(1.3818)
- while i < n:
- r1.append(r1[i]+h*(-(r1[i])**2-r1[i]*(math.tan(k))-k))
- r2.append(r2[i]+h*(-r2[i]*(r1[i]+math.tan(k))+(1+k)*(math.cos(k))+(k**2)*(math.sin(k))))
- i+=1
- k+=h
- i = 0
- while i < len(r1)-1:
- u.append(u[i]-h*(r1[len(r1)-1-i]*u[i]+r2[len(r1)-1-i]))
- i+=1
- u.reverse()
- print('Решение численным методом прогонки второго класса точности: ',u)
- # Реальное решение
- i = 0
- u0 = []
- for j in x:
- u0.append(realsolution(j))
- print('Ответ к задаче: ',u0)
- plt.subplot(111)
- plt.xlabel(r'$x$')
- plt.ylabel(r'$u(x)$')
- plt.title("Boundary problem")
- plt.plot(x, u0, "b",label = 'Real solution')
- plt.plot(x, y, "--h", label = 'Numerical solution first precision')
- plt.plot(x,u,"g",label = 'Numerical solution second precision')
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement