Algebraic loop in MRAC discrete model

Hello everyone.
I have this system I built that represents an RMAC model
After the system worked continuously as it should, I converted it to discrete with a sampling time of 0.01
I get an error that the system enters an algebraic loop and I am unable to converge to the desired values. (The output of the system is increased to very, very high values)
I tried to put a unit delay but it neither helps nor leads to entertainment
what can be done? I would really appreciate some help.. I tried everything possible
Also, should I change the sampling time of each block to 0.01 or is it ok to leave -1?
Many thanks to everyone who helps
errors:
Error:'sim2/discrete plant2' or the model referenced by it contains a block that updates persistent or state variables while computing outputs and is not supported in an algebraic loop. It is in an algebraic loop with the following blocks.
Error:Input ports (1) of 'sim2/Adaptive Mechanism_known parameters_ discrete2/MATLAB Function1' are involved in the loop.
and more..

Respuestas (1)

Umar
Umar el 8 de Jul. de 2024
Editada: Walter Roberson el 14 de Jul. de 2024
Hi Yovel,
When dealing with algebraic loop errors in a discrete RMAC model, it is crucial to understand the nature of the problem and implement appropriate solutions. Algebraic loops occur when a block updates persistent or state variables while computing outputs, creating a dependency loop that disrupts the system's stability and convergence.
To address the algebraic loop issue in your discrete RMAC model, consider the following steps:
1. Break the Algebraic Loop
One effective approach is to break the algebraic loop by introducing a discrete-time delay element in the loop. While you mentioned that using a unit delay did not resolve the problem, you may need to carefully analyze the loop structure and identify the specific blocks causing the loop. Try introducing delays at strategic points within the loop to break the dependency chain.
% Example of introducing a delay to break the algebraic loop
delayBlock = dsp.Delay(1); % Create a one-sample delay block
2. Check Block Dependencies
Review the blocks involved in the algebraic loop, especially those updating persistent or state variables. Ensure that the dependencies are correctly defined and that no block is inadvertently causing the loop by updating variables at inappropriate times.
3. Adjust Sampling Time
Regarding the sampling time for each block in your model, it is generally advisable to maintain consistency with the overall system sampling time. In your case, setting a sampling time of 0.01 for all blocks should help ensure synchronization and proper functioning of the discrete RMAC model.
% Set sampling time for a block to 0.01
block.SampleTime = 0.01;
4. Debugging and Validation
Perform thorough debugging and validation of your model to identify any other potential issues that may be contributing to the algebraic loop error. Check for data dependencies, signal flows, and block configurations to ensure the model's correctness.
By following these steps and carefully analyzing the structure of your discrete RMAC model, you can effectively troubleshoot and resolve the algebraic loop error. Remember to test the modified model iteratively to verify its stability and convergence under different scenarios.
If you encounter further challenges or need additional assistance, feel free to provide more details about your model's specific components for a more tailored solution. Good luck with resolving the algebraic loop error in your discrete RMAC model!

13 comentarios

