Swift中的中心计算公式
我正在从事一个pet项目,该项目涉及计算日出/日落数据。我正在努力在Swift中实现以下公式: 中心方程: C=(1.9148*sin(平均太阳辐射量))+(0.0200* sin(2*平均太阳能发电量)+(0.0003*sin(3*平均太阳能发电量)) 以下是我应该得到的给定Lat/Lon的答案: C=1.9148*sin(18.30143135945)+0.0200*sin(2*18.30143135945)+ 0.0003*sin(3*18.30143135945)=0.61344892821988 这是我的代码,它没有给出正确的值作为最终值:Swift中的中心计算公式,swift,algebra,astronomy,Swift,Algebra,Astronomy,我正在从事一个pet项目,该项目涉及计算日出/日落数据。我正在努力在Swift中实现以下公式: 中心方程: C=(1.9148*sin(平均太阳辐射量))+(0.0200* sin(2*平均太阳能发电量)+(0.0003*sin(3*平均太阳能发电量)) 以下是我应该得到的给定Lat/Lon的答案: C=1.9148*sin(18.30143135945)+0.0200*sin(2*18.30143135945)+ 0.0003*sin(3*18.30143135945)=0.613448928
// meanSolarAnomaly has a value of 18.30143135945036 at this point
let center = (1.9148 * sin(meanSolarAnomaly)) + (0.0200 * sin(2*meanSolarAnomaly)) + (0.0003 * sin(3*meanSolarAnomaly))
我的代码显示center=-1.015867439183884,而正确的center=0.61344892821988
我反复检查了这个方程式,但我似乎看不出我的错误。我希望这是一个简单的语法错误,但如果它是
我正在根据提供的方程式和答案进行计算
以下是完整的编辑代码:
//: Playground - noun: a place where people can play
import UIKit
//calculator to determine what time of day Sunset / Sunrise will occur
func jdFromDate(date : NSDate) -> Double {
let JD_JAN_1_1970_0000GMT = 2440587.5
return JD_JAN_1_1970_0000GMT + date.timeIntervalSince1970 / 86400
}
func dateFromJd(jd : Double) -> NSDate {
let JD_JAN_1_1970_0000GMT = 2440587.5
return NSDate(timeIntervalSince1970: (jd - JD_JAN_1_1970_0000GMT) * 86400)
}
let julianOffset = 2451545 as Double
let refDateFormatter = NSDateFormatter()
refDateFormatter.dateFormat = "MM-dd-yyyy"
let today = NSDate()
let julianDaysToToday = round(jdFromDate(today))
//get the lat/lon variables set (Tampa in example)
let lat = 27.9681
let lon = 82.4764
//now we need to calculate julian cycle
let nRaw = (julianDaysToToday - julianOffset - 0.0009) - (lon / 360)
let n = round(nRaw)
//n now contains the julian cycle
//next we must calculate the julian date of solar noon (approximately)
//J* = 2451545 + 0.0009 + (lw/360) + n
let jSolarNoon = julianOffset + 0.0009 + (lon/360) + n
//next calculate the mean solar anomaly
//M = [357.5291 + 0.98560028 * (J* - 2451545)] mod 360
let meanSolarAnomaly = (357.5291 + 0.98560028 * (jSolarNoon - julianOffset)) % 360
//next calculate the equation of center
let center = (1.9148 * sin(meanSolarAnomaly)) + (0.0200 * sin(2*meanSolarAnomaly)) + (0.0003 * sin(3*meanSolarAnomaly))
//Now, using Center and Mean, calculate the ecliptical longitude of the sun.
//λ = (M + 102.9372 + C + 180) mod 360
let eclLonOfSun = (meanSolarAnomaly + 102.9372 + center + 180) % 360
//now we can finally get an accurate julian date for solar noon
let jTransit = jSolarNoon + (0.0053 * sin(meanSolarAnomaly)) - (0.0069 * sin(2 * eclLonOfSun))
//To calculate the hour angle we need to find the declination of the sun
//δ = arcsin( sin(λ) * sin(23.45) )
let declinationOfSun = asin(sin(eclLonOfSun) * sin(23.45))
//now calculate the hour angle
//H = arccos( [sin(-0.83) - sin(ln) * sin(δ)] / [cos(ln) * cos(δ)] )
let hourCosNum = sin(-0.83) - sin(lat) * sin(declinationOfSun)
let hourDenom = cos(lat)*cos(declinationOfSun)
let hourAngle = acos(hourCosNum)/hourDenom
//time to go back through the approximation again using the hour angle
let finalJulianApproximation = 2451545 + 0.0009 + ((hourAngle + lon)/360) + n
//The values of M and λ from above don't really change from solar noon to sunset, so there is no need to recalculate them before calculating sunset.
let jSet = finalJulianApproximation + (0.0053 * sin(meanSolarAnomaly)) - (0.0069 * sin(2*eclLonOfSun))
let sunset = dateFromJd(jSet)
正如@kennytm所说,平均异常(太阳或其他任何东西)是一个角度。Swift中的角度(以及数学库中的C)都是弧度,而天文学家用度来表示。以下是您在操场上的代码:
var meanSolarAnomaly = 18.30143135945036
var c = (1.9148 * sin(meanSolarAnomaly)) + (0.0200 * sin(2 * meanSolarAnomaly)) + (0.0003 * sin(3 * meanSolarAnomaly))
// = -1.01586743918389 - wrong answer
meanSolarAnomaly = meanSolarAnomaly * M_PI / 180.0
// Convert it to radians
c = (1.9148 * sin(meanSolarAnomaly)) + (0.0200 * sin(2 * meanSolarAnomaly)) + (0.0003 * sin(3 * meanSolarAnomaly))
// = 0.6134489282198807 - right answer
18.3是学位吗?我想你需要先把它转换成弧度。我用完整的代码更新了我的答案,让你看到meanSolarAnomaly(18.3)的推导过程。谢谢@kennytm和Grimxn的回答和反馈。这应该是显而易见的,但我的答案是肯定的!非常感谢你!!请随意接受答案!虽然18.xxx并不是一个以弧度表示的角度(意思是它们介于0和2π之间,或者-pi和π之间),但你提到的站点确实因为没有指定单位而有问题。我会尽快接受。我在计算太阳赤纬时遇到了类似的问题,但这不是最初的问题!:)将应用您的反馈,并查看是否是相同的错误