In: Advanced Math
Use the Runge-Kutta method and the Runge-Kutta semilinear method with the indicated step sizes to find approximate values of the solution of the given initial value problem at 11 equally spaced points (including the endpoints) in the interval. This question is from the differential equation.
y'-4y = x/y^2(y+1) , y(0) = 1; h=0.1, 0.05 , 0.025, on [0, 1]
clear all
close all
%function for which RK4 method have to calculate
f=@(x,y) 4.*y+(x./((y.^2).*(y+1)));
fprintf('function for which RK4 method have to calculate f(x,y)=
')
disp(f)
%all h
h=[0.1 0.05 0.025];
%step size h
%initial value
y0=1; x0=0;
xx=0:0.1:1;
fprintf('\tx\ty_h=0.1 \ty_h=0.05
\ty_h=0.025\n')
for it=1:length(xx)
x_end=xx(it);
[t_result1,y1_result1] =
RK4_method(f,y0,x0,x_end,h(1));
[t_result2,y1_result2] =
RK4_method(f,y0,x0,x_end,h(2));
[t_result3,y1_result3] =
RK4_method(f,y0,x0,x_end,h(3));
fprintf('\t%2.2f
\t%f\t%f\t%f\n',t_result1(end),y1_result1(end),y1_result2(end),y1_result3(end))
end
%%Matlab function for Runge Kutta Method
function [t_rk,y_rk]=RK4_method(f,yinit,tinit,tend,h)
% RK4 method
% h amount of intervals
t=tinit; % initial
t
y=yinit; % initial
y
t_eval=tend; % at what
point we have to evaluate
n=(t_eval-t)/h; % Number of steps
t_rk(1)=t;
y_rk(1)=y;
for i=1:n
%RK4 Steps
k1=h*double(f(t,y));
k2=h*double(f((t+h/2),(y+k1/2)));
k3=h*double(f((t+h/2),(y+k2/2)));
k4=h*double(f((t+h),(y+k3)));
dy=(1/6)*(k1+2*k2+2*k3+k4);
t=t+h;
y=y+dy;
t_rk(i+1)=t;
y_rk(i+1)=y;
end
end
%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%