HI, thank you very much for the answer and the help. I still can't find the source of the loop..
I am attaching the 3 codes of my blocks in the hope that you might be able to remotely tell me where the problem is :\
%------- The controller block: -----------
function u_k = controller(yp_k, addaptivelaw,ym_k,ddy_m)
persistent ek1 dy_m_k1 yp_k1 ym_k1
% % Initialization:
if isempty(ek1)
ek1=0;
yp_k1=0;
ym_k1=0;
dy_m_k1=0;
end
%the continues controller u:
%dd_yr=dd_ym-lambda*de;
% u = h*dd_yr +a1*d_yp+a2*yp+b1*(dd_yp)^2 -k*z;
%the discrete controller:
% Contstant parameters:
T=0.01;
lambda= 2;
k=0.5;
h=addaptivelaw(1); a1=addaptivelaw(2); a2=addaptivelaw(1); b1=addaptivelaw(4); z=addaptivelaw(5);
ek=yp_k-ym_k;
% Define the derivative of the Error:
de_k=(ek-ek1)/T;
dy_p_k = (yp_k-yp_k1)/T;
ddy_r_k=ddy_m - lambda *de_k;
% ========== Step 2 : CODE ==========
uk = h * ddy_r_k - k * z + a1 * dy_p_k + a2 * yp_k + b1 * (dy_p_k^2) ; % u: control input
% Store data for nex step:
yp_k1=yp_k;
ym_k1=ym_k;
ek1=ek;
u_k = uk;
%------- The Plant block: -----------
function y_p_k = Gc(uk)
persistent yk1 yk2 uk1 uk2
% % Initialization:
if isempty(yk2)
yk1=0;
yk2=0;
uk1=0;
uk2=0;
end
% Contstant parameters:
T=0.01;
M=8; % [kg]
m=3; % [kg]
l=3; % [m]
g=9.81; % [m/s^2]
c=2;
A=4*l*(M+m);
B=l*(M+m)+l*m;
% ========== Step 2 : CODE ==========
%the continues dd_yp:
% ddYp = -(l*m*dtheta^2 + c*dtheta - u + g*m*theta)/(l*(M + m));
% discrete time: new try: 8/7: (after laplace and z transformation)
%1: yk*(4*B+2*c*T+g*m*T^2)+yk1*(-8*B+2*g*m*T^2)+yk2*(4*B-2c*T+g*m*T^2)=(T^2)*(uk+2*uk1+uk2)
%2: yk*(4*B-2*c*T-g*m*T^2)=(T^2)*(uk+2*uk1+uk2)-yk1*(-8*B+2*g*m*T^2)-yk2*(4*B-2*c*T+g*m*T^2)
yk = ( (T^2)*(uk+2*uk1+uk2)-yk1*(-8*B+2*g*m*T^2)-yk2*(4*B-2*c*T+g*m*T^2) )/ (4*B-2*c*T-g*m*T^2);
disp('yk is')
disp(yk)
% Store data for nex step:
yk2=yk1;
yk1=yk;
uk2=uk1;
uk1=uk;
y_p_k = yk;
%The adaptive control law block:
function [h,a1, a2, b1, zk] = Adaptive_control(ek)
T=0.01;
gamma=2;
M=8; % [kg]
m=3; % [kg]
l=3; % [m]
g=9.81; % [m/s^2]
c=2;
h = (M+m)*l;
a1 = c;
a2 = m*g;
b1 = l*m; % Yp=theta
persistent ek1
% % Initialization:
if isempty(ek1)
ek1=0;
end
% ========== Step 2 : CODE ==========
% Define the Error:
de_k=(ek-ek1)/T;
% Store data for nex step:
ek1=ek;
zk = de_k+gamma*ek;
Thank you so much!!!!!!!!
Umar
Umar el 8 de Jul. de 2024

Hi Yovel,

Based on the provided code snippets for the controller, plant, and adaptive control law blocks, it appears that the issue lies in the implementation of the adaptive control law block.

Let's analyze each block individually to identify potential problems: 1. *Controller Block:* - The controller block seems well-structured and correctly computes the control input `u_k` based on the defined equations and parameters. - The persistent variables are appropriately initialized and updated in each step. 2. *Plant Block:* - The plant block defines the system dynamics based on given constants and equations. - The calculation of `y_p_k` based on discrete-time equations appears to be correct. - The persistent variables are initialized and updated correctly. 3. *Adaptive Control Law Block:* - In the adaptive control law block, there seems to be an issue with the calculation of `zk` which is defined as `de_k + gamma * ek`. - The initialization of the persistent variable `ek1` is correctly handled. - However, it's important to ensure that the calculation of `zk` accurately reflects the intended adaptive control law logic.

To address the problem in the adaptive control law block, you may need to revisit the calculation of `zk` to ensure it aligns with your desired adaptive control strategy. Double-checking the mathematical formulation and considering any potential errors in the logic could help resolve this issue. Feel free to provide more details or specific error messages if further assistance is needed in troubleshooting this issue.Good luck!

