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
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
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.
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.
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.
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
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 ;
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]
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;
(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)
As others have stated, your question is not much clear. There are two ways to solve an equation programmatically:
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.