%   three body simulate problem
clear all
X=zeros(8,3);           %location velocity mass R
force=zeros(3,3,3); %force(i,j,k)means body(i) forced by body(j),the force is 3-dim
global G
G=1;
for N=1:1      %generate N times
    % 1. generate random location,velocity and mass
    %X=random(X);
    % 2 .definite the parameters manually
    X=[100 0 0    0 1 0    1 0.1;
         0 0 0    0 0 0        100 10;
         -1 0 0    0 -10 0    1 0.1]';
    %generate the filename
    file_name=strcat(['mat_threebody',num2str(N),'.dat']);
    %open file
    file=fopen(file_name,'w');
    %time march for one system
    T=10;     %second or others
    dt=0.01;
    for t=1:dt:T
        distance=dis(X);                    %function calculate the distance
        force=F_body(X);   %function calculate the force without the collapse
       acce=zeros(3,3);
        for i=1:3
            for j=1:3
                acce(i,j)=force(i,1,j)+force(i,2,j)+force(i,3,j);
            acce(i,j)=acce(i,j)/X(7,i);
            end
        end
        for i=1:3
            for j=1:3
          fprintf(file,'%8.3f',X(j,i));
          X(j,i)=X(j,i)+(X(j+3,i)+acce(i,j)*dt)*dt;
            end
        end
        fprintf(file,'\n');
    end
    fclose(file);
end