Assignment 2
Assignment 2
PROBLEM STATEMENT:
y’ = -2xy2 ; y(0) = 1
Find y at x = 1 using Runge – Kutta method of order 4 by solving the given IVP. Take step
size of 0.2
MODEL:
k1 = h f (xn , yn)
k4 = h f(xn + h, yn + k3)
Thus, the equation became y = 1/(x2 + 1), and y at x = 1 = 0.5, which is the same solution we
found using the RK method.
CODE:
x_initial = 0;
y_initial = 1;
step_length = 0.2;
i = 1;
while x_initial<=1
k1 = step_length*y_dash(x_initial,y_initial);
k2 = step_length*y_dash((x_initial+step_length/2),(y_initial + k1/2));
k3 = step_length*y_dash((x_initial+step_length/2),(y_initial + k2/2));
k4 = step_length*y_dash((x_initial+step_length),(y_initial + k3));
km = (k1+2*k2+2*k3+k4)/6;
x(i) = x_initial;
y(i) = y_initial;
i = i+1;
end
disp(y_initial)
[y1’, y2’] = [ 9y1 + 24y2 + (5 cos(t) – (1/3) sin(t), -24y1 -51y2 -9 cos(t) + (1/3) sin(t)]
y0 = [4/3, 2/3]
Solve the system of differential equations by 4th Order RK method from t = 0 to t = 1 using
step size (h) of 0.05, and find the area under the curve
MODEL:
k1 = h f (xn , yn)
k4 = h f(xn + h, yn + k3)
Here, since there are 3 variables in each equation (y1, y2, t), each call becomes
k1 = h f(yn1 , yn2, t)
l1 = h g(xn1 , yn2, t)
and so on.
y1_0 = 4/3;
y2_0 = 2/3;
t0 = 0;
tf = 1;
h = 0.05;
% Number of steps
N = (tf - t0) / h;
t = t0:h:tf;
y1 = zeros(1, N+1);
y2 = zeros(1, N+1);
y1(1) = y1_0;
y2(1) = y2_0;
for i = 1:N
end
fprintf('t\t\ty1\t\ty2\n');
for i = 1:N+1
end
figure;
subplot(2,1,1);
xlabel('t');
ylabel('y1');