Plotting first and second order ode of the same equation

6 次查看(过去 30 天)
Here is the second order differential equation (1):
with initial conditions
and
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 sinuisoidal 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:
and continue solving it so that,
and that leads to first order ODE (2),
which is the solution to second order ODE (1).
The matlab to plot first ODE is shown below:
function first_order
t=0:0.001:14;
initial_x=1;
[t,x]=ode45(@45rhs,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 in matlab?

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by