Calculate the solution of the initial-value problem (IVP). Solve the IVP from the Taylor (Order Two) method. Args: f (function): equation f(x). df1 (function): 1's derivative of equation f(x). a (float): the initial point. b (float): the final point.
(f, df1, a, b, n, ya)
| 45 | |
| 46 | |
| 47 | def taylor2(f, df1, a, b, n, ya): |
| 48 | """Calculate the solution of the initial-value problem (IVP). |
| 49 | |
| 50 | Solve the IVP from the Taylor (Order Two) method. |
| 51 | |
| 52 | Args: |
| 53 | f (function): equation f(x). |
| 54 | df1 (function): 1's derivative of equation f(x). |
| 55 | a (float): the initial point. |
| 56 | b (float): the final point. |
| 57 | n (int): number of intervals. |
| 58 | ya (numpy.ndarray): initial values. |
| 59 | |
| 60 | Returns: |
| 61 | vx (numpy.ndarray): x values. |
| 62 | vy (numpy.ndarray): y values (solution of IVP). |
| 63 | """ |
| 64 | vx = np.zeros(n) |
| 65 | vy = np.zeros(n) |
| 66 | |
| 67 | h = (b - a) / n |
| 68 | x = a |
| 69 | y = ya |
| 70 | |
| 71 | vx[0] = x |
| 72 | vy[0] = y |
| 73 | |
| 74 | print(f"i = 000,\tx = {x:+.4f},\ty = {y:+.4f}") |
| 75 | |
| 76 | for i in range(0, n): |
| 77 | y += h * (f(x, y) + 0.5 * h * df1(x, y)) |
| 78 | x = a + (i + 1) * h |
| 79 | |
| 80 | print(f"i = {(i + 1):03d},\tx = {x:+.4f},\ty = {y:+.4f}") |
| 81 | vx[i] = x |
| 82 | vy[i] = y |
| 83 | |
| 84 | return vx, vy |
| 85 | |
| 86 | |
| 87 | def taylor4(f, df1, df2, df3, a, b, n, ya): |