Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

math challenge

Status
Not open for further replies.

petey

Programmer
Mar 25, 2001
383
0
0
US
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:
Code:
double y = (Math.sqrt(x) - 1/x) / 2;
It basically takes x as a given and computes y resulting in a particuar 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.) How would one go about this? Any tips or links would be appreciated.

Thanks!

 
I don't think you can invert that function analytically as it has a singularity at x=0. If you graph the function from, say, x=1 to 100, it looks an awful lot like a hyperbolic function of some kind but I don't suppose that helps.

Bob Rashkin
rrashkin@csc.com
 
It's a long time ago, so I may be wrong...

isn't Math.sqrt(x) = x^(1/2) ?
and
isn't 1/x = x^(-1) ?

That would give us
y = (x^(1/2) - x^(-1)) /2
2y = x^(1/2) - x^(-1)

I don't know how to reduce the right term further...



seeking a job as java-programmer in Berlin:
 
Thanks, all. I'll have to read up on what a singularity is, I'm picturing a supermassive black hole sucking all the numbers into an alternate dimension. The equation is actually an average between a negative inverse proportion and a square root, hence the divide by two part. Anyway I might try to approach this problem from a different angle, since it might not be as simple as I first thought.
 
It's the same thing really. A singularity in math is where something goes to infinity at a point. In a black hole, it's the density. In your equation, it's "y".

Bob Rashkin
rrashkin@csc.com
 
Trial and error to the rescue!

Given:
y is a global variable (double) and the value you are trying to come up with.
desiredError is a global variable (double) and the acceptable amount you're willing to be off (e.g., 0.00001).
The function is first called as

Code:
double x = solveForX(y*y*4);

...and solveForX(...) is:

Code:
private double solveForX(double trial)
{
 double trialSol = (Math.sqrt(trial) - 1/trial) / 2;
 double trialError = y - trialSol;
 double absError = trialError;
 if(absError < 0)
  absError *= -1; //absolute value of error

 if(absError <= desiredError) //close enough!
 {
  return trial;
 }
 else
 {
  return solveForX((y+trialError)*(y+trialError)*4);
 }
}

Give it a go!

--Dave
 
Based on further research I've learned this resolves to a cubic equation that can be solved with an algorithm known as the cubic formula, which produces multiple roots (solutions for x that satisfy the equation). java.awt.geom.CubicCurve2D has a static method dedicated to solving these types of formulas. Anyway thanks for the input everybody.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top