Python
import math

def runge_kutta_4(f, x0, y0, x_final, step_size):
    """
    Implements the Runge-Kutta 4th order method for solving ODEs.
    """
    x_vals = [x0 + i * step_size for i in range(int((x_final - x0) / step_size) + 1)]
    y = y0

    print("\nRunge-Kutta 4 Method")
    print("x\t\t y\t\t k1\t\t k2\t\t k3\t\t k4")
    
    for x in x_vals:
        k1 = f(x, y)
        k2 = f(x + step_size / 2, y + step_size * k1 / 2)
        k3 = f(x + step_size / 2, y + step_size * k2 / 2)
        k4 = f(x + step_size, y + step_size * k3)
        
        print(f"{x:.2f}\t {y:.4f}\t {k1:.4f}\t {k2:.4f}\t {k3:.4f}\t {k4:.4f}")
        
        y += (step_size / 6) * (k1 + 2 * k2 + 2 * k3 + k4)

# Define the differential equations as functions
def eq1(x, y): return 2*x - 3*y + 1
def eq2(x, y): return 4*x - 2*y
def eq3(x, y): return 1 - y**2
def eq4(x, y): return x**2 - y**2
def eq5(x, y): return math.exp(-y)
def eq6(x, y): return x - y**2
def eq7(x, y): return (x - y)**2
def eq8(x, y): return x*y + math.sqrt(y) if y >= 0 else x*y
def eq9(x, y): return x*y**2 - y
def eq10(x, y): return y - y**2

# Initial conditions and intervals for each problem
problems = [
    (eq1, 1, 5, 1.5),
    (eq2, 0, 2, 0.5),
    (eq3, 0, 0, 0.5),
    (eq4, 0, 1, 0.5),
    (eq5, 0, 0, 0.5),
    (eq6, 0, 0, 0.5),
    (eq7, 0, 0.5, 0.5),
    (eq8, 0, 1, 0.5),
    (eq9, 1, 1, 1.5),
    (eq10, 0, 0.5, 0.5)
]

# Solve for step sizes h = 0.1 and h = 0.05
for i, (func, x0, y0, x_final) in enumerate(problems, start=1):
    print(f"\nSolving Problem 9.1.{i} with step size h = 0.1")
    runge_kutta_4(func, x0, y0, x_final, 0.1)
    
    print(f"\nSolving Problem 9.1.{i} with step size h = 0.05")
    runge_kutta_4(func, x0, y0, x_final, 0.05)
Solving Problem 9.1.1 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
1.00	 5.0000	 -12.0000	 -10.1000	 -10.3850	 -8.6845
1.10	 3.9724	 -8.7173	 -7.3097	 -7.5208	 -6.2610
1.20	 3.2284	 -6.2853	 -5.2425	 -5.3989	 -4.4656
1.30	 2.6945	 -4.4836	 -3.7111	 -3.8270	 -3.1355
1.40	 2.3163	 -3.1489	 -2.5765	 -2.6624	 -2.1501
1.50	 2.0533	 -2.1600	 -1.7360	 -1.7996	 -1.4201

Solving Problem 9.1.1 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
1.00	 5.0000	 -12.0000	 -11.0500	 -11.1212	 -10.2318
1.05	 4.4452	 -10.2356	 -9.4180	 -9.4793	 -8.7137
1.10	 3.9723	 -8.7170	 -8.0133	 -8.0660	 -7.4071
1.15	 3.5700	 -7.4100	 -6.8042	 -6.8497	 -6.2825
1.20	 3.2283	 -6.2850	 -5.7636	 -5.8027	 -5.3146
1.25	 2.9389	 -5.3167	 -4.8679	 -4.9016	 -4.4814
1.30	 2.6944	 -4.4832	 -4.0970	 -4.1260	 -3.7643
1.35	 2.4886	 -3.7659	 -3.4335	 -3.4584	 -3.1471
1.40	 2.3162	 -3.1485	 -2.8623	 -2.8838	 -2.6159
1.45	 2.1724	 -2.6171	 -2.3708	 -2.3893	 -2.1587
1.50	 2.0532	 -2.1597	 -1.9477	 -1.9636	 -1.7651

Solving Problem 9.1.2 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 2.0000	 -4.0000	 -3.4000	 -3.4600	 -2.9080
0.10	 1.6562	 -2.9124	 -2.4212	 -2.4703	 -2.0183
0.20	 1.4110	 -2.0219	 -1.6198	 -1.6600	 -1.2900
0.30	 1.2465	 -1.2929	 -0.9636	 -0.9965	 -0.6936
0.40	 1.1480	 -0.6960	 -0.4264	 -0.4534	 -0.2053
0.50	 1.1037	 -0.2073	 0.0134	 -0.0087	 0.1944

