3
$\begingroup$

I'm trying to solve the ODE below using Matlab's ode45:

$$\frac{dy}{dx}=\sqrt{A\cdot \sin(y) + B\cdot \cos(y)}$$

where $0\le x \le 1$.

The Matlab code is:

A = -2.45;
B = 2.50;
tspan  = 0:0.01:1;
odefun = @(t,y) sqrt(A*sin(y) + B*cos(y));
[t,y] = ode45(odefun, [0 1], 0);

The constants $A$ and $B$ are chosen such that inside the square root it is always non-negative. The solution seems fine for most values of $x$, but for the last few points close to $x=1$ I get complex $y$ with very small imaginary parts. I then took the real or the absolute value of $y$, but then I would get negative value inside the square root.

Should I try other ODE solvers? Or is there anything else I could do to eliminate the complex solutions?

  • 0
    You should provide your code. Btw why don't you try to solve it using elliptical functions?2017-02-21
  • 0
    Just edited it. Tbh, I'm not very familiar with elliptic integral. I thought in Matlab if you use functions such as ellipke, you need to specify the limits of integration. That works if I just want the value of y at a particular x. What if I want to get y for all values of x in [0,1]? Thank you.2017-02-21
  • 0
    Have you noticed that in your equation, $x$ is a function of $y$, typically?2017-02-21
  • 0
    Please edit your question to provide runnable code that can be used to replicate your particular issue.2017-02-21
  • 0
    @horchler Edited with runnable code on matlab.2017-02-21
  • 0
    @polfosol Sure but what I'm concerned is that numerically I got complex solutions. I suspect very close to x=1 the gradient approaches 0 and there's some issue with the numerical tolerance settings.2017-02-21
  • 0
    For $y$ greater than about 0.8 you get negative values inside the square root for the constants provided. Also, shouldn't the ODE include both the positive and negative branches of the square root, i.e., $\pm\sqrt()$? You might check out [this post](http://blogs.mathworks.com/cleve/2016/11/14/my-favorite-ode/) from The MathWorks on systems like this.2017-02-21
  • 0
    @horchler Thanks for your advice. The derivative dy/dx represents curvature and I intend it to be non-negative for my application. I realized a critical value for y can be determined by setting the square root to be zero. I then did the change of variable and converted it to 1st kind incomplete elliptic integral, to help check if the integral exceeds 1 (the upper limit for x). If yes, then there exists real solution; otherwise complex solution should be expected.2017-02-22

2 Answers 2

2

This first-order ODE is such that $dy/dx \geqslant 0$. Therefore, a solution $y$ must be an increasing function of $x$. Here, we start with the initial condition $y(0)=0$, where the derivative satisfies $dy/dx \approx 1.6 > 0$. The derivative $dy/dx$ becomes complex when its square becomes negative. For this to happen, an equilibrium value (i.e. where the derivative vanishes) must have been reached. Here, the smallest positive equilibrium value is \begin{equation} y^* = {-\arctan(B/A)} \approx 0.796 \, . \end{equation} This can be observed when using Matlab: the value $y^*$ is reached numerically just before $x=1$. The choice $A=2.45$ and $B=2.5$ does not avoid $dy/dx$ to become complex.

Note: the Picard-Lindelöf theorem applies here. If $x^*$ is such that $y(x^*)=y^*$, we know that the solution $y$ over $[0, x^*[$ is unique. Thus, it can be approximated numerically with classical methods such as ode45.

2

If you square the equation and take the derivative you get $$ 2y'y''=(A\cos(y)-B\sin(y))y' $$ so that on segments where $y$ is not constant you get the regular ODE $$ y''=\frac12(A\cos(y)-B\sin(y)),~~y(0)=0,~~y'(0)=\sqrt{B} $$ which is easily solved numerically

enter image description here

For the solution of the original equation, one would have to detect the maxima/minima and continue from there as a constant solution.

enter image description here

These connection points of the composite solution only have a continuous first derivative, which invalidates the assumptions of higher-order solvers, and more so of step size controllers.

More specific to this problem, the evaluations of the stages take place close by but not necessarily on the solution curve. In this case this can lead to a negative value for the term under the root, even if this term for the exact solution is non-negative.

Another cause for unexpected behavior could be that the method errors accumulate so that the numerical solution does not stay below the critical value. Here in the solution above with standard tolerances this happens in the interval $(0.953,0.975)$ where $A\sin(y) + B\cos(y)$ takes values down to $-0.0003$. So try to set sharper tolerances.