# NDSolve and maximum number of steps reached

I am newbie at Mathematica but I need to solve differential equation so I took my chances and tried to use this powerful tool to solve this equation: $$\frac{d}{dx}\left([\cos(2\pi x)+1]\frac{du(x)}{dx}\right) = 0$$ $$x \in[0,1]$$ $$\frac{du(0)}{dx}+u(0)=1$$ $$u(1)=0$$

So I read that I should use NDSolve to solve differential equations.

I wrote:

eqn = {(D[((Cos[2*Pi*x] + 1)*D[y[x], x]), x] == 0),
D[y[0], x] + y[0] == 1, y[1] == 0}
sol = NDSolve[eqn, y, {x, 0, 1}]


But I am getting: NDSolve::mxst: Maximum number of 10000 steps reached at the point x == 0.49999990622587265. >> NDSolve::berr: "There are significant errors {0.,0.017511} in the boundary value residuals. Returning the best solution found" NDSolve::mxst: Maximum number of 10000 steps reached at the point x == 0.4999999052334023.

How to avoid this errors and my program run and succeed?

• You can use MaxSteps ->100 instead of 100 you can use other numbers.
– MOON
Dec 27, 2014 at 22:39
• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. Dec 27, 2014 at 22:43
• @yashar I am still getting NDSolve::mxst: Maximum number of 100 steps reached at the point x == 0.46504538392462047. >> Dec 27, 2014 at 22:46
• NDSolve is encountering a singularity where Cos[2*Pi*x] + 1 is equal to zero. Dec 27, 2014 at 22:49
• @bbgodfrey Hmm, I see. How to avoid that, and make this run ? Dec 27, 2014 at 22:52

Use DSolve instead of NDSolve.

eqn = {D[(Cos[2*Pi*x] + 1)*D[y[x], x], x] == 0, y'[0] + y[0] == 1, y[1] == 0};
sol = DSolve[eqn, y[x], {x, 0, 1}][[1]]


The solution is Tan[Pi*x]/Pi.

Note: The original equation in the question contained D[y[0], x], which evaluates to zero, because y[0] is a constant. I presume that y'[0] is meant and made that replacement.

• Now I am getting : DSolve::dsvar: 0 cannot be used as a variable. Dec 27, 2014 at 23:10
• Are you using exactly what I have in the answer? Please also see the note that I just added to the bottom of my answer. Dec 27, 2014 at 23:17
• Yes I copied and used exactly the same code (link) Dec 27, 2014 at 23:23
• Strange I used the same code in Mathematica 10 (instead of Mathematica 8) and it worked ! Thank you Dec 27, 2014 at 23:27
• If you look at the code in this answer you see it is using DSolve and it is giving the independent variable as {x,0,1}. That would be fine if it were NDSolve, where you must give upper and lower bounds on the independent variable, or if it is for version 10 which introduces this new ability to solve exactly within a domain, but for DSolve in versions prior to 10 changing {x,0,1} to just x makes the error go away. I wonder if that change is mentioned in the new features list in V10.
– Bill
Dec 28, 2014 at 4:22