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?

You can get the "curved" solution from a first order equation by further parametrizing the integrated equation $$ \frac12\dot x^2+\frac12\left(2\sin\frac x2\right)^2=\frac12\left(2\sin\frac 12\right)^2 $$ by recognizing this as a circle equation for $(\dot x,2\sin\frac x2)$ and thus setting $$ 2\sin\frac{x(t)}2 = 2\sin\frac 12\cdot \sin(u(t)),\\ \dot x(t)= 2\sin\frac 12\cdot \cos(u(t)), $$ $u(0)=\frac\pi2$, and then comparing the derivative of the first equation with the second equation, $$ \cos\frac{x(t)}2\cdot\dot x(t) = 2\sin\frac 12\cdot\cos(u(t))\dot u(t) $$ so that for non-constant solutions (so that $\cos(u(t))\ne 0$) $$ \dot u(t) = \cos(\frac{x(t)}2)=\sqrt{1-\sin^2\frac 12\cdot \sin^2(u(t))} $$ For the plot you have then to reverse the parametrization to $$ x(t)=2\arcsin\left(\sin\frac 12\cdot \sin(u(t))\right) $$