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

Basics of Matlab Code for Runge-Kutta

h=0.01;
%step size
t = 0:h:1;
%range of t
x = zeros(1,length(t));
x(1) = -7;
%initial conidition
F_tx = @(t,x) 1-4*x*(1-x);
%given function
for i=1:(length(t)-1)
k_1 = F_tx(t(i),x(i));
k_2 = F_tx(t(i)+0.5*h,x(i)+0.5*h*k_1);
k_3 = F_tx((t(i)+0.5*x),(x(i)+0.5*h*k_2));
k_4 = F_tx((t(i)+h),(x(i)+k_3*h)); % parts of Runge-Kutta
x(i+1) = x(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h; %entire Runge-Kutta
end
plot(t,x,'y')

%plot curve in certain colour.

From this I compiled a single plot with the 8 different initial conditions,
(-7, -5, -3, -1, 1, 3, 5, 7).

You might also like