Number crunching algorithm
I've run into a programming challenge that involves number crunching and I'm stumped on an equation. Here's what I have that works so far:
double y = (Math.sqrt(x) - 1/x) / 2;
It basically takes x as a given and computes y, producing a 2D curve. Where I'm stumped is I want to take y as a given and compute x to produce the identical curve. Is this possible? I've managed to reformulate the equation several different ways on paper, but haven't arrived at anything useful. (I'm kind of rusty on algebra.) Any help on how to go about this would be appreciated.
> I've run into a programming challenge that involves
> number crunching and I'm stumped on an equation.
> Here's what I have that works so far:
> > double y = (Math.sqrt(x) - 1/x) / 2;
>
> It basically takes x as a given and computes y,
> producing a 2D curve. Where I'm stumped is I want to
> take y as a given and compute x to produce the
> identical curve. Is this possible? I've managed to
> reformulate the equation several different ways on
> paper, but haven't arrived at anything useful. (I'm
> kind of rusty on algebra.) Any help on how to go about
> this would be appreciated.
let a = sqr(x), and rewrite your function:
y = (a- 1/a^2)/2
simple above, you get
2y = a - (1/a^2)
then
a = 2y + 1/a^2
then
a = ((2a^2)*y + 1)/a^2
then
a^3 = (2a^2)*y + 1
then
(2a^2)*y = a^3 - 1
therefore
y = (a^3 - 1)/2a^2
then substitute sqr(x) back, you get
y = (sqr(x)-1)/2
hmm...hope I have done everything correctly....
yue42 at 2007-7-7 0:58:03 >

oops... I am on crack, scratch that... me confused:)
yue42 at 2007-7-7 0:58:03 >

Difficult problem! Cheating, here's what matlab's symbolic toolbox gives me:
>> f=sym('y = (sqrt(x) - 1/x) / 2');
>> finv = solve(f, x)
finv =
[(1/6*(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)+8/3*y^2/(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)+2/3*y)^2]
[ (-1/12*(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)-4/3*y^2/(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)+2/3*y+1/2*i*3^(1/2)*(1/6*(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)-8/3*y^2/(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)))^2]
[ (-1/12*(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)-4/3*y^2/(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)+2/3*y-1/2*i*3^(1/2)*(1/6*(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)-8/3*y^2/(108+64*y^3+12*(81+96*y^3)^(1/2))^(1/3)))^2]
That's 3 separate equations, btw...
Good luck.
Algebra is fun:
y = (x^1/2 - x^-1 ) / 2
2y = x^1/2 - x^-1
2yx = x^3/2 - 1
(2yx + 1)^2/3 = x
4y^2x^2 + 4yx + 1 = x^3
x^3 - 1 = 4y^2x^2 + 4yx
x^3 - 4x^2y^2 - 4xy - 1 = 0 (now in cubic form and can be solved using the [url http://mathworld.wolfram.com/CubicFormula.html]closed cubic formula[/url] with a2=-4y^2, a1=-4y, a0=-1
Thanks. I experimented with java.awt.geom.CubicCurve2D to try to solve the equation for x using the solveCubic() method and it gives unexpected results--maybe I'm not ordering the coefficients right, I'll have to keep fiddling with it. It crunches them fairly fast I think; about a million per second on my four year old wintel laptop, so that's good at least.
> Thanks. I experimented with java.awt.geom.CubicCurve2D to try to solve the equation for x using the solveCubic() methodUnbelievable. I spent ages implementing a method to solve cubics last week, and it was lurking in the API all the time.
It's some consolation to discover that the CubicCurve2D implementation fails my unit test, so I didn't entirely waste my time.
As to ordering of coefficients: the documentation is far more confusing than it should be. To solve ax^3 + bx^2 + cx + d you call CubicCurve2D.solveCubic(new double[]{d, c, b, a});
Out of curiosity, how exactly does it fail?
Two things. Firstly, the answers it returns to some of my tests are outside the tolerances I specified (which my code meets). Secondly, it doesn't find all the roots. Take the case of x^3 + 467 x^2 - 16745 x + 144789That has two roots: -501 and 17 (twice). CubicCurve2D only finds -501.
I think I must be having the same problems with it. Maybe large coefficients cause it to fail?