How to respresent and plot data while on loop in Matlab?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alexandru Lapusneanu
el 13 de Ag. de 2018
Comentada: Alexandru Lapusneanu
el 13 de Ag. de 2018
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:
figure(1);
orbitsat1=animatedline('Color','r');
orbitsat2=animatedline('Color','g');
msat1=124;
msat2=234;
Asat1=2.2;
Asat2=3.2;
sat10(1,1)= 453322.3616 ;
sat10(2,1)= -2346021.219 ;
sat10(3,1)= -7894131.349 ;
sat10(4,1)= 2142.38067;
sat10(5,1)= 7487.44895 ;
sat10(6,1)= -9864.00872;
sat20(1,1)= 543322.3616 ;
sat20(2,1)= -3246021.219 ;
sat20(3,1)= -8794131.349 ;
sat20(4,1)= 1242.38067;
sat20(5,1)= 4787.44895 ;
sat20(6,1)= -8964.00872;
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) = double(sat2(:,i) + (1/6)*(k_12+2*k_22+2*k_32+k_42)*h);
sat1(:,i+1) = double(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 = ((sat1(1,i).^2 + sat1(2,i).^2 + sat1(3,i).^2).^0.5);
Rcsc = ((sat2(1,i).^2 + sat2(2,i).^2 + sat2(3,i).^2).^0.5);
dif=Rorb-Rcsc;
if (dif >0.5*10^10 && dif < 2*10^5) && 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.4418*10^9;
magn=(sat1(1)^2+sat1(2)^2+sat1(3)^2)^(3/2);
sat1prim=zeros(6,1);
sat1prim(1,1)=sat1(4);
sat1prim(2,1)=sat1(5);
sat1prim(3,1)=sat1(6);
sat1prim(4,1)=double(-miu*sat1(1)/magn);
sat1prim(5,1)=double(-miu*sat1(2)/magn);
sat1prim(6,1)=double(-miu*sat1(3)/magn);
end
and simsat2
function sat2prim=simsat2(t,sat2,msat2,Asat2,vit)
miu=398600.4418*10^9;
magn=(sat2(1)^2+sat2(2)^2+sat2(3)^2)^(3/2);
sat2prim=zeros(6,1);
sat2prim(1,1)=sat2(4);
sat2prim(2,1)=sat2(5);
sat2prim(3,1)=sat2(6);
sat2prim(4,1)=double(-miu*sat2(1)/magn);
sat2prim(5,1)=double(-miu*sat2(2)/magn);
sat2prim(6,1)=double(-miu*sat2(3)/magn);
end
6 comentarios
James Tursa
el 13 de Ag. de 2018
So, is this all supposed to be inside a function, and you want to return the satellite positions up to the point of the collision to the caller? But you are having some type of problem doing that?
Respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!