Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to solve two equations that take the form:

$W_0 \sqrt{1+\left(\dfrac{z}{\lambda\,\pi\, W_{0}^2}\right)}=W_1\;\,\quad \ldots(1)$

$W_0\sqrt{1+\left( \dfrac{z+0.1}{\lambda\, \pi\, W_{0}^2}\right)}=W_2\quad\;\, \ldots(2)$

The quantities $\lambda, W_1, \text{ and } W_2$ are known parameters. I am trying to solve for the unknowns $W_0\text{ and } z$. How would I go about doing this. The result is to be in the $\mathbb{R}$. My attempts are below:

Attempt 1:

Solve[{W Sqrt[1+(z/(\[Lambda] \[Pi] W^2))^2]==A,W Sqrt[1+((z+0.1)/(\[Lambda] \[Pi]
W^2))^2]==B},{W,z}]

Attempt 2:

FindInstance[W Sqrt[1+(z/(\[Lambda] \[Pi] W^2))^2]==A&&W Sqrt[1+((z+0.1)/(\[Lambda] 
\[Pi] W^2))^2]==B,{W,z},Complexes]

Attempt 3:

FindRoot[W Sqrt[1+(z/(\[Lambda] \[Pi] W^2))^2]==A,W Sqrt[1+((z+0.1)/(\[Lambda] \[Pi] 
W^2))^2]==B,{{W,1.03 10^-3},{z,4.0 10^-1}}]

My question is what is the best method to solve equations such as these and what will yield the best numerical or analytical result.

share|improve this question
2  
If you are trying to do this symbolically, remember to avoid introducing approximate numbers--change 0.1 to 1/10, for example. The result of Solve[{W Sqrt[1 + (z/(λ π W^2))^2] == A, W Sqrt[1 + ((z + 1/10)/(λ π W^2))^2] == B}, {W, z}] // FullSimplify is IMO quite nice. –  Oleksandr R. Sep 23 '12 at 0:21
1  
If you check your code then W (or W_0 in TeX) comes out to be at the power 4 under square root. In your TeX code it is at the power 2. –  Vitaliy Kaurov Sep 23 '12 at 0:39
1  
@OleksandrR. I've heard this before: changing 0.1 to 1/10. Why is that? –  drN Sep 23 '12 at 18:15
1  
@drN: halirutan's answer explains why. –  J. M. Sep 30 '12 at 23:38
add comment

1 Answer

up vote 8 down vote accepted

All you need is already mentioned in the comment. Let me give you some more information, how you could have prevented your mistake. When I solve something, I almost always try to get an analytic solution first, because then you not only having an answer, but you can investigate under which circumstances your solution is true.

What you have to understand first, that it is crucial in Mathematica, to give problems in the right form: And here I mean, the right form of numeric quantities. You have 3 types of numeric quantities in Mathematica:

  • Analytic (exact) values like 1,100,Exp[2],Pi
  • Machine numbers 1.0, Exp[2.0], N[Pi]
  • Arbitrary-precision numbers 1.0`10, N[Exp[2], 10]

Please read the Numerical Precision carefully to understand the differences. The first thing to know is, that whenever you use exact values only, Mathematica will never do a computation where you lose information. It will only do mathematical correct transformations. See the difference

Exp[I*Pi]
(*
  Out[17]= -1
*)

Exp[I*Pi + 0.0]
(*
  -1. + 1.22465*10^-16 I
*)

In the last example I forced Mathematica to leave the save field of exact evaluation by using 0.0. This told Mathematica to solve the expression numerically introducing some roundoff error.

And now you know, why you don't want to use 0.1 in your equation. This forces an numeric evaluation which gives you only numeric output instead of analytic.

Therefore, replace the 0.1 by 1/10 and use Solve or Reduce.

FullSimplify[
 Solve[{W Sqrt[1 + (z/(\[Lambda] \[Pi] W^2))^2] == A, 
   W Sqrt[1 + ((z + 1/10)/(\[Lambda] \[Pi] W^2))^2] == B}, {W, z}]
 ]

If you try Reduce, you may have to wait half an hour and you'll get output which takes 10 sheets of paper, but you get conditions which have to hold for an particular solution. Maybe not for this, but for smaller problems this can be very insightful.

Reduce[{W Sqrt[1 + (z/(\[Lambda] \[Pi] W^2))^2] == A, 
  W Sqrt[1 + ((z + 1/10)/(\[Lambda] \[Pi] W^2))^2] == B}, {W, z}, Reals]
share|improve this answer
2  
Great detailed answer on this topic –  kale Sep 23 '12 at 3:46
1  
Excellent! This was a great response I was looking for, and has also shed some light on me for going about solving equations more efficiently as to the type of solution I want out of it. +1... Thanks. –  night owl Sep 30 '12 at 21:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.