0
$\begingroup$

I need to numerically integrate a differential equation and define two exclusion zones to stop the integration. The Mathematica code looks like this :

NDSolve[{
    x'[s] == Bx[s],
    y'[s] == By[s],
    z'[s] == Bz[s],

    x[0] == initialDataX,
    y[0] == initialDataY,
    z[0] == initialDataZ

}, {x, y, z}, {s, 0, 1000},
Method -> Automatic,
MaxSteps -> Automatic,
StoppingTest -> (???)]

Now, the integration must stop if the vector {x[s], y[s], x[s]} goes inside one of two spheres of radius 1 located at Source1 and Source2, respectively (the center of the spheres). Currently, I'm using this StoppingTest constraint :

Sqrt[({x[s], y[s], z[s]} - Source[1]).({x[s], y[s], z[s]} - Source[1])] < 1 ||
Sqrt[({x[s], y[s], z[s]} - Source[2]).({x[s], y[s], z[s]} - Source[2])] < 1 

That part is long, and I believe that it is clumsy.

What is the best way to define the StoppingTest zone in this case ?

$\endgroup$
4
  • 1
    $\begingroup$ For starters, it seems to me that the Sqrt's are superfluous. BTW note the existence of SquaredEuclideanDistance and EuclideanDistance $\endgroup$ Feb 7, 2016 at 21:01
  • 2
    $\begingroup$ Oops, just noticed V7 -- that was too long ago....You should probably mention that in the body of the question (assuming that's what the tag means). For newer versions, the comment would be: I'm unaware of any option named StoppingTest. Look up WhenEvent, paying particular attention to the action "StopIntegration". $\endgroup$
    – Michael E2
    Feb 7, 2016 at 21:06
  • $\begingroup$ Yep, I could remove the Sqrt. And EuclideanDistance also exists in version 7. $\endgroup$
    – Cham
    Feb 7, 2016 at 21:09
  • 1
    $\begingroup$ @bel The tag numerical-integration explicitly includes questions about NDSolve. $\endgroup$
    – Michael E2
    Feb 7, 2016 at 21:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.