How can I solve symbolically a polynomial equation in Mathematica if the coefficient of the highest power might be zero? - wolfram-mathematica

Assume, I would like to solve the following equation:
ax^2+bx+c=0
with a, b and c being parameters later to be determined, a might be even 0.
My try was the following:
In[1]:= sol = Solve[a x^2 + b x + c == 0, x]
Out[1]= {{x -> (-b - Sqrt[b^2 - 4 a c])/(2 a)}, {x -> (-b + Sqrt[b^2 - 4 a c])/(2 a)}}
Now if I would like to get the solution for the case when a=0, I get an error message:
In[2]:= sol /. {a -> 0, b -> 1, c -> 2}
During evaluation of In[2]:= Power::infy: Infinite expression 1/0 encountered.
During evaluation of In[2]:= Power::infy: Infinite expression 1/0 encountered.
During evaluation of In[2]:= Infinity::indet: Indeterminate expression 0 ComplexInfinity encountered.
Out[2]= {{x -> ComplexInfinity}, {x -> Indeterminate}}
Actually I have to solve a cubic equation, I wrote a quadratic one for the sake of simplicity.

Related

Mathematica FullSimplify[Sqrt[5+2 Sqrt[6]]] yields Sqrt[2]+Sqrt[3] but FullSimplify[-Sqrt[5+2 Sqrt[6]]] is not simplified, why?

