Example of Calculating Control System Response Output Using MATLAB’s Runge-Kutta Method

First, fill in the transfer function of the continuous system, then convert it into a state-space model system. Next, determine the time interval and simulation time, followed by defining the input sine wave signal. Then, use the 4th-order Runge-Kutta method to solve the system of differential equations in state-space, and finally observe the system’s output signal.

% Transfer function: G(s)=(13.6*s^2+18.1*s+22.5)/(s^3+6.7*s^2+7.8*s+8.9)
% feedback()
% tf2ss();

clear all;close all;clc;

% System transfer function
num1=[13.6  18.1  22.5 ];
den1=[1  6.7  7.8  8.9];
feedbackNum1=[0.1  0.2];
feedbackDen1=[4, 5.0];

% Feedback system
[num2,den2]=feedback(num1,den1, feedbackNum1,feedbackDen1);

% Roots of the system
% roots(num2);
[A1,B1,C1,D1]=tf2ss(num2,den2);
timeStep=0.01;
timeEnd=20;
Len=timeEnd/timeStep+1;
time1=zeros(1,Len);
row1=length(A1(:,1));
stateX=zeros(row1,Len);
inX=zeros(1,Len);
outY=zeros(1,Len);
t1=0;
x1=zeros(row1,1);
y1=0;
w1=2;

% Runge-Kutta method
for ii1=1:1:Len
    time1(ii1)=ii1*timeStep;
    inX(ii1)=5.0*sin(2*pi*w1*ii1*timeStep);
    K1=A1*x1+B1*inX(ii1);
    K2=A1*(x1+timeStep*K1/2)+B1*inX(ii1);
    K3=A1*(x1+timeStep*K2/2)+B1*inX(ii1);
    K4=A1*(x1+timeStep*K3)+B1*inX(ii1);
    x1=x1+timeStep*(K1+2*K2+2*K3+K4)/6;
    stateX(:,ii1)=x1;
    y1=C1*x1; % +D1*x1;
    outY(ii1)=y1;
end

figure(1);plot(time1,inX,'b',time1,outY,'r');
title('Input and Output Curves.');
xlabel('t(s)');
ylabel('y');
legend('Input x', 'Output y');
figure(2);plot(time1, outY-inX, 'b');
title('Output and Input Difference.');
xlabel('t(s)');
ylabel('y-x');
legend('y-x');
disp('End.');

Keywords: Runge-Kutta, state-space model, control system, MATLAB, differential equations

Leave a Comment