我正在努力改进美国海军天文台的algorithm for calculating the position of the sun,以更简单的方式编写,所有数字都已正确识别 - 所以普通人也可以理解它。
但不知何故,时间等式已经过了27天。也许有人在这里可以发现什么是错的?
试运行:
1) Failure:
test_equation_of_time(TestSolarCalculations):
Expected 2011-10-01 10:23:00 UTC, not 2011-10-28 10:59:31 UTC.
2) Failure:
test_suns_declination(TestSolarCalculations):
Expected -3.18, not -3.2087920449753007.
新算法:
# Constants for J2000.0 / 1 Jan 2000 12:00Z:
#
EPOCHS_JULIAN_DATE = 2451545
ANOMALISTIC_YEAR_IN_DAYS = 365.259636
TROPICAL_YEAR_IN_DAYS = 365.2421897
SUNS_MEAN_ANOMALY_AT_EPOCH = degrees_to_radians 357.5291
SUNS_MEAN_LONGITUDE_AT_EPOCH = degrees_to_radians 280.459
SUNS_GEODETIC_PRECESSION = degrees_to_radians 1.915
EARTHS_ORBITAL_ECCENTRICITY = 0.020
EARTHS_ADJUSTED_AVERAGE_RADIUS_IN_AU = 1.00014
EARTHS_APPROXIMATE_ATMOSPHERIC_REFRACTION = degrees_to_radians 0.01671
EARTHS_ECLIPTIC_MEAN_OBLIQUITY = degrees_to_radians 23.439
EARTHS_ECLIPTIC_OBLIQUITY_CHANGE_RATE = degrees_to_radians 0.00000036
def days_from_epoch
@todays_julian_date - EPOCHS_JULIAN_DATE
end
def suns_daily_mean_anomaly_change_rate
degrees_to_radians(360 / ANOMALISTIC_YEAR_IN_DAYS)
end
def suns_mean_anomaly
SUNS_MEAN_ANOMALY_AT_EPOCH + suns_daily_mean_anomaly_change_rate * days_from_epoch
end
def suns_daily_mean_longitude_change_rate
degrees_to_radians(360 / TROPICAL_YEAR_IN_DAYS)
end
def suns_mean_longitude
SUNS_MEAN_LONGITUDE_AT_EPOCH + suns_daily_mean_longitude_change_rate * days_from_epoch
end
def suns_apparent_ecliptic_longitude
suns_mean_longitude + SUNS_GEODETIC_PRECESSION * sin(suns_mean_anomaly) + EARTHS_ORBITAL_ECCENTRICITY * sin(2 * suns_mean_anomaly)
end
def suns_distance_from_earth_in_au
EARTHS_ADJUSTED_AVERAGE_RADIUS_IN_AU - EARTHS_APPROXIMATE_ATMOSPHERIC_REFRACTION * cos(suns_mean_anomaly) - (EARTHS_APPROXIMATE_ATMOSPHERIC_REFRACTION ^ 2 / 2) * cos(2 * suns_mean_anomaly)
end
def earths_ecliptic_mean_obliquity
EARTHS_ECLIPTIC_MEAN_OBLIQUITY - EARTHS_ECLIPTIC_OBLIQUITY_CHANGE_RATE * days_from_epoch
end
def suns_right_ascension
atan2(cos(suns_apparent_ecliptic_longitude) * sin(suns_apparent_ecliptic_longitude), cos(suns_apparent_ecliptic_longitude)) / 15
end
# Time.utc(Time.now.year) => 2011-01-01 00:00:00 UTC
#
def equation_of_time
Time.utc(Time.now.year) + (radians_to_degrees(suns_mean_longitude) / 15 - suns_right_ascension) * 60 * 60 * 24
end
def suns_declination
radians_to_degrees(asin(sin(earths_ecliptic_mean_obliquity) * sin(suns_apparent_ecliptic_longitude)))
end
谢谢!
垫
答案 0 :(得分:1)
下面:
def suns_apparent_ecliptic_longitude
suns_mean_longitude
+ SUNS_GEODETIC_PRECESSION * sin(suns_mean_anomaly)
+ EARTHS_ORBITAL_ECCENTRICITY * sin(2 * suns_mean_anomaly)
end
你是混合单位。 suns_mean_longitude
是弧度,SUNS_GEODETIC_PRECESSION
也是如此(因此第二项也是如此),但EARTHS_ORBITAL_ECCENTRICITY
为0.020
度。
此外:
EARTHS_APPROXIMATE_ATMOSPHERIC_REFRACTION = degrees_to_radians 0.01671
应该只是
EARTHS_APPROXIMATE_ATMOSPHERIC_REFRACTION = 0.01671
我认为,因为这用于确定距离的公式。
另外,
def suns_right_ascension
atan2(cos(suns_apparent_ecliptic_longitude) * sin(suns_apparent_ecliptic_longitude), cos(suns_apparent_ecliptic_longitude)) / 15
end
我还没有检查过这个atan2
函数返回的单位 - 你确定它是度数,它必须是为了除以15才有意义吗?
可能还有其他问题;这些只是我能看到的。
答案 1 :(得分:1)
我的一些代码。
# From angles.rb:<br>
# eccentricity of elliptical Earth orbit around Sun
# Horner calculation method
def eccentricity_Earth( ta = A2000 )
ta = check_jct_zero( ta )
# 0.016708617 - ta[ 0 ] * ( 0.000042037 + ta[ 0 ] * 0.0000001235 )
[-0.0000001235, -0.000042037, 0.016708617].inject(0.0) {|p, a| p * ta[0] + a}
end
alias_method :eccentricity_earth_orbit, :eccentricity_Earth
它在哪里使用?
# From angles.rb:<br>
# equation of centre
# added to mean anomaly to get true anomaly.
def center( ta = A2000)
ta = check_jct_zero( ta )
sine_1M = sin( 1.0 * deg_to_rad( @ma ) )
sine_2M = sin( 2.0 * deg_to_rad( @ma ) )
sine_3M = sin( 3.0 * deg_to_rad( @ma ) )
sine_4M = sin( 4.0 * deg_to_rad( @ma ) )
sine_5M = sin( 5.0 * deg_to_rad( @ma ) )
e = eccentricity_Earth( ta )
rad_to_deg( sine_1M * ( 2.0 * e - e**3/4.0 + 5/96.0 * e**5 ) +
sine_2M * ( 5/4.0 * e**2 - 11/24.0 * e**4 ) +
sine_3M * ( 13/12.0 * e**3 - 43/64.0 * e**5 ) +
sine_4M * 103/96.0 * e**4 +
sine_5M * 1097/960.0 * e**5 )
# sine_1M *( 1.914602 - ta[ 0 ] * ( 0.004817 + ta[ 0 ] * 0.000014 )) +
# sine_2M *( 0.019993 - ta[ 0 ] * 0.000101 ) +
# sine_3M * 0.000289
end
alias_method :equation_of_center, :center
答案 2 :(得分:0)
可能是Ruby浮点数的精度问题,而不是程序逻辑的问题?我没有使用ruby,但似乎根据这个帖子存在ruby浮点运算中存在的一些问题。 Issue with precision of Ruby math operations
根据帖子,您可能需要使用BigDecimal类进行浮点运算。我实际上没有通过你的程序,这只是猜测。