Plotting first and second order ode of the same equation
1 view (last 30 days)
Show older comments
Here is the second order differential equation (1):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/220483/image.png)
with initial conditions
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/220484/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/220485/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/220486/image.png)
and continue solving it so that,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/220487/image.png)
and that leads to first order ODE (2),
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/220488/image.png)
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 Comments
Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!