# Multiple stopping constraints in NDSolve

I need to numerically solve several differential equations, with several constraints, like this :

Coordinates[s_] := {x[s], y[s], z[s]}

NDSolve[{
x'[s] == F[s],
y'[s] == G[s],
z'[s] == H[s],
x[0] == DataX,
y[0] == DataY,
z[0] == DataZ}, {x, y, z}, {s, 0, 500},
Method -> Automatic, MaxSteps -> Automatic,
StoppingTest -> (
EuclideanDistance[Coordinates[s], Source[1]] < 1 ||
EuclideanDistance[Coordinates[s], Source[2]] < 1 ||
EuclideanDistance[Coordinates[s], Source[3]] < 1 ||
...
EuclideanDistance[Coordinates[s], Source[NumSources]] < 1
)
]


Now, there are NumSources constraints of this type :

    EuclideanDistance[Coordinates[s], Source[k]] < 1


where k = 1, 2, 3, ..., NumSources.

How can I simplify that long part of constraints in the NDSolve defined above ?

This is probably basic, but I don't see how to simplify this.

UPDATE : Just a picture to show off. The magnetic field lines of an arbitrary number of magnetic dipoles (magnetized spheres) :

• Isn't this a duplicate of your previous question, mathematica.stackexchange.com/questions/105869/…? Commented Feb 9, 2016 at 14:46
• It is related to that question, but it isn't the same. Not a duplicate. Or maybe it is ? At first, I made that code just for 2 sources. Now, I want to generalize the code for an arbitrary number of sources.
– Cham
Commented Feb 9, 2016 at 14:49
• The first question was about some aspects for just 2 constraints. Now, I'm trying to generalize the code for an arbitrary number of constraints. Maybe it should be in the same question.
– Cham
Commented Feb 9, 2016 at 14:51
• On a practical note, I don't suppose you can upgrade? My feeling is that the differential equations folks no longer have access to V7, and things have changed so much that no one can test any ideas they might have. Commented Feb 9, 2016 at 14:53
• So can you answer your previous question, then? Your solution might help others suggest one to this question. Commented Feb 9, 2016 at 14:54

StoppingTest ->
(Apply[
Or,
Table[EuclideanDistance[Coordinates[s], Source[i]] < 1, {i,1,NumSources}],
{0}])

• It doesn't appear to work well. I'm getting lots of warning messages, and the output is all messy. Is this compatible with Mma version 7 ? And why the {0} in the Apply ?
– Cham
Commented Feb 9, 2016 at 14:47
• The {0} specifies the level. You can omit it (I copied and pasted it from another code). Despite the fact that the Apply command is updated in Mma version 10.3 it should work in this context in Mma 7. Have you defined the parameter s within Coordinates. Maybe s is a table and thus you have to give it as s[[i]]
– demm
Commented Feb 9, 2016 at 15:02
• Oh boy ! I just found the source of my problem ! This was trivial ! My bad ! Your suggestion appears to be working great now !
– Cham
Commented Feb 9, 2016 at 15:05