## Circles shouldn’t be this hard

For my project for my History class, I’m writing a computer simulation of (among other things) Ptolemy’s model of the solar system. Ptolemy’s model is based entirely on constant circular motion. Since the sun, moon, and stars don’t actually travel on circular paths, the model does things like having a planet move around a circle, and the center of that circle moves around another circle, and that circle’s center is offset by a small amount from the Earth.

It’s somewhat disconcerting that I’m having so much trouble implementing his model for the sun, which is the easiest part of his model. According to Ptolemy, the sun moves around a circle, and that circle’s center is offset from the Earth. No circles-in-circles or anything like that.

Naturally, the models have a few parameters, such as the radii of circles and the speeds at which points move along their paths. What I’m trying to do is write the program so that it tries to fit the parameters to a set of “observations” so that my simulation is testing the model itself and not the particular parameters that Ptolemy chose. Sadly, this seems to be harder than I thought it would be. Even though I have an algorithm for doing the non-linear least-squares (that seems to work well, no less), I haven’t yet gotten it to work for this model. It keeps thinking the matrices I’m trying to solve are singular. Which probably means I’m feeding it the wrong data. *sigh*

Also, it would be fantastic if the inverse trig functions knew which of the two angles I wanted. Java’s Math.atan2() is pretty useful here, but there’s no equivalent for Math.acos(). I wouldn’t be surprised if it’s something like that that’s keeping things from working….

## One Response

No, not wrong data. It turns out the function that defines the sun’s position isn’t continuous in terms of the parameters. Nonlinear least squares doesn’t play well with discontinuous functions. The series of guesses it generates just get wilder and wilder, bouncing around all over the place. (The eccentricity stayes small, but the two angles weren’t even bounded by +/- 2*pi.)

Looks like I’ll need to deduce the parameters by looking at the behavior of the function. It doesn’t feel as precise, but it might actually *work*.