Making a Simulation of planets orbits as they move around each other.... Getting imaginary numbers for some reason

1 view (last 30 days)
I have been working on this code for quite some time now and I am at wits end for figuring out the issue. My code is designed take a bunch of user inputted data for two planets (such as direction,d....velocity,v...and mass,m) and then create a visual simulation of them moving...
somehow for some reason my code works for a little bit... then my theta computation (see below) starts to output imaginary numbers (idk why) and it breaks
I have included a pre loaded set of data to be run just for visualization purposes of my problem
if true
% clear
clc
close all
load DataForPlanets.mat
%%Data Computation
a_px=-30;
a_py=0;
b_px=30;
b_py=0;
n=0;
for t=1:dt:5
n=n+1;
ff=(7*(a_m)*(b_m))/sqrt((abs(a_px(n)-b_px(n)))^2+(abs(a_py(n)-b_py(n)))^2);
theta_a=asind( (abs(a_py(n)-b_py(n)))/(abs(a_px(n)-b_px(n))) );
%PLANET A
a_fbx=ff*cosd(theta_a);
a_fby=-ff*sind(theta_a);
%x
a_ax=a_fbx/a_m;
a_vx=a_vx+a_ax*(t);
a_px(n+1)=a_px(n)+a_vx*t+.5*(a_ax)*(t)^2;
%y
a_ay=a_fby/a_m;
a_vy=a_vy+a_ay*(t);
a_py(n+1)=a_py(n)+a_vy*t+.5*(a_ay)*(t)^2;
%PLANET B
theta_b=theta_a-180;
b_fbx=ff*cosd(theta_b);
b_fby=-ff*sind(theta_b);
%x
b_ax=b_fbx/b_m;
b_vx=b_vx+b_ax*(t);
b_px(n+1)=b_px(n)+b_vx*t+.5*(b_ax)*(t)^2;
%y
b_ay=b_fby/b_m;
b_vy=b_vy+b_ay*(t);
b_py(n+1)=b_py(n)+b_vy*t+.5*(b_ay)*(t)^2;
end
%%Visual Simulation
ax=a_px;
ay=a_py;
bx=b_px;
by=b_py;
run1=0;
hold on
for n=1:n(end)
axis([-100 100 -100 100]);
plot(ax([1:n]),ay([1:n]),'b-')
plot(bx([1:n]),by([1:n]),'r-')
a(n)=plot(ax(n),ay(n),'bo');
b(n)=plot(bx(n),by(n),'ro');
if run1==1
delete(a(n-1))
delete(b(n-1))
else
run1=1;
end
drawnow
pause(.3)
end
end
ignore the first 'if true' and the last 'end' the issue i think is on the 'theta_a' line
I dont expect anyone to take the time to help me out but if you do it will be so greatly appreciated... this is my first real attempt at doing anything in matlab and it doesnt work -_-
thanks!
  2 Comments
Star Strider
Star Strider on 13 May 2017
Where (at what assignment) do the complex results first appear?
What are the variable values in that assignment?
(I see no obvious source.)
Vasko Nechev
Vasko Nechev on 13 May 2017
It happened on the 11th iteration of the first for loop. I mistakenly used a sin instead of a tan as I already found the error!
thanks though!

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 13 May 2017
We don't have your data .mat file. So we cannot even hazard a serious guess at where the answer lies, nor know how to fix it.
A wild guess puts it in one of these lines:
ff=(7*(a_m)*(b_m))/sqrt((abs(a_px(n)-b_px(n)))^2+(abs(a_py(n)-b_py(n)))^2);
theta_a=asind( (abs(a_py(n)-b_py(n)))/(abs(a_px(n)-b_px(n))) );
The first line is the sqrt of a sum of squares. So that is unlikely to have a problem. The second line has an asind in it, which can be highly problematic, if you don't take care with your computations.
That is consistent with your guess, but you have the data! Run the code, ONE line at a time.
My guess is that the argument to asind
(abs(a_py(n)-b_py(n)))/(abs(a_px(n)-b_px(n)))
is at some point either greater than 1, or less than -1. In either of those cases, you will generate a complex result.
My real question is, are you REALLY sure that computation is correct? It almost looks like something that would be appropriate for an inverse TANGENT function, not an inverse sine. But, hey, it is your computation, your formula. Only you know where it came from or why it is there.
  1 Comment
Vasko Nechev
Vasko Nechev on 13 May 2017
Your right! There should have been a 'atand' instead of a 'asind' ! ! ! Such a simple mistake but it totally messed with everything.
My apologies for not attaching the .mat file but your tip turned out to be correct! Thank you so much! :D

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!