75 views (last 30 days)

Paul
on 20 Dec 2020

Maybe this example will help, which essentially illustrates the comment made by Alvery.

Consider a plant P(s) = 1/(s + 5) that is controlled via PI compensation with Ki = Kp = 1. The sample time of the discrete control is Ts = 0.01, with Ts defined as such in the base workspace. This system is modeled in the top half of this diagram, where the sample time parameter for the Zero Order Hold and Discrete Time Integrator blocks is set to Ts.

Now we wish to implement the PI control in a Matlab Function block as in the boltom half of the diagram. We have the following equation for the control input:

u[k] = x[k] + Kp*e[k] % Kp = 1

The difference equation for x[k] is determined from the z-transform of the integrator:

X(z)/E(z) = Ki*Ts/(z-1) % Ki = 1

(z-1)*X(z) = Ki*Ts*E(z)

x[k+1] - x[k] = Ki*Ts*e[k]

x[k+1] = Ki*Ts*e[k] + x[k]

so the PI function needs to impement the equations for u[k] and x[k]. Here's the function

function u = PI(e)

persistent x

% initialize the integrator output

if isempty(x)

x = 0;

end

Kp = 1;

Ki = 1;

Ts = 0.01; % needs to be same value as Ts defined in the workspace

% control at the current time step

u = Kp*e + x;

% update the integrator for the next time step

x = Ki*Ts*e + x;

end

And here is the scope from the model showing that the implementation works as expected:

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 4 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/694760-simulink-difference-equation-implementation#comment_1210020

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/694760-simulink-difference-equation-implementation#comment_1210020

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/694760-simulink-difference-equation-implementation#comment_1211980

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/694760-simulink-difference-equation-implementation#comment_1211980

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/694760-simulink-difference-equation-implementation#comment_1218735

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/694760-simulink-difference-equation-implementation#comment_1218735

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/694760-simulink-difference-equation-implementation#comment_1218935

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/694760-simulink-difference-equation-implementation#comment_1218935

Sign in to comment.