An unexpected NDSolve behavior [closed]

I observe the following. The code:

 Clear[eq, ic1, ic2, y, t, nds];
eq = y''[t] == -y[t];
ic1 = y[0] == 1;
ic2 = y'[0] == 0;
nds = NDSolve[{eq, ic1, ic2}, y[t], {t, 0, 30}][[1, 1]]
Plot[Evaluate[y /. nds], {y, 0, 30}]


returns this:

As you see, the equation is that of a harmonic oscillator. One expects a cosine as a solution. But one sees what you see here. I am at Win7, Mma 10.4.1.0. Finally, this is a fresh session, I did not have a chance to evaluate anything else before this, and, thus, have nothing in Mma memory.

closed as off-topic by Quantum_Oli, Michael E2, m_goldberg, MarcoB, user9660 Apr 29 '16 at 13:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Quantum_Oli, Michael E2, m_goldberg, MarcoB, Community
If this question can be reworded to fit the rules in the help center, please edit the question.

• Should your last line not read Plot[Evaluate[y[t] /. nds], {t, 0, 30}]? You want to plot as a function of t. Also inside NDSolve you want to solve for y not y[t]. At least with 10.3 syntax you do – Quantum_Oli Apr 29 '16 at 9:24
• Or keep it as it is and plot just Plot[nds[[2]], {t, 0, 30}]. Eitherway I think your syntax is just a little off. (Although maybe one might expect MMA to be able to figure it out). – Quantum_Oli Apr 29 '16 at 9:27
• @Quantum_Oli No, in that case Mma returns the empty plot. And besides, the question is different. Whatever the way I obtained it, the plot should show a cosine, rather than a straight line. – Alexei Boulbitch Apr 29 '16 at 9:39
• I have to apologize, the whole question is the result of my stupid mistake Plot[Evaluate[y /. nds], {y, 0, 30}] instead of Plot[Evaluate[y /. nds], {t, 0, 30}] . I propose to close this question as the one based on a simple mistake. – Alexei Boulbitch Apr 29 '16 at 11:21

1 Answer

I'm on MMA 10.3 on OSX 10.10.5

Running your code indeed gives me the same result, a straight line.

However this is not surprising to me as

Evaluate[y /. nds]


returns, unexpectedly,

y

Therefore in the plot we are plotting, y from 0 to 30, hence a straight line, gradient 1, from 0 to 30...

Furthermore the syntax in your NDSolve is a little off to me. Checking the docs we see that the correct syntax is

NDSolve[{eq, ic1, ic2}, y, {t, 0, 30}]


not

NDSolve[{eq, ic1, ic2}, y[t], {t, 0, 30}]


The difference in the outputs is that in the second case a [t] is just appended to both sides of the output rule of NDSolve:

y -> InterpolatingFunction[{{0., 30.}}, <>]

vs

y[t] -> InterpolatingFunction[{{0., 30.}}, <>][t]

You can actually use this as we can run either:

nds = NDSolve[{eq, ic1, ic2}, y[t], {t, 0, 30}][[1, 1, 2]];
Plot[Evaluate[nds], {t, 0, 30}]


or

nds = NDSolve[{eq, ic1, ic2}, y, {t, 0, 30}][[1, 1]];
Plot[Evaluate[y[t] /. nds], {t, 0, 30}]


and get the same output:

Note I'm plotting over t, not y.

Maybe I've missed something more subtle in your question but it seems to be just a couple of small syntaxy mistakes.

Edit: If you really want to plot with an independent variable y you could even do:

nds = NDSolve[{eq, ic1, ic2}, y, {t, 0, 30}][[1, 1]];
Plot[Evaluate[y /. nds][y], {y, 0, 30}]


For the same result (although I'm not sure I'd recommend it)