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.

Here is a toy problem:

I have a (directional) line that starts from {2,0} and goes through {1.8, 0.01}. Also I have a parametric plot defined like this: {Sin[u], Sin[2 u]} where 0 <= u <= 2 Pi.
I would like to find the intersection of these two.

I tried something like:

Solve[a*({1.8, .01} - {2, 0}) + {2, 0} == {Sin[u], Sin[2 u]} 
  && 0 <= u < 2 Pi && a > 0, {u, a}]

But both Solve and NSolve seem to struggle with this. Findroot is able to find an answer quickly but it gives me only one answer depending on the starting point while I'd like to have all correct answers, four in this case.

Also I see if I redefine the line section so that it has only one intersecting point with the curve (e.g. line starts at {0, 0} and goes through {-0.1 , 0.01}) then Solve is able to find that one answer comfortably!

Any idea how to solve this?

share|improve this question
    
Please include working code for both your line and the parametric plot. –  Mr.Wizard Jul 22 '13 at 9:40
    
@Mr.Wizard I think my question boils down to: how can I solve this Solve[a*({1.8, .01} - {2, 0}) + {2, 0} == {Sin[u], Sin[2 u]} && 0 <= u < 2 Pi && a > 0, {u, a}] –  user5131 Jul 22 '13 at 9:45

1 Answer 1

up vote 11 down vote accepted

At first, one should appropriately define the system of equations. Instead of machine precission numbers we prefer exact numbers therefore we would define:

{a, b} /. Solve[{ 9/5  a + b == 1/100, 2 a + b == 0}, {a, b}]
{{-(1/20), 1/10}}

Now

pts = {Sin[u], Sin[2 u]} /. Solve[{ x == Sin[u], 
                                    Sin[2 u] == -(1/20) x + 1/10, -2 Pi <= u <= 2 Pi},
                                  {u}, {x}, Reals]
{ {Sin[2 ArcTan[Root[1 - 41 #1 + 2 #1^2 + 39 #1^3 + #1^4 &, 1]]], 
   Sin[2 (2 Pi + 2 ArcTan[ Root[1 - 41 #1 + 2 #1^2 + 39 #1^3 + #1^4 &, 1]])]}, 
  {Sin[ 2 ArcTan[Root[1 - 41 #1 + 2 #1^2 + 39 #1^3 + #1^4 &, 2]]], 
   Sin[2 (2 Pi + 2 ArcTan[ Root[1 - 41 #1 + 2 #1^2 + 39 #1^3 + #1^4 &, 2]])]}, 
  {Sin[2 ArcTan[Root[1 - 41 #1 + 2 #1^2 + 39 #1^3 + #1^4 &, 3]]], 
   Sin[4 ArcTan[Root[1 - 41 #1 + 2 #1^2 + 39 #1^3 + #1^4 &, 3]]]}, 
  {Sin[2 ArcTan[Root[1 - 41 #1 + 2 #1^2 + 39 #1^3 + #1^4 &, 4]]], 
   Sin[4 ArcTan[Root[1 - 41 #1 + 2 #1^2 + 39 #1^3 + #1^4 &, 4]]]} }    
N @ %
{{-0.0513515, 0.102568}, {-0.997173, 0.149859}, 
 {0.0488373, 0.0975581}, {0.999687, 0.0500156}}
 ParametricPlot[{ {Sin[u], Sin[2 u]}, 
                  {u, (-(1/20) u + 1/10) ConditionalExpression[1, -5/4 <= u <= 5/4]}}, 
                {u, -2 Pi, 2 Pi}, 
                Epilog -> {Red, PointSize[0.017], Point[pts]}]

enter image description here

Edit

Your original system works as well if you add the specification domain: Reals (mind different variables here).

Solve[ a ({1.8, .01} - {2, 0}) + {2, 0} == {Sin[u], Sin[2 u]} && 
       0 <= u < 2 Pi && a > 0, {u, a}, Reals]
Solve::ratnz: Solve was unable to solve the system with inexact coefficients. 
The answer was obtained by solving a corresponding exact system 
and numericizing the result. >>

{{u -> 0.0488568, a -> 9.75581}, {u -> 1.54578, a -> 5.00156}, 
 {u -> 3.19297, a -> 10.2568}, {u -> 4.63718, a -> 14.9859}}   

alternatively substituting {1.8, .01} by {9/5, 1/100} yields appropriate results without adding domain specification, however in terms or radicals not explicitly real.

Even though 0 <= u < 2 Pi and a > 0 inequalities should restrict variables u and a to the real numbers, nonetheless most likely on the internal processing level there might appear some inconsistency. Therefore one should explicitly add the domain specification.

share|improve this answer
    
thank you. Adding 'Reals' solved my problem. –  user5131 Jul 22 '13 at 12:54

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.