1

我终于得到了从曲线到运行点的距离公式: approx = 2 * (b * (Math.Log(a) * (Math.Log(k) * Math.Pow(k, (b * cycleX))) * Math.Pow(a, (Math.Pow(k, (b * cycleX)))) * (Math.Pow(a, (Math.Pow(k, (b * cycleX))))) - points[i].Y) + cycleX - points[i].X);

因此,随着approx接近 0,cycleX给了我正确的坐标来计算到该点的距离。

这里唯一的问题是定义一种修改cycleX. 我尝试使用一系列if',但有时approx最终会跳到正数(来自负数)。我应该怎么做才能正确修改cycleX的值?

注意:通常需要降低到 0.0001 才能得到 -1 到 1 范围内的值。

4

1 回答 1

0

For this kind of problem, it's often useful to know about Newton's method:

Of course, the forumula for that is

x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}. \,!

Of course, besides the fact that for some functions this quite unstable (I don't expect yours to be, though), implemented purely for your case, it would mean you would need to calculate yet another derivative (of your derivative)! However, I think for your case, you might be able to just approximate the derivative.

You didn't mention the language your implementation would eventually be in, so I'll just use javascript for convenience.

To estimate your derivative, simply choose a deltaX that would be convenient.

So if you have a function

var df = function (cycleX) {
  return 2 * (b * (Math.log(a) * (Math.log(k) * Math.pow(k, (b * cycleX))) * Math.pow(a, (Math.pow(k, (b * cycleX)))) * (Math.pow(a, (Math.pow(k, (b * cycleX))))) - Y) + cycleX - X);
};

you can estimate it's derivative via

  y = df(cycleX);
  y1 = (df(cycleX + deltaX) - y) / deltaX;

And then proceed via.

  cycleXnew = cycleX - y / y1;

And then it's just a matter of looping until it converges (or not).

See example jsFiddle: http://jsfiddle.net/jfcox/3wRtj/

Edit: I give no guarantees as to how fast it might converge or even how well an estimated derivative would work with respect to Newton's method. For the parameters I've tried given your function f(x) = a^(k^(bx)), it seems to work well, but I haven't tried much.

Edit II. Of course, the above jsFiddle also assumes only a single solution that we'd need to search for.

于 2013-02-03T00:01:36.860 回答