Plotting first and second order ODE

graphing-functionsMATLABordinary differential equations

Here is the second order differential equation:

$\frac{d^2x}{dt^2} + \sin(x) = 0$

with initial conditions $x(0)=1$ and $x'(0)=0$.

The written code in matlab that numerically solves and plots the results is shown below:

function second_order
t=0:0.001:14;
initial_x=1;
initial_dxdt = 0;
[t,x]=ode45(@rhs,t,[initial_x initial_dxdt]);
plot(t,x(:,1));
xlabel('t');ylabel('x');

function dxdt=rhs(t,x)
dxdt_1=x(2);
dxdt_2=-sin(x(1));
dxdt = [dxdt_1; dxdt_2);
end
end

The plot shows the sinusoidal curve with maximum amplitude plus and minus one. This plot is correct. However, when I turn the second order differential equation into first one as follows:

$ \frac{d^2x}{dt^2}\frac{dx}{dt} + \sin(x)\frac{dx}{dt} = 0$

$\frac{1}{2}(\frac{dx}{dt})^2 = \cos(x)-\cos(1)$ and that leads to first order ODE,

$\frac{dx}{dt}=\sqrt{2(\cos(x)-\cos(1))}$

The matlab to plot first ODE is shown below:

function first_order
t=0:0.001:14;
initial_x=1;
[t,x]=ode45(@rhs, t, initial_x);
plot(t,x);
xlabel('t'); ylabel('x');

function dxdt=rhs(t,x)
dxdt = sqrt(2)*sqrt(cos(x)-cos(1));
end
end

The problem is that I am not getting the same plot as in second ODE code. Instead, I am getting a straight line at $x=1$. I know that the code is correct because I tested it with other first order differential equation. Therefore, why I am not getting the same plot even thought the first and second order differential equations are the same. First order is basically a solution of the second order. But values of $x$ should be the same. Is this approach not applicable? or am I doing something wrong?

Best Answer

$$ \frac{d^2x}{dt^2} + \sin(x) = 0 \tag 1$$ $$ \frac{d^2x}{dt^2}\frac{dx}{dt} + \sin(x)\frac{dx}{dt} = 0\tag 2$$ When you multiply Eq.$(1)$ by $\frac{dx}{dt}$ in order to get Eq.$(2)$, you introduces additional solutions which are solutions of $(2)$ but not solutions of $(1)$. The additional solutions are the solutions of $$\frac{dx}{dt}=0 \quad\implies\quad x(t)=c\tag 3$$ The boundary conditions $x(0)=1$ and $x'(0)=0$ give $c=1$. So an additional solution was introduced : $$x(t)=1$$ Thus is is correct that, according to the boundary conditions, the drawing of the solution of $(2)$ is the same as the drawing of the solution of $(1)$ plus the straight line $x=1$.

$$ $$

IN ADDITION after the comments :

The analytic solution of $\quad\frac{dx}{dt}=\sqrt{2(\cos(x)-\cos(1))}\quad$ is $$\begin{cases} x(t)=1\\ x(t)=2\text{ am}\left(\sqrt{\frac{1-\cos(1)}{2}}(c-t) \:\bigg|\: \csc^2(\frac12) \right) \end{cases}$$ $\text{am}$ denotes the Jacobi amplitude function.

With the condition $x(0)=1\quad\implies\quad c=\csc(\frac12)F\left(\frac12 \:\big|\:\csc^2(\frac12) \right)\simeq 1.67499$

$F$ denotes the elliptic integral of the first kind.

enter image description here

Related Question