Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 231
Mike Bailey Avatar asked May 27 '10 23:05

Mike Bailey


People also ask

How do you type square root in Mathematica?

√z can also be used for input. The √ character is entered as sqrt or \[Sqrt].

How do you square root in Matlab?

B = sqrt( X ) returns the square root of each element of the array X . For the elements of X that are negative or complex, sqrt(X) produces complex results.


2 Answers

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]}}
like image 149
Janus Avatar answered Oct 21 '22 19:10

Janus


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}}
like image 31
Scott Morrison Avatar answered Oct 21 '22 19:10

Scott Morrison