Yovel
Yovel el 9 de Jul. de 2024
Hi, thanks again for your help :)
z is calculated as follows:
z = dy_t - dy_m + gamma * e;
What am I doing wrong in the discrete calculation of Z?
I tried putting a constant Z, let's say z=3 and I still get an algebraic loop error.
The problem is not in controller and PLANT in that u depends on yp and in addition yp depends on u?
This error is so frustrating :/
Umar
Umar el 9 de Jul. de 2024
Hi Yovel,
The problem lies in the feedback loop structure of the system. When u depends on yp and yp depends on u, the system enters a loop where the values of these variables cannot be determined independently at each time step. To resolve the algebraic loop error, you need to break the circular dependency between u and yp. One common approach is to introduce a delay in the system to decouple the variables. By introducing a delay element, you can ensure that the values of u and yp are not directly dependent on each other at the same time step. By introducing a delay in the calculation of yp, you break the direct dependency between u and yp, resolving the algebraic loop error.
Yovel
Yovel el 12 de Jul. de 2024
Editada: Walter Roberson el 14 de Jul. de 2024
Hi Umar :)
I try to convert it to matlab script
clc; clear; close;
dt = 0.001;
t_start = 0;
t_end = 70;
k_end = (t_end - t_start) / dt;
time = linspace(t_start, t_end, k_end);
r = heaviside(time);
r(1)=1;
T=dt;
% Parameters
M=8; % cart mass [kg]
m=3; % pendulum mass [kg]
l=3; % pendulum len [m]
g=9.81; % gravity [m/s^2]
c=2; % Damping coefficient
B=l*(M+m)+l*m;
% Reference model parameters
I_m = 8; % Moment of inertia
c_m = 3; % Damping coefficient
mgl=1;
A=mgl*T^2;
% Initialize variables
y_mk1 = 0;
y_mk2 = 0;
dy_mk1 = 0;
Rk1 = 0;
Rk2 = 0;
y_pk1=0;
y_pk2=0;
uk1=0;
uk2=0;
ek1=0;
y_pk=0;
% Preallocate arrays for storage
yp = zeros(1, k_end);
ym = zeros(1, k_end);
U = zeros(1, k_end);
E = zeros(1, k_end);
DE = zeros(1, k_end);
lambda= 80; k=10;
for j = 1:k_end
i = time(j);
Rk = r(j);
h = (M+m)*l;
a1 = c;
a2 = m*g;
b1 = l*m;
if mod(j, 100 * dt) == 0
y_mk = ( (T^2)*(Rk+2*Rk1+Rk2) -y_mk1*(-8*I_m+2*A) -y_mk2*(4*I_m-2*c_m*T+A) )/(4*I_m+2*c_m*T+A);
% --controller block
ek=y_pk-y_mk; %error
dy_mk = (y_mk-y_mk1)/T; %derv of ymk
ddy_mk = (dy_mk - dy_mk1)/T;
dy_pk = (y_pk-y_pk1)/T; %derv of ypk
de_k = (dy_pk - dy_mk); %derv of error
ddy_rk = ddy_mk-(lambda*de_k);
zk = de_k+lambda*ek;
% -----end
uk = h * ddy_rk - k * zk + a1 * dy_pk + a2 * y_pk + b1 * (dy_pk^2) ; % u: control input
% if mod(j, 10 * dt) == 0
end
y_pk = ( (T^2)*(uk+2*uk1+uk2) -y_pk1*(-8*B+2*m*g*T^2) -y_pk2*(4*B-2*c*T+m*g*T^2) )/ (4*B+2*c*T+m*g*T^2);
% Store results
yp(j) = y_pk;
ym(j) = y_mk;
U(j) = uk;
E(j) = ek;
DE(j) = de_k;
% Store data for nex step: plant
y_pk2=y_pk1;
y_pk1=y_pk;
y_mk2=y_mk1;
dy_mk1=dy_mk;
y_mk1=y_mk;
ek1=ek;
uk2=uk1;
uk1=uk;
Rk2=Rk1;
Rk1=Rk;
end
If I want the sampling time to be every 0.1 seconds
Do I need to change dt= 0.1, or should I update part of the system every 0.1 seconds? The Plant or the controller and the reference system? And what should I put in T (continuous time - very small or put 0.1?) I'm not sure..
Because my graph is unstable
I would appreciate your help, thank you very much!
Umar
Umar el 12 de Jul. de 2024
Hi Yovel,
Good to hear back from you. I am guessing that the script appears to be a control system simulation with a reference model and a controller block. To address your question, you should change 'dt' to 0.1, update both the plant and the controller at this new sampling time, and set 'T' to 0.1 to match the discrete-time interval. Additionally, reviewing the synchronization between your simulation and plotting may help resolve the instability issues in your graph.
Yovel
Yovel el 14 de Jul. de 2024
Hi
I am updating that I managed to solve the problem :))
I have one last question and I would really appreciate your help
The reference model and the controller are sampled every 0.3 seconds and the plant's system is sampled continuously.
I was asked to think of an idea to improve performance in a discrete time
Is it possible to add something to the controller \ to the system to reduce the error resulting from the delay?
Besides changing the sampling time*
Umar
Umar el 14 de Jul. de 2024