Solving Problem 9.1.2 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 2.0000	 -4.0000	 -3.7000	 -3.7150	 -3.4285
0.05	 1.8145	 -3.4290	 -3.1576	 -3.1711	 -2.9119
0.10	 1.6562	 -2.9124	 -2.6668	 -2.6790	 -2.4445
0.15	 1.5225	 -2.4449	 -2.2227	 -2.2338	 -2.0215
0.20	 1.4110	 -2.0219	 -1.8208	 -1.8309	 -1.6388
0.25	 1.3196	 -1.6392	 -1.4572	 -1.4663	 -1.2926
0.30	 1.2464	 -1.2929	 -1.1282	 -1.1365	 -0.9792
0.35	 1.1898	 -0.9795	 -0.8305	 -0.8380	 -0.6957
0.40	 1.1480	 -0.6960	 -0.5612	 -0.5679	 -0.4392
0.45	 1.1197	 -0.4394	 -0.3174	 -0.3235	 -0.2071
0.50	 1.1036	 -0.2073	 -0.0969	 -0.1024	 0.0030

Solving Problem 9.1.3 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.0000	 1.0000	 0.9975	 0.9975	 0.9900
0.10	 0.0997	 0.9901	 0.9777	 0.9779	 0.9610
0.20	 0.1974	 0.9610	 0.9398	 0.9403	 0.9151
0.30	 0.2913	 0.9151	 0.8864	 0.8874	 0.8556
0.40	 0.3799	 0.8556	 0.8213	 0.8227	 0.7863
0.50	 0.4621	 0.7864	 0.7486	 0.7505	 0.7115

Solving Problem 9.1.3 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.0000	 1.0000	 0.9994	 0.9994	 0.9975
0.05	 0.0500	 0.9975	 0.9944	 0.9944	 0.9901
0.10	 0.0997	 0.9901	 0.9845	 0.9846	 0.9778
0.15	 0.1489	 0.9778	 0.9700	 0.9700	 0.9610
0.20	 0.1974	 0.9610	 0.9510	 0.9511	 0.9400
0.25	 0.2449	 0.9400	 0.9280	 0.9281	 0.9151
0.30	 0.2913	 0.9151	 0.9013	 0.9015	 0.8868
0.35	 0.3364	 0.8869	 0.8714	 0.8717	 0.8556
0.40	 0.3799	 0.8556	 0.8389	 0.8393	 0.8220
0.45	 0.4219	 0.8220	 0.8042	 0.8046	 0.7864
0.50	 0.4621	 0.7864	 0.7679	 0.7683	 0.7495

Solving Problem 9.1.4 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 1.0000	 -1.0000	 -0.9000	 -0.9095	 -0.8164
0.10	 0.9094	 -0.8170	 -0.7319	 -0.7393	 -0.6580
0.20	 0.8358	 -0.6585	 -0.5821	 -0.5882	 -0.5137
0.30	 0.7772	 -0.5141	 -0.4423	 -0.4477	 -0.3765
0.40	 0.7327	 -0.3769	 -0.3071	 -0.3121	 -0.2421
0.50	 0.7018	 -0.2425	 -0.1731	 -0.1779	 -0.1078

Solving Problem 9.1.4 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 1.0000	 -1.0000	 -0.9500	 -0.9524	 -0.9045
0.05	 0.9524	 -0.9046	 -0.8589	 -0.8610	 -0.8170
0.10	 0.9094	 -0.8170	 -0.7747	 -0.7766	 -0.7354
0.15	 0.8706	 -0.7355	 -0.6957	 -0.6974	 -0.6585
0.20	 0.8358	 -0.6585	 -0.6207	 -0.6222	 -0.5850
0.25	 0.8047	 -0.5851	 -0.5486	 -0.5500	 -0.5140
0.30	 0.7772	 -0.5141	 -0.4787	 -0.4800	 -0.4449
0.35	 0.7533	 -0.4449	 -0.4102	 -0.4114	 -0.3768
0.40	 0.7327	 -0.3769	 -0.3425	 -0.3438	 -0.3095
0.45	 0.7156	 -0.3095	 -0.2754	 -0.2766	 -0.2424
0.50	 0.7018	 -0.2425	 -0.2084	 -0.2096	 -0.1754

