Input, Calling function, Data, Func doesnt work??

1 view (last 30 days)
Cat
Cat on 3 Jun 2017
Commented: Cat on 6 Jun 2017
So after much re-writing I can't find a solution to why my function doesn't work. I appreciate it's probably written very badly but it results in RA = [empty sym] Dec = [empty sym]. I don't think the variables of the nested functions are being used by the parent function?
Any advice would be greatly appreciated, please and thank you.
function planet_pos()
long = []; inc = []; w = []; a = []; ec = []; m = []; obl = []; ea = [];
v = []; r = []; xec = []; yec = []; zec = []; xeq = []; yeq = []; zeq = [];
year = input('please enter the year');
mon = input('please enter the month as a number');
day = input('please enter the day');
d = 367 * year - (7*(year +((mon + 9)/12)))/4 + (275 * mon)/9 + day - 730530;
planet = input('please enter the planet','s');
function [long,inc,w,a,ec,m,obl]= mercury(d)
long = 48.3313 + 3.24587e-5 * d;
inc = 7.0047 + 5.00e-8 * d;
w = 29.1241 + 1.01444e-5 * d;
a = 0.387098;
ec = 0.205635 + 5.59e-10 * d;
m = 168.6562 + 4.0923344368 * d;
obl = 0.1;
end
function [long,inc,w,a,ec,m,obl]= venus(d)
long = 76.6799 + 2.46590e-5 * d;
inc = 3.3946 + 2.75e-8 * d;
w = 54.8910 + 1.38374e-5 * d;
a = 0.723330;
ec = 0.006773 - 1.302e-9 * d;
m = 48.0052 + 1.6021302244 * d;
obl = 177.4;
end
function [long,inc,w,a,ec,m,obl]= earth(d)
long = 174.8731758 - 0.2410908 * d;
inc = 0 + 0.0130548 * d;
w = 282.9404 + 4.70935e-5;
a = 1.0000010;
ec = 0.00167086 - 1.151e-9 * d;
m = 365.0470 + 0.9856002585 *d;
obl = 23.45;
end
function [long,inc,w,a,ec,m,obl]= mars(d)
long = 49.5574 + 2.11081e-5 * d;
inc = 1.8497 - 1.78E-8 * d;
w = 286.5016 + 2.92961E-5 * d;
a = 1.523688;
ec = 0.093405 + 2.516E-9 * d;
m = 18.6021 + 0.5240207766 * d;
obl = 25.19;
end
function [long,inc,w,a,ec,m,obl]= jupiter (d)
long = 100.4542 + 2.76854e-5 * d;
inc = 1.3030 - 1.557e-7 * d;
w = 273.8777 + 1.64505e-5 * d;
a = 5.20256;
ec = 0.048498 + 4.469e-9 * d;
m = 19.8950 + 0.0830853001 * d;
obl = 3.12;
end
function [long,inc,w,a,ec,m,obl]= saturn(d)
long = 113.6634 + 2.38980e-5 * d;
inc = 2.4886 - 1.081e-7 * d;
w = 339.3939 + 2.97661e-5 * d;
a = 9.55475;
ec = 0.055546 - 9.499e-9 * d;
m = 316.9670 + 0.0334442282 * d;
obl = 26.73;
end
function [long,inc,w,a,ec,m,obl]= uranus(d)
long = 74.0005 + 1.3978e-5 * d;
inc = 0.7733 + 1.9e-8 * d;
w = 96.6612 + 3.0565e-5 * d;
a = 19.18171 - 1.55e-8 * d;
ec = 0.047318 + 7.45e-9 * d;
m = 142.5905 + 0.011725806 * d;
obl = 97.86;
end
function [long,inc,w,a,ec,m,obl]= neptune (d)
long = 131.7806 + 3.0173e-5 * d;
inc = 1.7700 - 2.55e-7 * d;
w = 272.8461 - 6.027e-6 * d;
a = 30.05826 + 3.313e-8 * d;
ec = 0.008606 + 2.15e-9 * d;
m = 260.2471 + 0.005995147 * d;
obl = 29.56;
end
pi20 = vpa(pi);
sind = @(x) sin(x*pi20/180);
asind = @(x) sin(x) * 180/pi20;
cosd = @(x) cos(x*pi20/180);
tand = @(x) tan(x*pi20/180);
atand = @(x) atan(x) * 180/pi20;
atan2d = @(y,x) atan2(y, x) * 180/pi20;
%find eccentric anomaly
ea = m + (180/pi20)* ec * sind(m)* (1 + ec *cosd(m));
keeplooping = true;
while ea > 0.005
E1 = (ea - (180/pi20)* ec *sind(ea)-m)/(1-ec *cosd(ea));
keeplooping;
if ea <= 0.005
keeplooping = false;
end
E1 = ea;
end
%find true anomaly and radial distance
v = 2*atand((sqrt(1+ec))* tand(ea/2));
r = (a *(1-(ec^2))/(1+ec* cosd(v)));
%compute ecliptic rectangular coordinates
x = [];
y = [];
x = cosd(ea) - ec;
y = sind(ea) * sqrt(1 - ec* ec);
r = sqrt(x*x + y*y);
v = atan2d(y,x);
long = v+w;
if long < 0
long = 360 + long;
elseif long > 360
long = long - 360;
end
xec = r * (cosd(long)* cosd(v+w) - sind(long)* sind(v+w)* cosd(inc));
yec = r * (sind(long)* cosd(v+w) - cosd(long)* sind(v+w)* cosd(inc));
zec = r * sind(v+w)* sind(inc);
%compute equitoral coordinates, right ascension and declination then print
xeq = xec;
yeq = yec * cosd(obl) - zec * sind(obl);
zeq = yec * sind(obl) + zec * cosd(obl);
RA = atan2d(yeq, xeq)
eq = sind(obl);
Dec = asind(eq/r)
fprintf('Right Acension is %.2f\n', RA)
fprintf ('Declination is %.2f\n', Dec)
end
  9 Comments
dpb
dpb on 3 Jun 2017
I'm still not seeing that the structure solves the computation question, Stephen?
Granted, it has many more level than needed; there could be simply one function that populates the result with a set--oh, I guess that's what you're saying...gotcha'.
Cat
Cat on 6 Jun 2017
thank you so much, definitely gives me some direction now.. very much appreciated!!

Sign in to comment.

Answers (0)

Categories

Find more on Mathematics 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!