Hi Yovel,

To resolve your problem, you can use a predictor or an observer in the control loop. I will use observer in my example to estimate the system's internal state based on input and output measurements. By incorporating an observer, the controller can adjust its actions based on the estimated state, compensating for delays and reducing error. Here's a basic example using an observer in a discrete-time system:

% Define system parameters

A = 0.9; % System dynamics B = 0.5; % Input coefficient C = 1; % Output coefficient

% Initialize observer

x_hat = 0; % Initial state estimate

% Simulate system response

for k = 1:10

    % Simulated plant response
    y = C * x_hat;
    % Calculate control input (example: simple proportional control)
    u = -0.5 * x_hat;
    % Update observer state estimate
    x_hat = A * x_hat + B * u + 0.1 * randn; % Add noise for realism
    disp(['Output at time step ', num2str(k), ': ', num2str(y)]);
end

Please see attached results.

Please let me know if you have any further questions. Good luck!

Yovel
Yovel el 14 de Jul. de 2024
Hi! thanks
I have a question
Does y represent the output (system response) of the plant?
And how do I choose the values of A, B, C?
Can you make a match on my code? Because I didn't quite understand you..
Thank you!
for j = 1:length(time)
%Current input signal:
Rk = r(j);
if mod(j, 100) == 0 ||firstrun
y_mk = ( (T^2)*(Rk+2*Rk1+Rk2) -y_mk1*(-8*I_m+2*A) -y_mk2*(4*I_m-2*c_m*T+A) )/(4*I_m+2*c_m*T+A); % reference model
ek=y_pk-y_mk; %error
dy_mk = (y_mk-y_mk1)/T; %derv of y_mk
ddy_mk = (dy_mk - dy_mk1)/T; %derv of dy_mk
de_k = (dy_pk - dy_mk); %derv of error
ddy_rk = ddy_mk-(lambda*de_k);
zk = de_k+lambda*ek;
%uk = h_hat * ddy_rk - k * zk + a1_hat * dy_pk + a2_hat * y_pk + b1_hat*(dy_pk^2) + b2_hat*(y_pk^2); % u: control input
uk = h_hat * ddy_rk - k * zk + a1_hat * dy_pk + a2_hat * y_pk + b1_hat*(dy_pk^2)...
+ b2_hat*(y_pk^2) ;%- 4*dy_pk +5*y_pk ; % u: control input
% Adaptive controller parameters - Update:
dh_hat = -gamma * zk * ddy_rk;
da1_hat = -gamma * zk * dy_pk;
da2_hat = -gamma * zk * y_pk;
db1_hat = -gamma * zk * dy_pk^2;
db2_hat = -gamma * zk * y_pk^2;
h_hat = h_hat + dh_hat * T;
a1_hat = a1_hat + da1_hat * T;
a2_hat = a2_hat + da2_hat * T;
b1_hat = b1_hat + db1_hat * T;
b2_hat = b2_hat + db2_hat * T;
% Store data for nex step:
y_mk2=y_mk1;
y_mk1=y_mk;
Rk2=Rk1;
Rk1=Rk;
uk2=uk1;
uk1=uk;
ek1=ek;
firstrun=false;
end
y_pk = ( (dt^2)*(uk+2*uk1+uk2) -y_pk1*(-8*(c2+I)+2*B*dt^2) -...
y_pk2*(4*(c2+I)-2*c1*dt+B*dt^2) ) /(4*(c2+I)+2*c1*dt+B*dt^2); % plant model
dy_pk = (y_pk-y_pk1)/dt; %derv of ypk
% Store data for nex step: plant
y_pk2=y_pk1;
y_pk1=y_pk;
% Store results
yp_arr(j) = y_pk;
ym_arr(j) = y_mk;
u_arr(j) = uk;
e_arr(j) = ek;
de_arr(j) = de_k;
% Store adaptive parameters
h_hat_arr(j) = h_hat;
a1_hat_arr(j) = a1_hat;
a2_hat_arr(j) = a2_hat;
b1_hat_arr(j) = b1_hat;
b2_hat_arr(j) = b2_hat;
%V_arr(j) = abs(h)*(zk)^2+(gamma^-1)*( (h_hat-h)^2+(a1_hat-a1)^2+(a2_hat-a2)^2+(b1_hat-b1)^2+(b2_hat-b2)^2 );
V_arr(j) = -2 *abs(k)*(zk)^2;
end
Umar
Umar el 14 de Jul. de 2024
Editada: Walter Roberson el 14 de Jul. de 2024
Hi Yovel,
Clarifying my comments and suggestions based on my snippet code
Please see my response to your comments below. To clarify differences and help resolve your issues, I labeled my code as “Code#2 and yours code is labeled as “Code#1”. In Code#2, the variable `y` represents the output or system response of the plant at each time step. It is calculated based on the current estimated state `x_hat` and the output coefficient `C`. This output is then displayed for each time step within the simulation loop. To address your question regarding how to choose the values of `A`, `B`, and `C` in Code#1 based on Code#2, it's essential to understand their roles in the system dynamics. In Code#2, `A` represents the system dynamics, indicating how the state evolves over time. `B` is the input coefficient, influencing how control input affects the state transition, and `C` is the output coefficient, determining how the state translates into observable output. In your Code#1, you have a more complex adaptive controller structure with various parameters that are updated iteratively based on error signals and control inputs. To match this with Code#2, you need to consider how these adaptive parameters can be tuned to enhance system performance and reduce errors caused by delays. My suggestion would be incorporating elements from Code#2 into your adaptive controller design in Code#1. For instance, leveraging insights from how `x_hat` is estimated in Code#2 or how control inputs `u` are calculated based on state estimates can help refine your adaptive controller's behavior.By integrating concepts from both codes and aligning the system dynamics represented by `A`, `B`, and `C` with your adaptive control strategy in Code#1, you can potentially optimize performance and mitigate errors stemming from delays in the system.
Code#2 (@Umar’s code)
% Define system parameters
A = 0.9; % System dynamics B = 0.5; % Input coefficient C = 1; % Output coefficient
% Initialize observer
x_hat = 0; % Initial state estimate
% Simulate system response
for k = 1:10
% Simulated plant response
y = C * x_hat;
% Calculate control input (example: simple proportional control)
u = -0.5 * x_hat;
% Update observer state estimate
x_hat = A * x_hat + B * u + 0.1 * randn; % Add noise for realism
disp(['Output at time step ', num2str(k), ': ', num2str(y)]);
end
So, in code#2 Does y represent the output (system response) of the plant?
And how do I choose the values of A, B, C in code#1 based on code#2
Can you make a match on my code#1?
Code#1 (@Yovel’s Code)
for j = 1:length(time)
%Current input signal:
Rk = r(j);
if mod(j, 100) == 0 ||firstrun
y_mk = ( (T^2)*(Rk+2*Rk1+Rk2) -y_mk1*(-8*I_m+2*A) -y_mk2*(4*I_m-2*c_m*T+A) )/(4*I_m+2*c_m*T+A); % reference model
ek=y_pk-y_mk; %error
dy_mk = (y_mk-y_mk1)/T; %derv of y_mk
ddy_mk = (dy_mk - dy_mk1)/T; %derv of dy_mk
de_k = (dy_pk - dy_mk); %derv of error
ddy_rk = ddy_mk-(lambda*de_k);
zk = de_k+lambda*ek;
%uk = h_hat * ddy_rk - k * zk + a1_hat * dy_pk + a2_hat * y_pk + b1_hat*(dy_pk^2) + b2_hat*(y_pk^2); % u: control input
uk = h_hat * ddy_rk - k * zk + a1_hat * dy_pk + a2_hat * y_pk + b1_hat*(dy_pk^2)...
+ b2_hat*(y_pk^2) ;%- 4*dy_pk +5*y_pk ; % u: control input
% Adaptive controller parameters - Update:
dh_hat = -gamma * zk * ddy_rk;
da1_hat = -gamma * zk * dy_pk;
da2_hat = -gamma * zk * y_pk;
db1_hat = -gamma * zk * dy_pk^2;
db2_hat = -gamma * zk * y_pk^2;
h_hat = h_hat + dh_hat * T;
a1_hat = a1_hat + da1_hat * T;
a2_hat = a2_hat + da2_hat * T;
b1_hat = b1_hat + db1_hat * T;
b2_hat = b2_hat + db2_hat * T;
% Store data for nex step:
y_mk2=y_mk1;
y_mk1=y_mk;
Rk2=Rk1;
Rk1=Rk;
uk2=uk1;
uk1=uk;
ek1=ek;
firstrun=false;
end
y_pk = ( (dt^2)*(uk+2*uk1+uk2) -y_pk1*(-8*(c2+I)+2*B*dt^2) -...
y_pk2*(4*(c2+I)-2*c1*dt+B*dt^2) ) /(4*(c2+I)+2*c1*dt+B*dt^2); % plant model
dy_pk = (y_pk-y_pk1)/dt; %derv of ypk
% Store data for nex step: plant
y_pk2=y_pk1;
y_pk1=y_pk;
% Store results
yp_arr(j) = y_pk;
ym_arr(j) = y_mk;
u_arr(j) = uk;
e_arr(j) = ek;
de_arr(j) = de_k;
% Store adaptive parameters
h_hat_arr(j) = h_hat;
a1_hat_arr(j) = a1_hat;
a2_hat_arr(j) = a2_hat;
b1_hat_arr(j) = b1_hat;
b2_hat_arr(j) = b2_hat;
%V_arr(j) = abs(h)*(zk)^2+(gamma^-1)*( (h_hat-h)^2+(a1_hat-a1)^2+(a2_hat-a2)^2+(b1_hat-b1)^2+(b2_hat-b2)^2 );
V_arr(j) = -2 *abs(k)*(zk)^2;
end
Suggested solution to your problem
Going back to your comments about “The reference model and the controller are sampled every 0.3 seconds and the plant's system is sampled continuously.
I was asked to think of an idea to improve performance in a discrete time
Is it possible to add something to the controller \ to the system to reduce the error resulting from the delay besides changing the sampling time” and implementing it into your code, there are several approaches to improve the performance of the discrete time control system and reduce the error resulting from the delay. Please see solutions to your mentioned issues by updating your code.
Predictive Control:In your provided code, there is already a reference model (y_mk) that represents the desired output. To reduce the error resulting from the delay, you can update the control input (uk) using the predictive model.
% Update control input using predictive model
uk = h_hat * ddy_rk - k * zk + a1_hat * dy_pk + a2_hat * y_pk + b1_hat * (dy_pk^2) + b2_hat * (y_pk^2);
By incorporating the predictive model into the control input calculation, the controller can anticipate the future behavior of the system and adjust the control action accordingly
Adaptive Control: In your provided code, there are adaptive controller parameters (h_hat, a1_hat, a2_hat, b1_hat, b2_hat) that are updated based on the error (ek) and its derivatives. To reduce the error resulting from the delay, you can update these parameters using adaptive control techniques.
% Adaptive controller parameters - Update
dh_hat = -gamma * zk * ddy_rk;
da1_hat = -gamma * zk * dy_pk;
da2_hat = -gamma * zk * y_pk;
db1_hat = -gamma * zk * dy_pk^2;
db2_hat = -gamma * zk * y_pk^2;
% Update adaptive controller parameters
h_hat = h_hat + dh_hat * T;
a1_hat = a1_hat + da1_hat * T;
a2_hat = a2_hat + da2_hat * T;
b1_hat = b1_hat + db1_hat * T;
b2_hat = b2_hat + db2_hat * T;
By updating the adaptive controller parameters based on the error and its derivatives, the controller can continuously adjust its behavior to reduce the error resulting from the delay.
Error Compensation: Another approach is to directly compensate for the error resulting from the delay. This can be achieved by introducing additional terms in the control input calculation that specifically target the delay-related error. In the provided code, you can add a compensation term to the control input calculation.
% Add error compensation term to control input
uk = h_hat * ddy_rk - k * zk + a1_hat * dy_pk + a2_hat * y_pk + b1_hat * (dy_pk^2) + b2_hat * (y_pk^2) - 4 * dy_pk + 5 * y_pk;
By adding the error compensation term (- 4 * dy_pk + 5 * y_pk) to the control input calculation, the controller can directly compensate for the error resulting from the delay.
By implementing these approaches, you can enhance the performance of the control system and reduce the error resulting from the delay. Please let me know if you have any further questions.
Yovel
Yovel el 15 de Jul. de 2024
Hi, I don't understand.. you wrote down everything I did already..
I asked if you could implement your suggestion with x_hat on my code because I didn't really understand how to do it, Or if you have another idea how to "rush" the controller? to overcome the delay
Umar
Umar el 15 de Jul. de 2024
Hi Yovel,
It seems that Walter edited my code because I do recall inserting code with fixing your error observed in your recent code and now it’s gone. You asked,if yiu have another idea how to rush the controller to overcome the delay. I already mentioned in my previous post but I will post the same comments verbatim.
Suggested solution to your problem
Going back to your comments about “The reference model and the controller are sampled every 0.3 seconds and the plant's system is sampled continuously. I was asked to think of an idea to improve performance in a discrete time Is it possible to add something to the controller \ to the system to reduce the error resulting from the delay besides changing the sampling time” and implementing it into your code, there are several approaches to improve the performance of the discrete time control system and reduce the error resulting from the delay. Please see solutions to your mentioned issues by updating your code. Predictive Control:In your provided code, there is already a reference model (y_mk) that represents the desired output. To reduce the error resulting from the delay, you can update the control input (uk) using the predictive model. % Update control input using predictive model uk = h_hat * ddy_rk - k * zk + a1_hat * dy_pk + a2_hat * y_pk + b1_hat * (dy_pk^2) + b2_hat * (y_pk^2); By incorporating the predictive model into the control input calculation, the controller can anticipate the future behavior of the system and adjust the control action accordingly Adaptive Control: In your provided code, there are adaptive controller parameters (h_hat, a1_hat, a2_hat, b1_hat, b2_hat) that are updated based on the error (ek) and its derivatives. To reduce the error resulting from the delay, you can update these parameters using adaptive control techniques. % Adaptive controller parameters - Update dh_hat = -gamma * zk * ddy_rk; da1_hat = -gamma * zk * dy_pk; da2_hat = -gamma * zk * y_pk; db1_hat = -gamma * zk * dy_pk^2; db2_hat = -gamma * zk * y_pk^2; % Update adaptive controller parameters h_hat = h_hat + dh_hat * T; a1_hat = a1_hat + da1_hat * T; a2_hat = a2_hat + da2_hat * T; b1_hat = b1_hat + db1_hat * T; b2_hat = b2_hat + db2_hat * T; By updating the adaptive controller parameters based on the error and its derivatives, the controller can continuously adjust its behavior to reduce the error resulting from the delay. Error Compensation: Another approach is to directly compensate for the error resulting from the delay. This can be achieved by introducing additional terms in the control input calculation that specifically target the delay-related error. In the provided code, you can add a compensation term to the control input calculation. % Add error compensation term to control input uk = h_hat * ddy_rk - k * zk + a1_hat * dy_pk + a2_hat * y_pk + b1_hat * (dy_pk^2) + b2_hat * (y_pk^2) - 4 * dy_pk + 5 * y_pk; By adding the error compensation term (- 4 * dy_pk + 5 * y_pk) to the control input calculation, the controller can directly compensate for the error resulting from the delay. By implementing these approaches, you can enhance the performance of the control system and reduce the error resulting from the delay. Please let me know if you have any further questions.
Umar
Umar el 15 de Jul. de 2024

