0
$\begingroup$

Is there any way to copy a number from an error message and assign it to a variable, without manually doing it?

For instance, I am trying to solve some coupled Differential equations as follows:

H[a_] := Sqrt[1/(3 10^18) (B[a] 10^(-3 a) + R[a] 10^(-4 a) )] ;
eqn1 = M'[a] + 1/H[a] (1/M[a])^2;
eqn2 = R'[a] + 10^a/M[a] M'[a] B[a];
eqn3 = B'[a] - 1/M[a] M'[a] B[a];
sol1 = NDSolve[{eqn1 == 0, eqn2 == 0, eqn3 == 0, M[0] == 100, 
   R[0] == 10^50, B[0] == 10^47}, {M, R, B}, {a, 0, 15}]

I am getting an error message (although the equations are getting solved upto a~13.5) as :

NDSolve::ndsz: At a == 13.536140129488167`, step size is effectively zero; singularity or stiff system suspected.

I want to extract the number 13.536140129488167 from the error message and assign it to variable, without manually copying and pasting it.

I need to do this since I have to run the code in some loop or vary some other parameters.

$\endgroup$
5
  • 1
    $\begingroup$ Michael E2's solution here works well, doesn't it?: (M["Domain"] /. First[sol1])[[1]] $\endgroup$
    – xzczd
    Commented Mar 28, 2021 at 12:25
  • 1
    $\begingroup$ If you don't know what M["Domain"] means, check these: mathematica.stackexchange.com/q/19042/1871 mathematica.stackexchange.com/a/28341/1871 For a more general solution, check the post I've already linked under your previous question. $\endgroup$
    – xzczd
    Commented Mar 28, 2021 at 12:29
  • 1
    $\begingroup$ If you're having difficulty with /., [[ ]], etc., please start from here: mathematica.stackexchange.com/a/25616/1871 $\endgroup$
    – xzczd
    Commented Mar 28, 2021 at 12:44
  • $\begingroup$ This question is superficially different, but its answer here answers this question. So I linked them. $\endgroup$
    – Michael E2
    Commented Mar 28, 2021 at 14:42
  • $\begingroup$ Yes, M["Domain"] /. First[sol1])[[1]] works. I used Reverse[(M["Domain"] /. First[sol1])[[1]]] // First to extract the number. Thanks. $\endgroup$
    – surujjd
    Commented Mar 28, 2021 at 15:27

0