Solving Problem 9.1.5 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.0000	 1.0000	 0.9512	 0.9536	 0.9090
0.10	 0.0953	 0.9091	 0.8687	 0.8705	 0.8333
0.20	 0.1823	 0.8333	 0.7993	 0.8007	 0.7692
0.30	 0.2624	 0.7692	 0.7402	 0.7413	 0.7143
0.40	 0.3365	 0.7143	 0.6892	 0.6901	 0.6667
0.50	 0.4055	 0.6667	 0.6448	 0.6455	 0.6250

Solving Problem 9.1.5 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.0000	 1.0000	 0.9753	 0.9759	 0.9524
0.05	 0.0488	 0.9524	 0.9300	 0.9305	 0.9091
0.10	 0.0953	 0.9091	 0.8887	 0.8891	 0.8696
0.15	 0.1398	 0.8696	 0.8509	 0.8513	 0.8333
0.20	 0.1823	 0.8333	 0.8162	 0.8165	 0.8000
0.25	 0.2231	 0.8000	 0.7842	 0.7845	 0.7692
0.30	 0.2624	 0.7692	 0.7546	 0.7549	 0.7407
0.35	 0.3001	 0.7407	 0.7271	 0.7274	 0.7143
0.40	 0.3365	 0.7143	 0.7016	 0.7019	 0.6897
0.45	 0.3716	 0.6897	 0.6779	 0.6781	 0.6667
0.50	 0.4055	 0.6667	 0.6556	 0.6558	 0.6452

Solving Problem 9.1.6 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.0000	 0.0000	 0.0500	 0.0500	 0.1000
0.10	 0.0050	 0.1000	 0.1499	 0.1498	 0.1996
0.20	 0.0200	 0.1996	 0.2491	 0.2489	 0.2980
0.30	 0.0449	 0.2980	 0.3464	 0.3461	 0.3937
0.40	 0.0795	 0.3937	 0.4402	 0.4397	 0.4848
0.50	 0.1235	 0.4848	 0.5282	 0.5275	 0.5689

Solving Problem 9.1.6 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.0000	 0.0000	 0.0250	 0.0250	 0.0500
0.05	 0.0012	 0.0500	 0.0750	 0.0750	 0.1000
0.10	 0.0050	 0.1000	 0.1249	 0.1249	 0.1499
0.15	 0.0112	 0.1499	 0.1748	 0.1748	 0.1996
0.20	 0.0200	 0.1996	 0.2244	 0.2243	 0.2490
0.25	 0.0312	 0.2490	 0.2736	 0.2736	 0.2980
0.30	 0.0449	 0.2980	 0.3223	 0.3222	 0.3463
0.35	 0.0610	 0.3463	 0.3701	 0.3701	 0.3937
0.40	 0.0795	 0.3937	 0.4170	 0.4169	 0.4399
0.45	 0.1003	 0.4399	 0.4626	 0.4625	 0.4848
0.50	 0.1235	 0.4848	 0.5066	 0.5065	 0.5279

Solving Problem 9.1.7 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.5000	 0.2500	 0.2139	 0.2122	 0.1774
0.10	 0.5213	 0.1775	 0.1446	 0.1433	 0.1127
0.20	 0.5358	 0.1127	 0.0849	 0.0841	 0.0596
0.30	 0.5443	 0.0597	 0.0389	 0.0385	 0.0219
0.40	 0.5482	 0.0220	 0.0099	 0.0097	 0.0024
0.50	 0.5493	 0.0024	 0.0000	 0.0000	 0.0026

Solving Problem 9.1.7 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.5000	 0.2500	 0.2316	 0.2312	 0.2130
0.05	 0.5116	 0.2130	 0.1953	 0.1949	 0.1775
0.10	 0.5213	 0.1775	 0.1606	 0.1603	 0.1439
0.15	 0.5294	 0.1439	 0.1281	 0.1278	 0.1127
0.20	 0.5358	 0.1127	 0.0983	 0.0981	 0.0845
0.25	 0.5407	 0.0845	 0.0717	 0.0715	 0.0597
0.30	 0.5443	 0.0597	 0.0487	 0.0486	 0.0387
0.35	 0.5467	 0.0387	 0.0298	 0.0297	 0.0220
0.40	 0.5482	 0.0220	 0.0153	 0.0153	 0.0098
0.45	 0.5490	 0.0098	 0.0055	 0.0055	 0.0024
0.50	 0.5493	 0.0024	 0.0006	 0.0006	 0.0000

