# Euler

Oct 24th, 2021
695
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def method_Euler():
2.     flag = 0
3.     N = 10
4.     help_err = 0
5.
6.     plt.xlabel('x')
7.     plt.ylabel('y')
8.
9.
10.     while True:
11.         step = (B - A) / (N - 1)
12.         u_help = np.empty(N)
13.         arr_err = np.empty(N)
14.
15.         u_help[0] = 0
16.         arr_err[0] = 0
17.
18.         error = 0
19.
20.         for i in range(1, N):
21.             u_help[i] = u_help[i - 1] + step * g(step * (i - 1), u_help[i - 1])
22.             arr_err[i] = np.fabs(u_help[i] - phi_3(i * step))
23.
24.             if error < arr_err[i]:
25.                 error = np.fabs(u_help[i] - phi_3(i * step))
26.
27.
28.         if error < eps:
29.             flag = 1
30.
31.         print(error, end = '\t')
32.         if help_err != 0:
33.             print("p = ", np.log2(help_err / error))
34.         help_err = error
35.
36.
37.         a = np.linspace(A, B, 160)
38.         b = phi_3(a)
39.         plt.grid()
40.         plt.plot(a, b, 'r')
41.
42.         c = np.linspace(A, B, N)
43.         plt.plot(c, arr_err, 'g')
44.         plt.plot(c, u_help, 'b')
45.         plt.pause(5)
46.
47.         if flag == 1:
48.             break
49.         N = 2 * N
50.
51.     plt.show()
52.
53.
54.
55.     return u_help
RAW Paste Data