1
$\begingroup$

today i was writing a function for my project using mathematica that gives a line equation in form of $y=mx+b$ and a circle equation in form of $x^2+y^2=r^2$ and finds the intersection point which is on the left side of circle($x<0$)

intersectionPoint[eqLine_, eqCircle_] :=
Module[{eq1 = eqLine, eq2 = eqCircle, pts, x1, x2, a},
    pts = NSolve[eq1 && eq2, {x, y}];
    x1 = x /. pts[[1, 1]];
    x2 = x /. pts[[2, 1]];
    if[x1 < 0, a = pts[[1]], a = pts[[2]]];
    a
 ];

So for a input like this

b = intersectionPoint[y == 0*x + 4, x^2 + y^2 == 25]

I expect b should be equal to this

{x -> -3., y -> 4.}

but instead it returns

{x -> 3., y -> 4.}

I read the wolfram If reference and in Generalizations part there was something like this

In[1]:=x = -2;
If[x < 0, y = -x, y = x]; y
Out[1]= 2

I can't understand why my function does not give me my expected result.

$\endgroup$

1 Answer 1

0
$\begingroup$

I found a couple typos in your code, the most glaring being the lower-case if. This seems to work,

intersectionPoint[eqLine_, eqCircle_] := 
  Module[{eq1 = eqLine, eq2 = eqCircle, pts, x1, x2, a},
         pts = NSolve[eq1 && eq2, {x, y}];
         x1 = x /. pts[[1]];
         x2 = x /. pts[[2]];
         If[x1 < 0, a = pts[[1]], a = pts[[2]]];
         a];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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