0% found this document useful (0 votes)
17 views4 pages

RK Plot123 4 Ball

The document contains MATLAB functions implementing the Runge-Kutta 4th order method (RK4) for solving ordinary differential equations with varying step sizes (h = 0.5, 0.2, and 0.05). Each function computes the results over a specified number of iterations, stores the results in arrays, and plots them. Additionally, there is a separate function that uses a different differential equation for a larger step size (h = 240).

Uploaded by

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

RK Plot123 4 Ball

The document contains MATLAB functions implementing the Runge-Kutta 4th order method (RK4) for solving ordinary differential equations with varying step sizes (h = 0.5, 0.2, and 0.05). Each function computes the results over a specified number of iterations, stores the results in arrays, and plots them. Additionally, there is a separate function that uses a different differential equation for a larger step size (h = 240).

Uploaded by

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

% RK_4.

m
function RK_4()
h = 0.5;
t = 0;
w = 0.5;

% Preallocate arrays for storing results


T = t;
W = w;

for i = 1:4
k1 = h * f(t, w);
k2 = h * f(t + h/2, w + k1/2);
k3 = h * f(t + h/2, w + k2/2);
k4 = h * f(t + h, w + k3);
w = w + (k1 + 2*k2 + 2*k3 + k4) / 6;
t = t + h;

% Store results
T(end+1) = t;
W(end+1) = w;
end

% Plot results
plot(T, W, 'b-', 'DisplayName', 'h = 0.2');
hold on;
end

function v = f(t, y)
v = y - t^2 + 1;
end

% RK_4_2.m
function RK_4_2()
h = 0.2;
t = 0;
w = 0.5;

% Preallocate arrays for storing results


T = t;
W = w;

for i = 1:10
k1 = h * f(t, w);
k2 = h * f(t + h/2, w + k1/2);
k3 = h * f(t + h/2, w + k2/2);
k4 = h * f(t + h, w + k3);
w = w + (k1 + 2*k2 + 2*k3 + k4) / 6;
t = t + h;

% Store results
T(end+1) = t;
W(end+1) = w;
end

% Plot results
plot(T, W, 'r-','DisplayName', 'h = 0.2');
end

function v = f(t, y)
v = y - t^2 + 1;
end

% RK_4_3.m
function RK_4_3()
h = 0.05;
t = 0;
w = 0.5;

% Preallocate arrays for storing results


T = t;
W = w;

for i = 1:40
k1 = h * f(t, w);
k2 = h * f(t + h/2, w + k1/2);
k3 = h * f(t + h/2, w + k2/2);
k4 = h * f(t + h, w + k3);
w = w + (k1 + 2*k2 + 2*k3 + k4) / 6;
t = t + h;

% Store results
T(end+1) = t;
W(end+1) = w;
end

% Plot results
plot(T, W, 'g-', 'DisplayName', 'h = 0.05');
end

function v = f(t, y)
v = y - t^2 + 1;
end

figure;
RK_4();
RK_4_2();
RK_4_3();
xlabel('t');
ylabel('w');
title('RK4 Method with Different Step Sizes');
legend;
grid on;
hold on;

% RK_4.m
function plot_1()
h = 240;
t = 0;
w = 1200;

% Preallocate arrays for storing results


T = t;
W = w;

for i = 1:2
k1 = h * f(t, w);
k2 = h * f(t + h/2, w + k1/2);
k3 = h * f(t + h/2, w + k2/2);
k4 = h * f(t + h, w + k3);
w = w + (k1 + 2*k2 + 2*k3 + k4) / 6;
t = t + h;

% Store results
T(end+1) = t;
W(end+1) = w;
end

% Plot results
plot(T, W, 'DisplayName', 'h = 0.5');
hold on;
end

function v = f(t, y)
v = (-2.2067*10^(-12))*(y^4-81*(10^8))
end

You might also like