Calculate the solution of the initial-value problem (IVP). Solve the IVP from the Taylor (Order Four) method. Args: f (function): equation f(x). df1 (function): 1's derivative of equation f(x). df2 (function): 2's derivative of equation f(x). df3 (function):
(f, df1, df2, df3, a, b, n, ya)
| 85 | |
| 86 | |
| 87 | def taylor4(f, df1, df2, df3, a, b, n, ya): |
| 88 | """Calculate the solution of the initial-value problem (IVP). |
| 89 | |
| 90 | Solve the IVP from the Taylor (Order Four) method. |
| 91 | |
| 92 | Args: |
| 93 | f (function): equation f(x). |
| 94 | df1 (function): 1's derivative of equation f(x). |
| 95 | df2 (function): 2's derivative of equation f(x). |
| 96 | df3 (function): 3's derivative of equation f(x). |
| 97 | a (float): the initial point. |
| 98 | b (float): the final point. |
| 99 | n (int): number of intervals. |
| 100 | ya (numpy.ndarray): initial values. |
| 101 | |
| 102 | Returns: |
| 103 | vx (numpy.ndarray): x values. |
| 104 | vy (numpy.ndarray): y values (solution of IVP). |
| 105 | """ |
| 106 | vx = np.zeros(n) |
| 107 | vy = np.zeros(n) |
| 108 | |
| 109 | h = (b - a) / n |
| 110 | x = a |
| 111 | y = ya |
| 112 | |
| 113 | vx[0] = x |
| 114 | vy[0] = y |
| 115 | |
| 116 | print(f"i = 000,\tx = {x:+.4f},\ty = {y:+.4f}") |
| 117 | |
| 118 | for i in range(0, n): |
| 119 | y += h * (f(x, y) + 0.5 * h * df1(x, y) + (h ** 2 / 6) * df2(x, y) + |
| 120 | (h ** 3 / 24) * df3(x, y)) |
| 121 | x = a + (i + 1) * h |
| 122 | |
| 123 | print(f"i = {(i + 1):03d},\tx = {x:+.4f},\ty = {y:+.4f}") |
| 124 | vx[i] = x |
| 125 | vy[i] = y |
| 126 | |
| 127 | return vx, vy |
| 128 | |
| 129 | |
| 130 | def rk4(f, a, b, n, ya): |