% Runge-Kutta de 4ª ordem
 
 
format short;
 
clear all
close all
a=0; 
b=10;
h=0.1;
n=3;
np=(b-a)/h+1;
y=zeros(np,n);
t=[a:h:b]';
y0=[1 1 1];
y(1,:)=y0;
c2=10;

for k=1:np-1

        tk=t(k);
        yk=y(k,:)';
        k1=[(-yk(1)) (yk(1)-c2*yk(2)) (c2*yk(2))]';

        tk=t(k)+0.5*h;
        yk=y(k,:)'+0.5*h*k1;
        k2=[(-yk(1)) (yk(1)-c2*yk(2)) (c2*yk(2))]';

        yk=y(k,:)'+0.5*h*k2;
        k3=[(-yk(1)) (yk(1)-c2*yk(2)) (c2*yk(2))]';

        tk=t(k)+h;
        yk=y(k,:)'+h*k3;
        k4=[(-yk(1)) (yk(1)-c2*yk(2)) (c2*yk(2))]';
 
        media=h/6*(k1+2*k2+2*k3+k4);

        y(k+1,:)=y(k,:)+media';
    
 

 end 
temp=y;
 
save rk_ex2ci temp
% grafico
plot(t,y(:,1),'r*:'); hold on; 
plot(t,y(:,2),'b+:'); hold on;
plot(t,y(:,3),'k.-'); hold on;
xlabel('x_k'); hold on; 
ylabel('t_k'); hold on; 
%legend('Runge-Kutta 4ª ordem',1); hold on; 
