Plotting first and second order ode of the same equation
1 回表示 (過去 30 日間)
古いコメントを表示
Here is the second order differential equation (1):

with initial conditions


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 件のコメント
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!