Hi Yovel,

I do understand your frustration not having this problem resolved. Could you provide details about what you want your code to do, I demonstrated examples in my previous code to help you understand the concept of implementing predictor or an observer in the control loop to reduce the error resulting from the delay because they can estimate the system's current state based on past information, compensating for delays. Predictors on one hand use past inputs and outputs to forecast the system's behavior, while observers on the other hand estimate the system's internal state. These tools help in making real-time adjustments, reducing errors caused by delays and enhancing system performance. When you wrote a lengthy code with no comments incorporated, it made my job little difficult to analyze your code but I still tried my best to analyze your code and provided my comments to improve your code. I do appreciate your efforts implementing an adaptive control algorithm, trying to calculate control inputs based on the error between a reference model and a plant model, and updating adaptive controller parameters to improve the control performance. To reduce the error in control loop, I will help you understand by providing example snippet code which will help you to implement this concept in your code to resolve the issue. So, I implemented an adaptive control algorithm in Matlab. The algorithm calculates control inputs based on the error between a reference model and a plant model. It then updates adaptive controller parameters to enhance control performance. Pay close attention to part of the snippet code below, “Calculate control input based on error”

% Adaptive Control Algorithm Implementation

% Define plant model parameters

A = [0.5, 0.2; 0.1, 0.3]; B = [1; 0]; C = [1, 0]; D = 0;

