solving an equation for the square of the variable?
Given a expression (polynomial, or any equation in general) such as
a s^2+b = 0
I want to solve for s^2, to get s^2 = -b/a. We all know that one can't just write
Solve[eq==0,s^2]
because s^2 is not a 'variable开发者_运维百科'. only s is a 'variable'. So what I do is
eq = a s^2+b;
sol = First@Solve[eq==0/.s^2->z,z];
z/.sol
-(b/a)
I was wondering if there is a way to do the above, without the intermediate variable substitution? I tried many commands, but no success (reduce, collect, eliminate, factor. etc...).
thanks --Nasser
One way is to solve for s
and then square it...
eq=a s^2+b;
sol=#^2 &@ (s/.Solve[eq==0,s])//DeleteDuplicates
Out[1]= {-(b/a)}
You could use the Notation package, but it leads to other issues. So here is your original equation:
In[1]:= Solve[b + a s^2 == 0, s^2]
During evaluation of In[1]:= Solve::ivar: s^2 is not a valid variable. >>
Out[1]= Solve[b + a s^2 == 0, s^2]
Now Symbolize
s^2 so that the normal Mathematica evaluator treats it like any other symbol
In[2]:= Needs["Notation`"]
In[3]:= Symbolize[ParsedBoxWrapper[SuperscriptBox["s", "2"]]]
In[4]:= Solve[b + a s^2 == 0, s^2]
Out[4]= {{s^2 -> -(b/a)}}
The problem is that s^2 really is treated as just another symbol, eg
In[6]:= Sqrt[s^2] // PowerExpand
Out[6]= Sqrt[s^2]
A work around is to replace s^2 with s*s, since Symbolize
only acts on user inputed expressions (ie at the level of interpreting inputted Box structures)
In[7]:= Sqrt[s^2] /. s^2 -> s s // PowerExpand
Out[7]= s
精彩评论