MATLAB: Does anyone could help me out with this RK4 code, the code runs but it does plot nothing

runge_kutta code

function rungekutta
h = 0.1; %tamao de paso
t = 0; %valor incial del intervalo
a = 1; %valor final del intervalo
w = 0.5; %condicin inicial
n=(a-t)/h; %cantidad de subintervalos
for i=0:n-1
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);
wo = w + (k1+2*k2+2*k3+k4)/6;
to = t + h;
fprintf('Step %d: to = %6.4f\n wo = %18.15f\n', i, to, wo);
plot(wo, to)
end
%%%%%%%%%%%%%%%%%%
function v = f(t,y)
v = y-t^2+1;

Best Answer

function rungekutta
h = 0.1; %tama?o de paso
aleft = 0;
aright = 1;
n = (aright-aleft)/h; %cantidad de subintervalos
t = zeros(n+1,1); %valor incial del intervalo
w = zeros(n+1,1); %condici?n inicial
t(1) = 0;
w(1) = 0.5;
for i=2:n+1
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(i) = w(i-1) + (k1+2*k2+2*k3+k4)/6;
t(i) = t(i-1) + h;
end
plot(t,w)
%%%%%%%%%%%%%%%%%%
function v = f(t,y)
v = y-t^2+1;
Related Question