tags:

views:

769

answers:

9

Solve this equation for x, (1 + x)^4=34.5 . I am interested in the math libraries you'd use.

the equation is MUCH SIMPLER (1 + x)^4=34.5

thanks

+2  A: 

Solving something like that in C isn't going to be much different from solving it by hand; using a system more suited to doing symbolic math (Mathematica?) is probably easier. A similar question was asked recently.

Carl Norum
I missed this: I see that there are mentions other CAS libraries.
MaD70
+3  A: 

You mean numerically solve? I would use C runtime with "math.h" because Newton–Raphson is straightforward to implement. Actually, you should state the requirements, such as acceptable error magnitude, performance, etc... then library choice would be narrowed.

AlexKR
+5  A: 

It depends on what you mean by "solve".

If you mean "find a value for double x that satisfies the equation to the limits of this machine's floating point accuracy" then Luiscencio's approach is fine.

If by solve you mean "find an equation of the form 'x = ' such that x satisfies the given equation" (AKA "solve algebraically") then neither C nor C++ has libraries that will help. As Carl noted, you'd either have to do it by hand or use Mathematica or a similar symbolic math package to do it.

If you mean something different from either of those, ask again with more detail.

Berry
you contradict yourself: "neither C nor C++ has libraries that will help." -> "or a similar symbolic math package". the latter is the correct answer if you need to do it programmaticly.
rmeador
Generally such "similar symbolic math packages" are not c/c++ libraries. They are stand-alone applications.
Alan
Alan correctly stated what I meant. So-called "math libraries" that are part of a language are usually really for arithmetic and various functions like square root, logs, trig and so on. Symbolic solving usually not supported (I'd say never but someone would be sure to prove me wrong with some weird academic language)
Berry
+5  A: 

approximate x*(x+a)^b=c

You'll need a more robust solution for more complex polynomials, but this may be good enough to finish your homework.

This algorithm uses Newton's Method and is written in Ruby. You can verify that the derivative and answer is correct using wolfram|alpha.

def f(x,a,b,c)
 return x*(x+a)**b-c
end

def df(x,a,b,c)
 return (x+a)**b+b*x*(x+a)**(b-1)
end

def newton(a,b,c)
 xn=0   #initial seed for Newton's method

 while true
  xn2=xn-f(xn,a,b,c)/df(xn,a,b,c)  #Newton's method
  print "f(%.5f)=%.5f\n"%[xn,f(xn,a,b,c)]  
  break if (xn2*10000).to_i==(xn*10000).to_i #set desired precision here
  xn=xn2
 end
 print "root is %.5f"%[xn2]
end

newton(1,4,34.5)

this produces:

f(0.00000)=-34.50000
f(34.50000)=54793902.65625
f(27.44093)=17954483.09402
f(21.79391)=5883122.74717
f(17.27661)=1927672.51373
f(13.66318)=631598.66717
f(10.77301)=206926.07160
f(8.46171)=67782.26596
f(6.61400)=22194.34671
f(5.13819)=7259.61867
f(3.96214)=2367.67791
f(3.03097)=765.73665
f(2.30728)=241.54928
f(1.77466)=70.68568
f(1.43951)=16.48341
f(1.30101)=1.97186
f(1.27945)=0.04145
f(1.27897)=0.00002
root is 1.27897
avguchenko
nice work, and cool link to wolfram alpha
jrhicks
horrible, no one speaks python here...
vehomzzz
it's Ruby, Andrei.
avguchenko
1.27 does not check out. (1+1.27)^4 = 26.55. WTF?
abelenky
this solution is for x*(x+1)^4=34.5. the question was changed/clarified after i posted this. see my other answer for solution to (x+1)^4=34.5
avguchenko
+4  A: 

I am assuming that this question has been drastically changed since others answered, because the solution is a trivial rearrangement of the equation:

x = 34.5^(1/4) - 1

in code:

double x = pow( 34.5, 1.0/4.0 ) - 1 ;
Clifford
it is generally undesirable to take roots of a factor containing the variable because you may lose solutions. in this specific example, the polynomial has two solutions but your method finds only one.
avguchenko
@avguchenko: To be pedantic about it, there are two real solutions and two complex solutions.
Pillsy
Agreed. The question (presumably after editing) just says the question is 'much simpler' (only shouted). Much simpler than what I wonder? And why is it now much simpler? This is a 'much simpler' answer ;)
Clifford
+1  A: 

this is for the simpler function. it also has multiple seeds to make sure we find all roots.

# solve (x+a)^b=c
def f(x,a,b,c)
  return (x+a)**b-c
end

def df(x,a,b,c)
  return b*(x+a)**(b-1)
end

def newton(a,b,c)
  roots=[]
  for seed in [-100000, -100, -1,1,100, 100000] # set initial guesses here
    print "\n    with seed %d\n"%[seed]
    root=newton_root(seed,a,b,c)
    if root and not roots.include?(root)
      roots << root
    end
  end
  return roots
end

def newton_root(xn,a,b,c)
  while true
    if (df(xn,a,b,c)).abs<0.000001  # give up with this seed if derivative is too low
      print "    gave up on this seed\n"
      return nil
    end

    xn2=xn-f(xn,a,b,c)/df(xn,a,b,c)  
    #  print "    f(%.5f)=%.5f\n"%[xn,f(xn,a,b,c), xn2]  

    if (xn2*10000).to_i==(xn*10000).to_i # set precision here
      rounded_xn=(xn2*10000).to_i/10000.0
      print "        found root %0.5f\n"%[rounded_xn]
      return rounded_xn
    else
      xn=xn2
    end
  end
end


print newton(1,4,34.5).inspect

this produces:

with seed -100000
    found root -3.42350

with seed -100
    found root -3.42350

with seed -1
gave up on this seed

with seed 1
    found root 1.42350

with seed 100
    found root 1.42350

with seed 100000
    found root 1.42350

[-3.4235, 1.4235]

avguchenko
+1  A: 

x1 = 34.5^(1/4) - 1
x2 = -34.5^(1/4) - 1

// #include <math.h>
double x1 = sqrt(sqrt(34.5)) - 1;
double x2 = -sqrt(sqrt(34.5)) - 1;

Sergey
Those are the two real solutions, but there's a complex conjugate in there as well.
duffymo
+1  A: 

(1 + x)^4=34.5

(1+x)^2 = sqrt(34.5) = +/- 5.87367

1+x = sqrt(sqrt(34.5)) = +/- 2.42357

x = 1.423557 and x = -3.42357

Verify:

(1 + 1.423557)^4 = 34.4995 (checked)

(1 + -3.42357)^4 = 34.500 (checked)

abelenky
You forgot -1 + 2.42357i and -1 - 2.42357i
mobrule
A simpler and direct method of finding the 4th root is simply pow(34.5, 0.25)
Clifford
+2  A: 

As others have stated, your question is not much clear. There are two ways to solve an equation programmatically:

  1. numerically or
  2. symbolically.

Methods of the first kind are the subject of numerical analysis.

Methods of the second kind are developed for software called Computer Algebra Systems (CASs). There is at least one library in C++ developed for this purpose, GiNaC.

Also, as Carl Norum mentioned, a similar question was asked recently where others CAS libraries are cited in answers.

MaD70
apparently I can't edit the links in either...
Jimmy
I earned some reputation, so now I can. Thank you anyway and thanks for the badge.
MaD70