Solving Problem 9.1.8 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 1.0000	 1.0000	 1.0772	 1.0793	 1.1634
0.10	 1.1079	 1.1634	 1.2548	 1.2576	 1.3575
0.20	 1.2337	 1.3575	 1.4663	 1.4700	 1.5892
0.30	 1.3807	 1.5892	 1.7194	 1.7244	 1.8675
0.40	 1.5531	 1.8675	 2.0241	 2.0306	 2.2033
0.50	 1.7561	 2.2032	 2.3925	 2.4012	 2.6106

Solving Problem 9.1.8 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 1.0000	 1.0000	 1.0380	 1.0385	 1.0782
0.05	 1.0519	 1.0782	 1.1196	 1.1202	 1.1634
0.10	 1.1079	 1.1634	 1.2084	 1.2091	 1.2562
0.15	 1.1684	 1.2562	 1.3053	 1.3061	 1.3575
0.20	 1.2337	 1.3575	 1.4111	 1.4120	 1.4681
0.25	 1.3043	 1.4681	 1.5268	 1.5278	 1.5892
0.30	 1.3807	 1.5892	 1.6534	 1.6546	 1.7219
0.35	 1.4634	 1.7219	 1.7923	 1.7937	 1.8675
0.40	 1.5531	 1.8675	 1.9447	 1.9463	 2.0274
0.45	 1.6504	 2.0274	 2.1123	 2.1141	 2.2032
0.50	 1.7561	 2.2032	 2.2967	 2.2988	 2.3969

Solving Problem 9.1.9 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
1.00	 1.0000	 0.0000	 0.0500	 0.0528	 0.1064
1.10	 1.0052	 0.1063	 0.1638	 0.1676	 0.2313
1.20	 1.0219	 0.2312	 0.3015	 0.3071	 0.3877
1.30	 1.0525	 0.3875	 0.4791	 0.4878	 0.5966
1.40	 1.1011	 0.5963	 0.7236	 0.7382	 0.8958
1.50	 1.1747	 0.8952	 1.0855	 1.1121	 1.3598

Solving Problem 9.1.9 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
1.00	 1.0000	 0.0000	 0.0250	 0.0257	 0.0514
1.05	 1.0013	 0.0514	 0.0779	 0.0787	 0.1063
1.10	 1.0052	 0.1063	 0.1349	 0.1358	 0.1657
1.15	 1.0120	 0.1657	 0.1971	 0.1982	 0.2312
1.20	 1.0219	 0.2312	 0.2660	 0.2674	 0.3044
1.25	 1.0352	 0.3044	 0.3437	 0.3454	 0.3876
1.30	 1.0525	 0.3875	 0.4327	 0.4347	 0.4836
1.35	 1.0742	 0.4836	 0.5362	 0.5388	 0.5964
1.40	 1.1011	 0.5963	 0.6588	 0.6622	 0.7311
1.45	 1.1342	 0.7311	 0.8066	 0.8111	 0.8953
1.50	 1.1747	 0.8952	 0.9883	 0.9944	 1.0994

Solving Problem 9.1.10 with step size h = 0.1

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.5000	 0.2500	 0.2498	 0.2498	 0.2494
0.10	 0.5250	 0.2494	 0.2486	 0.2486	 0.2475
0.20	 0.5498	 0.2475	 0.2461	 0.2461	 0.2445
0.30	 0.5744	 0.2445	 0.2425	 0.2425	 0.2403
0.40	 0.5987	 0.2403	 0.2377	 0.2378	 0.2350
0.50	 0.6225	 0.2350	 0.2320	 0.2320	 0.2288

Solving Problem 9.1.10 with step size h = 0.05

Runge-Kutta 4 Method
x		 y		 k1		 k2		 k3		 k4
0.00	 0.5000	 0.2500	 0.2500	 0.2500	 0.2498
0.05	 0.5125	 0.2498	 0.2496	 0.2496	 0.2494
0.10	 0.5250	 0.2494	 0.2490	 0.2490	 0.2486
0.15	 0.5374	 0.2486	 0.2481	 0.2481	 0.2475
0.20	 0.5498	 0.2475	 0.2469	 0.2469	 0.2461
0.25	 0.5622	 0.2461	 0.2453	 0.2453	 0.2445
0.30	 0.5744	 0.2445	 0.2435	 0.2435	 0.2425
0.35	 0.5866	 0.2425	 0.2414	 0.2414	 0.2403
0.40	 0.5987	 0.2403	 0.2390	 0.2390	 0.2378
0.45	 0.6106	 0.2378	 0.2364	 0.2364	 0.2350
0.50	 0.6225	 0.2350	 0.2335	 0.2335	 0.2320