% Define reference model parameters

Am = [0.4, 0.1; 0.05, 0.35]; Bm = [1; 0]; Cm = [1, 0]; Dm = 0;

% Initialize adaptive controller parameters

theta = zeros(4, 1); % [theta1; theta2; theta3; theta4]

% Simulation loop

for k = 1:100

    % Calculate control input based on error
    e = ym(k) - y(k); % Calculate error between plant output y and reference model output ym
    u(k) = -theta(1)*e - theta(2)*y(k) - theta(3)*u(k-1); % Calculate control input u
    % Update adaptive controller parameters
    theta_dot = 0.1 * [e; y(k); u(k-1); u(k)] * e'; % Update rule for theta
    theta = theta + theta_dot; % Update theta parameters
end

You probably already know what is the above code snippet doing but I still want you to understand about the simulation loop where the code calculates the control input 'u' based on the error 'e' between the plant output 'y' and the reference model output 'ym'. The control input is updated using a linear combination of the error and previous control inputs.

Hopefully, now, following these steps and suggestions should help you answer your question, “ Is it possible to add something to the controller \ to the system to reduce the error resulting from the delay?”

Please let me know if you have any further questions.

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Centro de ayuda y File Exchange.

Productos

Versión

R2023b

Preguntada:

el 8 de Jul. de 2024

Comentada:

el 15 de Jul. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by