Plotting first and second order ode of the same equation

조회 수: 1 (최근 30일)
mathango
mathango 2019년 5월 21일
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