I was playing with the (beautiful) polynomial x^4 - 10x^2 + 1.
Look what happens:
In[46]:= f[x_] := x^4 - 10x^2 + 1
a = Sqrt[2];
b = Sqrt[3];
Simplify[f[ a + b]]
Simplify[f[ a - b]]
Simplify[f[-a + b]]
Simplify[f[-a - b]]
Out[49]= 0
Out[50]= 0
Out[51]= 0
Out[52]= 0
In[53]:= Solve[f[x] == 0, x]
Out[53]= {{x->-Sqrt[5-2 Sqrt[6]]},{x->Sqrt[5-2 Sqrt[6]]},{x->-Sqrt[5+2 Sqrt[6]]},{x->Sqrt[5+2 Sqrt[6]]}}
In[54]:= Simplify[Solve[f[x] == 0, x]]
Out[54]= {{x->-Sqrt[5-2 Sqrt[6]]},{x->Sqrt[5-2 Sqrt[6]]},{x->-Sqrt[5+2 Sqrt[6]]},{x->Sqrt[5+2 Sqrt[6]]}}
In[55]:= FullSimplify[Solve[f[x] == 0, x]]
Out[55]= {{x->Sqrt[2]-Sqrt[3]},{x->Sqrt[5-2 Sqrt[6]]},{x->-Sqrt[5+2 Sqrt[6]]},{x->Sqrt[2]+Sqrt[3]}}
Sqrt[5-2 Sqrt[6]] is equal to Sqrt[3]-Sqrt[2].
However, Mathematica's FullSimplify does not simplify Sqrt[5-2 Sqrt[6]].
Question: Should I use other more specialized functions to algebraically solve the equation? If so, which one?
Indeed, Solve doesn't simplify all roots to the max:
A FullSimplify postprocessing step simplifies two roots and leaves two others untouched:
Same initially happens with Roots:
Strange enough, now FullSimplify simplifies all roots:
The reason for this is, I assume, that for the default ComplexityFunction some of the solutions written above in nested radicals are in a sense simpler than the others.
BTW FunctionExpand knows how to deal with those radicals:
FullSimplify[ Solve[x^4-10x^2+1==0,x]
,
ComplexityFunction ->
(StringLength[ToString[
InputForm[#1]]] & )]
gives
{{x -> Sqrt[2] - Sqrt[3]}, {x -> -Sqrt[2] + Sqrt[3]}, {x -> -Sqrt[2] -
Sqrt[3]}, {x -> Sqrt[2] + Sqrt[3]}}

Using the output of Solve

I had a math problem I solved like this:
In[1]:= Solve[2x(a-x)==0, x]
Out[1]= {{x->0}, {x->a}}
In[2]:= Integrate[2x(a-x), {x,0,a}]
Out[2]= (a^3)/3
In[3]:= Solve[(a^3)/3==a, a]
Out[3]= {{a->0}, {a->-Sqrt[3]}, {a->Sqrt[3]}}
My question is if I could rewrite this to compute it in one step, rather than having to manually input the result from the previous line. I could easily replace the integral used in step three with the Integrate command from step two. But what I can't figure out is how I would use the result from step 1 as the limits of integration in the integral.
You could combine step 1 and 2 by doing something like
Integrate[2 x (a - x), {x, ##}] & ## (x /. Solve[2 x (a - x) == 0, x]);
If you agree to delegate the choice of the (positive oriented) domain to Integrate, by means of using Clip or Boole:
In[77]:= Solve[
Integrate[
Clip[2 x (a - x), {0, Infinity}], {x, -Infinity, Infinity}] == a, a]
Out[77]= {{a -> 0}, {a -> Sqrt[3]}}
or
In[81]:= Solve[
Integrate[
2 x (a - x) Boole[2 x (a - x) > 0], {x, -Infinity, Infinity}] ==
a, a]
Out[81]= {{a -> 0}, {a -> Sqrt[3]}}
The reason only non-negative roots are found, is that Integrate will integrate from the smallest root to the largest root, i.e. from {x,0,a} for positive a and {x,a,0} for negative a.

Mathematic D and Dt not behaving properly?

The derivative functions D and Dt don't appear to be functioning as advertised.
Following the first example in the "Properties and Relations" section of http://reference.wolfram.com/mathematica/ref/Constants.html I have:
In[1]:= {Dt[ax^2 + b, x, Constants -> {a, b}], D[ax^2 + b, x]}
Out[1]= {2 ax Dt[ax, x, Constants -> {a, b}], 0}
I've duplicated the input, but the output is totally different. How do I get the expected output { 2 a x, 2 a x}?
I am using Mathematica 8.0.1.0 64-bit as installed at Rutgers University.
You need a space between a and x, otherwise it thinks you're talking about a variable named ax:
In[2]:= {Dt[a x^2 + b, x, Constants -> {a, b}], D[a x^2 + b, x]}
Out[2]= {2 a x, 2 a x}
(I realize this isn't really answering the OP's question. But given the level of the question, along with OP's desire to use the Contants option, the following info may prove useful for others in the future.)
My 2 cents on Dt.
IMO, using the Constants option is less than ideal---mainly because it produces messy output. For example:
In[1]:= Dt[x^a y^b, Constants -> {a, b}]
Out[1]= a x^(-1 + a) y^b Dt[x, Constants -> {a, b}] +
b x^a y^(-1 + b) Dt[y, Constants -> {a, b}]
Am I the only one who finds the above behavior annoying/redundant? Is there a practical reason for this design? If so, please educate me... :)
Alternative approaches:
If you don't want to use the Constants option, here are some alternative approaches.
Use UpValues to force constants.
In[2]:= Remove[a, b];
a /: Dt[a] = 0;
b /: Dt[b] = 0;
Dt[x^a y^b]
Out[5]= a x^(-1 + a) y^b Dt[x] + b x^a y^(-1 + b) Dt[y]
Use Attributes. (i.e., give certain symbols the Constant Attribute.
In[6]:= Remove[a, b];
SetAttributes[{a, b}, Constant];
Dt[x^a y^b]
Out[8]= a x^(-1 + a) y^b Dt[x] + b x^a y^(-1 + b) Dt[y]
Use Rules to alter the output of the main Dt[] expression.
In[9]:= Remove[a, b];
Dt[x^a y^b] /. Dt[a] -> 0 /. Dt[b] -> 0
Out[10]= a x^(-1 + a) y^b Dt[x] + b x^a y^(-1 + b) Dt[y]

Finding the Fixed Points of an Iterative Map

I need to find fixed points of iterative map x[n] == 1/2 x[n-1]^2 - Mu.
My approach:
Subscript[g, n_ ][Mu_, x_] := Nest[0.5 * x^2 - Mu, x, n]
fixedPoints[n_] := Solve[Subscript[g, n][Mu, x] == x, x]
Plot[
Evaluate[{x,
Table[Subscript[g, 1][Mu, x], {Mu, 0.5, 4, 0.5}]}
], {x, 0, 0.5}, Frame -> True]
I'll change notation slightly (mostly so I myself can understand it). You might want something like this.
y[n_, mu_, x_] := Nest[#^2/2 - mu &, x, n]
fixedPoints[n_] := Solve[y[n, mu, x] == x, x]
The salient feature is that the "function" being nested now really is a function, in correct format.
Example:
fixedPoints[2]
Out[18]= {{x -> -1 - Sqrt[-3 + 2*mu]},
{x -> -1 + Sqrt[-3 + 2*mu]},
{x -> 1 - Sqrt[ 1 + 2*mu]},
{x -> 1 + Sqrt[ 1 + 2*mu]}}
Daniel Lichtblau
First of all, there is an error in your approach. Nest takes a pure function. Also I would use exact input, i.e. 1/2 instead of 0.5 since Solve is a symbolic rather than numeric solver.
Subscript[g, n_Integer][Mu_, x_] := Nest[Function[z, 1/2 z^2 - Mu], x, n]
Then
In[17]:= fixedPoints[1]
Out[17]= {{x -> 1 - Sqrt[1 + 2 Mu]}, {x -> 1 + Sqrt[1 + 2 Mu]}}
A side note:
Look what happens when you start very near to a fixed point (weird :) :
f[z_, Mu_, n_] := Abs[N#Nest[1/2 #^2 - Mu &, z, n] - z]
g[mu_] := f[1 + Sqrt[1 + 2*mu] - mu 10^-8, mu, 10^4]
Plot[g[mu], {mu, 0, 3}, PlotRange -> {0, 7}]
Edit
In fact, it seems you have an autosimilar structure there:

Take positive square root in Mathematica

I'm currently doing some normalization along the lines of:
J = Integrate[Psi[x, 0]^2, {x, 0, a}]
sol = Solve[J == 1, A]
A /. sol
For this type of normalization, the negative square root is extraneous. The result of this calculation is:
In[49]:= J = Integrate[Psi[x, 0]^2, {x, 0, a}]
Out[49]= 2 A^2
In[68]:= sol = Solve[J == 1, A]
Out[68]= {{A -> -(1/Sqrt[2])}, {A -> 1/Sqrt[2]}}
Even if I try giving it an Assuming[...] or Simplify[...], it still gives me the same results:
In[69]:= sol = Assuming[A > 0, Solve[J == 1, A]]
Out[69]= {{A -> -(1/Sqrt[2])}, {A -> 1/Sqrt[2]}}
In[70]:= sol = FullSimplify[Solve[J == 1, A], A > 0]
Out[70]= {{A -> -(1/Sqrt[2])}, {A -> 1/Sqrt[2]}}
Can anyone tell me what I'm doing wrong here?
I'm running Mathematica 7 on Windows 7 64-bit.
ToRules does what the box says: converts equations (as in Reduce output) to rules. In your case:
In[1]:= ToRules[Reduce[{x^2==1,x>0},x]]
Out[1]= {x->1}
In[2]:= {ToRules[Reduce[{x^2==1},x]]}
Out[2]= {{x->-1},{x->1}}
For more complex cases, I have often found it useful to just check the value of the symbolic solutions after pluging in typical parameter values. This is not foolproof, of course, but if you know there is one and only one solution then it is a simple and efficient method:
Solve[x^2==someparameter,x]
Select[%,((x/.#)/.{someparameter-> 0.1})>0&]
Out[3]= {{x->-Sqrt[someparameter]},{x->Sqrt[someparameter]}}
Out[4]= {{x->Sqrt[someparameter]}}
Solve doesn't work like this. You might try Reduce, instead, e.g.
In[1]:= Reduce[{x^2 == 1, x > 0}, x]
Out[1]= x == 1
It's then a little tricky to transform this output to replacement rules, at least in the general case, because Reduce might use arbitrary many logical connectives. In this case, we could just hack:
In[2]:= Solve[Reduce[{x^2 == 1, x > 0}, x], x]
Out[2]= {{x->1}}

Resources