Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Anand Tathe

Q
Q3

DEIC
roll no 65

MATLAB code :
Runge-Kutta Method :
F = @(y, x) (1 + x * y); % Function representing the right-hand side
of the ODE y' = F(x, y)
% Initial condition
a = 0; % Initial value of x
y0 = 0; % Initial value of y(a)

% Step size and number of steps


h = 0.1; % Step size
N = 10; % Number of steps

% Initialize arrays to store values


y = zeros(1, N+1);
x = zeros(1, N+1);

% Set initial values


y(1) = y0;
x(1) = a;

% Runge-Kutta method (4th order) for solving the ODE


for i = 1:N
k1 = h * F(y(i), x(i));
k2 = h * F(y(i) + 0.5 * k1, x(i) + 0.5 * h);
k3 = h * F(y(i) + 0.5 * k2, x(i) + 0.5 * h);
k4 = h * F(y(i) + k3, x(i) + h);
y(i+1) = y(i) + (1/6) * (k1 + 2*k2 + 2*k3 + k4);
x(i+1) = a + i * h;
fprintf('Approximate value of y at x=%.2f: %.6f\n', x(i+1),

y(i+1));
end
Output :
Runge_Kutta Approximate value of y at
x=0.10: 0.100334 Approximate value of y at
x=0.20: 0.202688 Approximate value of y at
x=0.30: 0.309164 Approximate value of y at
x=0.40: 0.422032 Approximate value of y at
x=0.50: 0.543826 Approximate value of y at
x=0.60: 0.677461 Approximate value of y at
x=0.70: 0.826367 Approximate value of y at
x=0.80: 0.994660 Approximate value of y at
x=0.90: 1.187363 Approximate value of y at
x=1.00: 1.410685

You might also like