Runge-Kutta Method
1. 𝑦 ′ = −𝑦 + 1 − 𝑥 𝑦(0) = 3, 𝑥 = 0 𝑡𝑜 1, ℎ = 0.1
Results from Midpoint, Ralston's, and Heun's Methods:
x midpoint ralston heun
___ __________ _________ ______
0 3 3 3
0.1 2.805 2.805 2.805
0.2 2.619 2.619 2.619
0.3 2.4412 2.4412 2.4412
0.4 2.2708 2.2708 2.2708
0.5 2.1071 2.1071 2.1071
0.6 1.9494 1.9494 1.9494
0.7 1.7972 1.7972 1.7972
0.8 1.65 1.65 1.65
0.9 1.5072 1.5072 1.5072
1 1.3685 1.3685 1.3685
𝑑𝑦
+𝑦 = 1−𝑥 P(x) = 1, Q(x) = 1 − x
𝑑𝑡
𝑖𝑓 = 𝑒 ∫ 𝑑𝑥 = 𝑒𝑥 Exact midpoint ralston heun
𝐶 ___ __________ _________ ______
𝑦 = −𝑥 + 2 + 𝑥 𝑥=1
𝑒
𝑦(0) = 3 1
𝑦(1) = −1 + 2 + 1.3679 1.3685 1.3685 1.3685
𝑒1
3 = −(0) + 2 + 𝐶(𝑒 −(0) )
𝐶=1 𝑦(1) = 𝟏. 𝟑𝟔𝟕𝟗
𝑑𝑦
2. 𝑑𝑥
=𝑥+𝑦−1 𝑦(0) = 2, 𝑥 = 1, ℎ = 0.1
Results from Midpoint, Ralston's, and Heun's Methods:
x midpoint ralston heun
___ __________ _________ ______
0 2 2 2
0.1 2.11 2.11 2.11
0.2 2.242 2.242 2.242
0.3 2.3985 2.3985 2.3985
0.4 2.5818 2.5818 2.5818
0.5 2.7949 2.7949 2.7949
0.6 3.0409 3.0409 3.0409
0.7 3.3231 3.3231 3.3231
0.8 3.6456 3.6456 3.6456
0.9 4.0124 4.0124 4.0124
1 4.4282 4.4282 4.4282
Heun’s = 4.4282
𝑑𝑦
3. = 𝑡√𝑦 𝑦(0) = 2, 𝑡 = 1, ℎ = 0.1
𝑑𝑡
Results from Midpoint, Ralston's, and Heun's Methods:
x midpoint ralston heun
___ ________ _______ ______
0 2 2 2
0.1 2.0071 2.0071 2.0071
0.2 2.0284 2.0284 2.0284
0.3 2.0641 2.0641 2.0641
0.4 2.1146 2.1147 2.1147
Runge-Kutta Method
0.5 2.1805 2.1806 2.1806
0.6 2.2624 2.2625 2.2626
0.7 2.3612 2.3613 2.3614
0.8 2.4777 2.4779 2.4781
0.9 2.6132 2.6134 2.6136
1 2.7689 2.7692 2.7694
Midpoint = 2.7689
𝑑𝑦
4. = 4𝑒0.8𝑥 − 0.5𝑦 𝑦(0) = 2, 𝑥 = 1, ℎ = 0.1
𝑑𝑥
Results from Midpoint, Ralston's, and Heun's Methods:
x midpoint ralston heun
___ ________ _______ ______
0 2 2 2
0.1 2.3088 2.309 2.3092
0.2 2.6364 2.6368 2.6371
0.3 2.9847 2.9852 2.9858
0.4 3.3558 3.3565 3.3572
0.5 3.7517 3.7526 3.7535
0.6 4.175 4.1761 4.1772
0.7 4.6281 4.6294 4.6308
0.8 5.1138 5.1154 5.1169
0.9 5.6351 5.6369 5.6387
1 6.1952 6.1972 6.1993
Ralston’s = 6.1993
𝑑𝑦
5. = −2𝑥3 + 12𝑥2 − 20𝑥 + 8.5 𝑦(0) = 5, 𝑥 = 1, ℎ = 0.1
𝑑𝑥
Results from Midpoint, Ralston's, and Heun's Methods:
x midpoint ralston heun
___ ________ _______ ______
0 5 5 5
0.1 5.753 5.7544 5.7559
0.2 6.3293 6.3322 6.335
0.3 6.7512 6.7554 6.7595
0.4 7.0396 7.045 7.0504
0.5 7.2144 7.221 7.2275
0.6 7.2941 7.3018 7.3094
0.7 7.2962 7.3049 7.3135
0.8 7.2368 7.2465 7.256
0.9 7.131 7.1415 7.1519
1 6.9925 7.0038 7.015
Runge-Kutta Method
2nd Order Runge-Kutta
order = input('Enter the order of the ODE (1, 2, or 3): ');
% Input the function for the ODE
if order == 1
f_str = input('Enter the function f(x, y) for the 1st order ODE (e.g., "y - x^2"): ',
's');
f = str2func(['@(x, y) ' f_str]); % Convert string to function handle
elseif order == 2
f_str = input('Enter the function f(x, y, y1) for the 2nd order ODE (e.g., "y1 - y -
x^2"): ', 's');
f = str2func(['@(x, y, y1) ' f_str]); % Convert string to function handle
elseif order == 3
f_str = input('Enter the function f(x, y, y1, y2) for the 3rd order ODE (e.g., "y2 - y1 -
x^2"): ', 's');
f = str2func(['@(x, y, y1, y2) ' f_str]); % Convert string to function handle
else
error('Currently only 1st, 2nd, and 3rd order ODEs are supported.');
end
% Input initial conditions
x0 = input('Enter the initial value of x (x0): ');
y0 = input('Enter the initial value of y (y0): ');
if order >= 2
y1_0 = input('Enter the initial value of y'' (y1_0): ');
end
if order == 3
y2_0 = input('Enter the initial value of y'''' (y2_0): ');
end
x_end = input('Enter the final value of x (x_end): ');
h = input('Enter the step size (h): ');
% Create the system of first-order ODEs
if order == 1
system_eq = @(x, y) f(x, y); % For 1st order, just return the function
elseif order == 2
system_eq = @(x, y) [y(2); f(x, y(1), y(2))]; % For 2nd order, return the system
elseif order == 3
system_eq = @(x, y) [y(2); y(3); f(x, y(1), y(2), y(3))]; % For 3rd order, return the
system
end
% Solve the system of equations using each method
if order == 1
[x_mid, y_mid] = midpoint_method(system_eq, x0, y0, x_end, h);
[x_ral, y_ral] = ralston_method(system_eq, x0, y0, x_end, h);
[x_heun, y_heun] = heuns_method(system_eq, x0, y0, x_end, h);
else
% For 2nd and 3rd order ODEs, the initial condition is a vector
if order == 2
initial_conditions = [y0; y1_0];
Runge-Kutta Method
elseif order == 3
initial_conditions = [y0; y1_0; y2_0];
end
[x_mid, z_mid] = midpoint_method(system_eq, x0, initial_conditions, x_end, h);
[x_ral, z_ral] = ralston_method(system_eq, x0, initial_conditions, x_end, h);
[x_heun, z_heun] = heuns_method(system_eq, x0, initial_conditions, x_end, h);
% Extract solutions for y from the system (first row is y)
y_mid = z_mid(1, :);
y_ral = z_ral(1, :);
y_heun = z_heun(1, :);
end
x_vals = linspace(x0, x_end, max([length(y_mid), length(y_ral), length(y_heun)]));
y_mid_interp = interp1(x_mid, y_mid, x_vals, 'linear', 'extrap');
y_ral_interp = interp1(x_ral, y_ral, x_vals, 'linear', 'extrap');
y_heun_interp = interp1(x_heun, y_heun, x_vals, 'linear', 'extrap');
% table
results_table = table(x_vals', y_mid_interp', y_ral_interp', y_heun_interp', ...
'VariableNames', {'x', 'midpoint', 'ralston', 'heun'});
% Display the table
disp('Results from Midpoint, Ralston''s, and Heun''s Methods:');
disp(results_table);
% Plot the results
figure;
hold on;
plot(x_vals, y_mid_interp, '-o', 'DisplayName', 'Midpoint Method', 'LineWidth', 2);
plot(x_vals, y_ral_interp, '-x', 'DisplayName', 'Ralston''s Method', 'LineWidth', 2);
plot(x_vals, y_heun_interp, '-s', 'DisplayName', 'Heun''s Method', 'LineWidth', 2);
hold off;
% Customize the plot
xlabel('x');
ylabel('y');
title('Comparison of Midpoint, Ralston''s, and Heun''s Methods for ODE');
legend('show');
grid on;
% Midpoint Method
function [x_vals, y_vals] = midpoint_method(f, x0, y0, x_end, h)
x_vals = x0:h:x_end;
y_vals = zeros(1, length(x_vals)); % Create storage for the solution
y_vals(1) = y0;
for i = 1:length(x_vals)-1
k1 = h * f(x_vals(i), y_vals(i));
k2 = h * f(x_vals(i) + 0.5 * h, y_vals(i) + 0.5 * k1);
y_vals(i+1) = y_vals(i) + k2;
end
end
% Ralston's Method
function [x_vals, y_vals] = ralston_method(f, x0, y0, x_end, h)
Runge-Kutta Method
x_vals = x0:h:x_end;
y_vals = zeros(1, length(x_vals)); % Create storage for the solution
y_vals(1) = y0;
for i = 1:length(x_vals)-1
k1 = h * f(x_vals(i), y_vals(i));
k2 = h * f(x_vals(i) + 0.75 * h, y_vals(i) + 0.75 * k1);
y_vals(i+1) = y_vals(i) + (1/3) * k1 + (2/3) * k2;
end
end
% Heun's Method
function [x_vals, y_vals] = heuns_method(f, x0, y0, x_end, h)
x_vals = x0:h:x_end;
y_vals = zeros(1, length(x_vals)); % Create storage for the solution
y_vals(1) = y0;
for i = 1:length(x_vals)-1
k1 = h * f(x_vals(i), y_vals(i));
k2 = h * f(x_vals(i+1), y_vals(i) + k1);
y_vals(i+1) = y_vals(i) + 0.5 * (k1 + k2);
end
end