% Metodo de Taylor de ordem 3 integrado num intervalo (ti,tf) com 
% condicao inicial (t0,x0) e passo h

function x = Taylor3_twosided(ti,t0,tf,h,x0,F0,F1,F2)

% integracao no futuro

    tp = (t0:h:tf)';

    xp = zeros(length(tp),length(x0));
    xp(1,:) = x0;
    
    for i = 2:length(tp)
       
        xp(i,:) = xp(i-1,:) + h.*F0(tp(i-1),xp(i-1,:)) ...
            + ((h^2)/2).*F1(tp(i-1),xp(i-1,:)) + ((h^3)/6).*F2(tp(i-1),xp(i-1,:));
        
    end

% integracao no passado

   tm = (ti:h:t0)';

    xm = zeros(length(tm),length(x0));
    xm(length(tm),:) = x0;
    
    for i = 1:length(tm)-1
 
        j = length(tm)-i;
        xm(j,:) = xm(j+1,:) - h.*F0(tm(j+1),xm(j+1,:)) ...
            + ((h^2)/2).*F1(tm(j+1),xm(j+1,:)) - ((h^3)/6).*F2(tm(j+1),xm(j+1,:));
        
    end
    
% concatenacao

    x=[xm(1:length(tm)-1);xp];

end