0% found this document useful (0 votes)
29 views2 pages

rk4 Simul Eqns - Py

The document contains a Python script implementing the RK4 method for solving a system of ordinary differential equations (ODEs). It defines two functions representing the ODEs, initializes parameters, and iteratively computes the solutions while also plotting the results against exact solutions. The script is authored by Bikalpa Bhattarai and includes visualization of the results using Matplotlib.

Uploaded by

bikalpab30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

rk4 Simul Eqns - Py

The document contains a Python script implementing the RK4 method for solving a system of ordinary differential equations (ODEs). It defines two functions representing the ODEs, initializes parameters, and iteratively computes the solutions while also plotting the results against exact solutions. The script is authored by Bikalpa Bhattarai and includes visualization of the results using Matplotlib.

Uploaded by

bikalpab30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

6/30/25, 6:22 PM rk4_simul_eqns.

py

~\Desktop\lab\rk4_simul_eqns.py

1 # RK4 Method for System of ODEs


2 # Name: Bikalpa Bhattarai
3 # Roll No: 080BEL023
4
5 import numpy as np
6 import matplotlib.pyplot as plt
7
8 def f1(x, y, z):
9 return 2*y + z
10
11 def f2(x, y, z):
12 return y - 3*z
13
14 x0 = 0
15 y0 = 0
16 z0 = 0.5
17 h = 0.1
18 n = 20
19
20 x_vals = [x0]
21 y_vals = [y0]
22 z_vals = [z0]
23
24 for _ in range(n):
25 x = x_vals[-1]
26 y = y_vals[-1]
27 z = z_vals[-1]
28
29 k1 = h * f1(x, y, z)
30 l1 = h * f2(x, y, z)
31
32 k2 = h * f1(x + h/2, y + k1/2, z + l1/2)
33 l2 = h * f2(x + h/2, y + k1/2, z + l1/2)
34
35 k3 = h * f1(x + h/2, y + k2/2, z + l2/2)
36 l3 = h * f2(x + h/2, y + k2/2, z + l2/2)
37
38 k4 = h * f1(x + h, y + k3, z + l3)
39 l4 = h * f2(x + h, y + k3, z + l3)
40
41 y_next = y + (k1 + 2*k2 + 2*k3 + k4) / 6
42 z_next = z + (l1 + 2*l2 + 2*l3 + l4) / 6

localhost:52370/3827afa9-39bd-4083-a1e2-e1cf61905253/ 1/2
6/30/25, 6:22 PM rk4_simul_eqns.py

43
44 x_vals.append(x + h)
45 y_vals.append(y_next)
46 z_vals.append(z_next)
47
48 print("x\t\ty\t\tz")
49 for i in range(len(x_vals)):
50 print(f"{x_vals[i]:.4f}\t{y_vals[i]:.4f}\t{z_vals[i]:.4f}")
51
52 def y_exact(x):
53 return 0.1 * (np.exp(2*x) - np.exp(-3*x))
54
55 def z_exact(x):
56 return 0.1 * (np.exp(2*x) + 4*np.exp(-3*x))
57
58 x_exact = np.linspace(x0, x0 + n*h, 500)
59 y_ex = y_exact(x_exact)
60 z_ex = z_exact(x_exact)
61
62 plt.figure(figsize=(12, 5))
63
64 plt.subplot(1, 2, 1)
65 plt.plot(x_vals, y_vals, 'bo-', label='RK4 y')
66 plt.plot(x_exact, y_ex, 'r--', label='Exact y')
67 plt.xlabel('x')
68 plt.ylabel('y')
69 plt.title('x vs y')
70 plt.legend()
71 plt.grid(True)
72
73 plt.subplot(1, 2, 2)
74 plt.plot(x_vals, z_vals, 'go-', label='RK4 z')
75 plt.plot(x_exact, z_ex, 'm--', label='Exact z')
76 plt.xlabel('x')
77 plt.ylabel('z')
78 plt.title('x vs z')
79 plt.legend()
80 plt.grid(True)
81
82 plt.suptitle('RK4 Solution vs Exact Solution\nBikalpa Bhattarai |
080BEL023')
83 plt.tight_layout()
84 plt.show()

localhost:52370/3827afa9-39bd-4083-a1e2-e1cf61905253/ 2/2

You might also like