What I'm trying to to is to simulate the position of two satellites and to determine if they will collide. For that I'm using a Rk4 method. As you can see, in the loop I have the drawnow command that adds a point on a 3d graph, after avery iteration. What I'm interested in is to find a way to represent the 3d plot until the condition of collision meets (where I have the message) and also to get the values on the .txt file, while in the loop not outside. When I use the return command, and try to plot or to add text, I have an error "vectors must be the same length" and doesn't let me obtain the data. That happens because lets say sat1=1X200 while tspan=1X201. Is there any other command that I could use instead of return or a better solution?
Here is my code:
clear; close all; clc;
figure(1);
orbitsat1 = animatedline('Color','r');
msat1 = 124;
msat2 = 234;
Asat1 = 2.2;
Asat2 = 3.2;
sat10(1) = 453322.3616;
sat10(2) = -2346021.219;
sat10(3) = -7894131.349;
sat10(4) = 2142.38067;
sat10(5) = 7487.44895;
sat10(6) = -9864.00872;
sat10 = sat10';
sat20(1) = 543322.3616;
sat20(2) = -3246021.219;
sat20(3) = -8794131.349;
sat20(4) = 1242.38067;
sat20(5) = 4787.44895;
sat20(6) = -8964.00872;
sat20 = sat20';
tspan = 0:200;
secpday = 60*60*24;
a1 = 2018;
la1 = 1;
z1 = 2;
o1 = 12;
m1 = 0;
s1 = 0;
n1 = datenum(a1,la1,z1,o1,m1,s1);
n1 = n1 + tspan/secpday;
[an,luna,zi,ora,min,sec] = datevec(n1);
h = 1;
sat1 = zeros(6, tspan(end)/h);
sat1(:,1) = sat10;
sat2 = zeros(6, tspan(end)/h);
sat2(:,1) = sat20;
for i = 1:tspan(end)/h
k_1 = simsat1(tspan(i), sat1(:,i), msat1, Asat1, sat1(4:6,i));
k_2 = simsat1(tspan(i)+0.5*h, sat1(:,i)+0.5*h*k_1,msat1, Asat1, sat1(4:6,i));
k_3 = simsat1(tspan(i)+0.5*h, sat1(:,i)+0.5*h*k_2, msat1, Asat1, sat1(4:6,i));
k_4 = simsat1(tspan(i)+h, sat1(:,i)+k_3*h, msat1, Asat1, sat1(4:6,i));
k_12 = simsat2(tspan(i), sat2(:,i), msat2, Asat2, sat2(4:6,i));
k_22 = simsat2(tspan(i)+0.5*h, sat2(:,i)+0.5*h*k_12, msat2, Asat2, sat2(4:6,i));
k_32 = simsat2(tspan(i)+0.5*h, sat2(:,i)+0.5*h*k_22, msat2, Asat2, sat2(4:6,i));
k_42 = simsat2(tspan(i)+h, sat2(:,i)+k_32*h, msat2, Asat2, sat2(4:6,i));
sat2(:,i+1) = sat2(:,i) + (1/6)*(k_12+2*k_22+2*k_32+k_42)*h;
sat1(:,i+1) = sat1(:,i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h;
addpoints(orbitsat1, sat1(1,i), sat1(2,i), sat1(3,i));
drawnow update;
hold off;
Rorb = sqrt(sum(sat1(1:3,i).^2));
Rcsc = sqrt(sum(sat2(1:3,i).^2));
dif = Rorb-Rcsc;
if (dif >0.5e10 && dif < 2e5) && i>350
Message = sprintf('Data:\nan:%d\nluna:%d\nzi:%d\nora:%d\nminut:%d\nsecunda:%d',an(i),luna(i),zi(i),ora(i),min(i),sec(i));
msgbox(Message)
msgbox('Coliziune!','Coliziune','warn',Message);
return
end
end
Pozx = sat1(1,:);
Pozy = sat1(2,:);
Pozz = sat1(3,:);
Tbody = [an luna zi ora min sec Pozx Pozy Pozz]';
twobody = fopen('nobody.txt','w');
fprintf(twobody,'Rezultate Twobody (metri) \n\n');
fprintf(twobody,' An luna zi ora min sec pozitia pe x pozitia pe y pozitia pe z viteza pe x viteza pe y viteza pe z\n\n');
fprintf(twobody,'---------------------------------------------------------------------------------------------------------------------------------------------\n%6d-%3d-%3d\t%3d:%3d:%3d\t\t\t\t%12.6f\t%12.6f\t\t%12.6f\t\t%12.6f\t%\n',Tbody);
fclose(twobody);
where simsat1
function sat1prim = simsat1(t,sat1,msat1,Asat1,vit)
miu = 398600.4418e9;
magn = sum(sat1(1:3).^2)^(3/2);
sat1prim = zeros(6,1);
sat1prim(1:3) = sat1(4:6);
sat1prim(4:6) = -miu.*sat1(1:3)./magn;
end
and simsat2
function sat2prim = simsat2(t,sat2,msat2,Asat2,vit)
miu = 398600.4418e9;
magn = sum(sat2(1:3).^2)^(3/2);
sat2prim = zeros(6,1);
sat2prim(1:3) = sat2(4:6);
sat2prim(4:6) = -miu.*sat2(1:3)./magn;
end
tspanvalue for plotting?secundePeZi,msat1,Asat1and then it throws an error when indexingsimsat1. There may be error afterwards but I gave up trying by this point. Do the following; addclose all; clear; clc;line at the very top of your code. Then make sure it runs and update your question. I'm guessing this code worked for you because you had all of these variables already in your workspace. To avoid this type of 'crosstalk' between the scripts I'd recommend always putting theclose all; clear; clc;at the beginning of every piece of code.dif >0.5*10^10 && dif < 2*10^5) && i>350. How candifbe simultaneously bigger than 5e9 and smaller than 2e5 ?? Are you sure you've got your<>signs correct. Also; are you sure that on the line before it shouldn't saydif = abs(Rorb-Rcsc);? 2. Your code as supplied does not actually produce the collision you speak of (nor the error on that matter). 3. Your functions take 5 input arguments but use only one - thesat1andsat2's. The others are apparently superfluous but are used and varied in your code...(?)