简体   繁体   中英

How do I solve a third order differential equation using ode23 in MATLAB and plot the step response

I have based my solution off the example provided by Matlab - solving a third order differential equation .

My problem is that I have to solve the third order differential equation, y'''+3y''+2y'+y=4u, by using the ode23 solver and plot the step response. Here is what I have so far.

function dy = diffuy( t, y )
%Split uy into variables in equation
%y'''+3y''+2y'+y=4u
%Have to take third order equation and convert to 1st order
%y0 = y
%y1 = y0'
%y2 = y1'
%y3 = y2'

%y0' = y1
%y1' = y2
%y2' = y3
%y3' = y''' = -3*y2-2*y1-y0+4*u
%Assume that y(0)= 0, y'(0)=0, y''(0)=0, no initial conditions
u = @(t) heaviside(t);

dy =  zeros(4,1);
dy(1) = y(2);
dy(2) = y(3);
dy(3) = y(4);
dy(4) = -3*y(3)-2*y(2)-y(1)+4*u(t);
end

In my main file, I have the code:

[T, Y]=ode23(@diffuy,[0 20],[0 0 0 0]);
figure(1)
plot(T,Y(:,1))


A=[0 1 0;0 0 1; -1 -2 -3]
B=[0;0;4]
C=[1 0 0]
D=[0]

sys4=ss(A,B,C,D)
figure(2)
step(sys4)

The problem I am having is that the step response produced from using the state-space representation commands in MATLAB do not match the step response produced by the ode23, so I assumed that I solved the differential equation incorrectly. Any tips or comments would be very helpful.

Step Response from ss commands: 在此处输入图片说明

Step Response from using ode23:

在此处输入图片说明

I'm not sure how the linked question got the correct answer because you're actually solving a fourth-order equation using their methodology. The right hand-side vector given to the ODE suite should only have n entries for an n -order problem.

In your case, the change of variables

变量矩阵变化

results in the third order system

三阶系统方程

with the initial conditions

初始条件方程 .

Changing diffuy to

function dy = diffuy( t, y )        
    dy =  zeros(3,1);
    dy(1) = y(2);
    dy(2) = y(3);
    dy(3) = -3*y(3)-2*y(2)-y(1)+4*u(t);
end

gives a solution that matches the state-space model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM