2
$\begingroup$
g = 9.81;
k = 0.009;
r = 10;
b = 0;
ω = Sqrt[g/r];

NDSolve[{r/g*y''[x] + (k*r/g + b*r^2/mg)*y'[x]^2 + Cos[x] - k*Sin[x] == 0, 
        y[0] == π/2, y'[0] == ω}, y, {x, 0, 2}]

Regarding to this question, NDSolve evaluates the ODE without an error message. NDSolve doesn't evaluate the term b*r^2/mg because b == 0, although the variable mg is unknown. This is not correct in my opinion.

Is it a bug?

$\endgroup$
6
  • 6
    $\begingroup$ No, it's not a bug, and it's not related to NDSolve. Take the equation and evaluate it without NDSolve. The b*... part will disappear due to b being 0. Essentially you are asking: "Is it correct that 0/mg automatically evaluates to 0 without mg having a value?" $\endgroup$
    – Szabolcs
    Oct 14, 2015 at 10:30
  • 1
    $\begingroup$ That question about 0/mg getting auto-simplified has been brought up before. Here's one related thread. There are others I cannot find right now. $\endgroup$
    – Szabolcs
    Oct 14, 2015 at 10:32
  • $\begingroup$ @Szabolcs Unusually behaviour, mg can be text or anything and yetl it will be solved! NDSolve[{r/g*y''[x] + (k*r/g + b*r^2/"abc ")*y'[x]^2 + Cos[x] - k*Sin[x] == 0, y[0] == \[Pi]/2, y'[0] == \[Omega]}, y, {x, 0, 2}] $\endgroup$
    – user31001
    Oct 14, 2015 at 11:45
  • $\begingroup$ It is not 0/mg, it is NDSolve, a numeric processor. Before it solves, all variables must be known as numeric. $\endgroup$
    – user31001
    Oct 14, 2015 at 12:45
  • $\begingroup$ I think you didn't understand my comment. NDSolve never sees 0/mg because 0/mg immediately evaluates to 0. What you see has absolutely nothing to do with NDSolve. When you type NDSolve[args], first args are evaluated, NDSolve is only processed afterwards. $\endgroup$
    – Szabolcs
    Oct 14, 2015 at 12:50

1 Answer 1

3
$\begingroup$

I think the issue behind this question is actually interesting. Not-so-experienced Mathematica users often suffer an illusion: the internal functions will merge when they are used together.

In your case, you probably felt that NDSolve together with the equations and initial conditions have merged to something that will solve the ODE. "So, what happened inside this something?" "Hmm, I don't know, and I believe no body knows, it happened internally." Unfortunately, as said above, it's generally not true. When functions are used together, they will just execute from inner to outer. (This order will be adjusted by attributes like HoldAll, HoldFirst, etc. of course.)

A choice to show the process is to use Trace, you can also try the functions in this post. Here I'll use WReach's traceView2:

NDSolve[{r/g y''[x] + (k r/g + b r^2/mg) y'[x]^2 + Cos[x] - k Sin[x] == 0, 
        y[0] == π/2, y'[0] == ω}, y, {x, 0, 2}] // traceView2

enter image description here Pictured by Simon Wood's shadow.

As you see, the equation inside NDSolve is executed before NDSolve begins to work.

$\endgroup$
1
  • $\begingroup$ A very clear explanation, thank's @xzczd for your effort. Yes, now I know Mathematica's working method. As a person, who deals daily with formulae, It makes me sick to my stomach when I think I can mix apples and pears, multiply it with zero, and now I'm ready to hand over the solution to a numeric solver. No hard feelings, I shall never forget Mathematica' s working method. $\endgroup$
    – user31001
    Oct 14, 2015 at 15:57