# How to implement an "if an error occurs"-condition in Mathematica programming?

In general Mathematica may return a value even if NDSolve has run into error.

A simple example is

s = NDSolve[{y'[x] == Sin[x], y == 0}, y, {x, 0, 1}, WorkingPrecision -> 60]
Print[First[y /. s]]


which returns a value for y along with an error

NDSolve::nderr: Error test failure at x == 4.832029910269428300830401992665537536870551202296365402622378790473503860.*^-7; unable to continue.

Now suppose that Mathematica is asked to loop over $$10^4$$ values of y to find $$10^4$$ y via NDSolve and then append {y, y} to some .txt file.

There are some different ways to achieve this but in any case one is facing the following question: from all the y's that are in my .txt in the end of the day (or in the next morning) which can I trust?

A brute-force approach is to open whatever .nb file you used, check the warnings one by one and then manually delete the corresponding values from the .txt file.

Another brute-force approach is not to loop over NDSolve at all and repeat manually the calculations for all $$10^4$$ y's

However it would be better if one had some variable e.g. error initiated as error=0 at the beggining of each loop and then coined unity i.e. error=1 when a warning occurs. If this value is then appended to the .txt along with y and y then one can erase any untrustworthy value with simple list manipulation.

In this case the above simplistic example should look like

error=0;
s = NDSolve[{y'[x] == Sin[x], y == 0}, y, {x, 0, 1}, WorkingPrecision -> 60]
Print[First[y /. s]]
If[(...),{error=1}];


So the question is: what should be the condition in (...)? How can one implement the condition "if an arror occurs" in Mathematica programming?

• Have you looked at the function Check? Jun 18 '20 at 12:47
• I tried Check[s = NDSolve[{y'[x] == Sin[x], y == 0}, y, {x, 0, 1}, WorkingPrecision -> 10], err] which shows no error. However Check does not set err to unity. So probably I'd rather ask how to implement Check to NDSolve?
– user67126
Jun 18 '20 at 12:54
• The basic syntax is Check[expr, failexpr] where failexpr is evaluated and returned if an error has occurred. In your case you could try Check[s = ..., error=1] if you want to use If. However I do not recommend this practice. You should perhaps have a look at Throw and Catch. Jun 18 '20 at 12:58
• There are several standard practices, a couple of which are mentioned in the comments above Jun 18 '20 at 13:07

One way

SetDirectory[NotebookDirectory[]];
ode1 = {y'[x] == Sin[x], y == 0};
ode2 = {y'[x] == x, y == 0};
ode3 = {y'[x] == Cos[x], y == 0};
odes = {ode1, ode2, ode3}
Do[
Print["solving ode number ", n];

s = Quiet@Check[
NDSolve[odes[[n]], y, {x, 0, 1}, WorkingPrecision -> 60]
,
Null,
NDSolve::nderr (*you can add more error code here*)
(*Or do not use this field at all to catch any error*)
];

If[s === Null,
Print["Skipping n=", n, " because error was detected"]
,
Print["good n=", n];
Print[First[y /. s]] (*change this to write to file *)
]
, {n, 1, Length@odes}] So basically, you write to the file, only when s is not Null, which means no error messages were generated.

You can customize this as needed.

• Thanks for your time Nasser! Thanks a lot!
– user67126
Jun 18 '20 at 13:14
• Is there any bibliography on how to loop over NDSolve? I really had no idea.
– user67126
Jun 18 '20 at 13:18
• You could make one big list of all your odes? and just iterate over them as above. You could arrange them in an association also. One field for the ode, second field for the initial conditions, third field for the dependent variable, and so on. Then once the list is made, just run the Do loop over them. Jun 18 '20 at 13:20
• Check out ParametricNDSolve`
– chuy
Jun 18 '20 at 15:55