Calculate the solution of the initial-value problem (IVP). Solve the IVP from the Euler method. Args: f (function): equation f(x). a (float): the initial point. b (float): the final point. n (int): number of intervals. ya (numpy.ndarray): initial val
(f, a, b, n, ya)
| 4 | |
| 5 | |
| 6 | def euler(f, a, b, n, ya): |
| 7 | """Calculate the solution of the initial-value problem (IVP). |
| 8 | |
| 9 | Solve the IVP from the Euler method. |
| 10 | |
| 11 | Args: |
| 12 | f (function): equation f(x). |
| 13 | a (float): the initial point. |
| 14 | b (float): the final point. |
| 15 | n (int): number of intervals. |
| 16 | ya (numpy.ndarray): initial values. |
| 17 | |
| 18 | Returns: |
| 19 | vx (numpy.ndarray): x values. |
| 20 | vy (numpy.ndarray): y values (solution of IVP). |
| 21 | """ |
| 22 | vx = np.zeros(n) |
| 23 | vy = np.zeros(n) |
| 24 | |
| 25 | h = (b - a) / n |
| 26 | x = a |
| 27 | y = ya |
| 28 | |
| 29 | vx[0] = x |
| 30 | vy[0] = y |
| 31 | |
| 32 | fxy = f(x, y) |
| 33 | print(f"i = 000,\tx = {x:+.4f},\ty = {y:+.4f}") |
| 34 | |
| 35 | for i in range(0, n): |
| 36 | x = a + (i + 1) * h |
| 37 | y += h * fxy |
| 38 | |
| 39 | fxy = f(x, y) |
| 40 | print(f"i = {(i+1):03d},\tx = {x:+.4f},\ty = {y:+.4f}") |
| 41 | vx[i] = x |
| 42 | vy[i] = y |
| 43 | |
| 44 | return vx, vy |
| 45 | |
| 46 | |
| 47 | def taylor2(f, df1, a, b, n, ya): |