Please can anyone help me to correct this code !!!!

5 vues (au cours des 30 derniers jours)
yousra yahia
yousra yahia le 7 Juin 2024
Commenté : Umar le 17 Juin 2024
% Définition des matrices de système
A = [0 0 1 -1 0 0; 0 1 0 0 1 0; 0 0 0 0 0 1];
B1 = [0 1 0; 0 0 0; 0 0 0];
B2 = [0 0 1; 0 0 0; 0 0 0];
C1 = [1 0 0; 0 1 0; 0 0 1];
C2 = [0 0 1; 0 0 0; 0 0 0];
D1 = [1 0 0; 0 1 0; 0 0 1];
% Définition des fonctions d'appartenance floues
f1 = @(t) (t - 0.5)^2;
f2 = @(t) t;
% Définition des règles floues
rule1 = 'IF f1(t) is "Long" and f2(t) is "Extended", THEN _x(t) = A1*x(t) + B1*x(t) + B2*sat(u(t) - s(t))';
rule2 = 'IF f1(t) is "Short" and f2(t) is "Compressed", THEN _x(t) = A2*x(t) + B1*x(t) + B2*sat(u(t) - s(t))';
rule3 = 'IF f1(t) is "Long" and f2(t) is "Compressed", THEN _x(t) = A3*x(t) + B1*x(t) + B2*sat(u(t) - s(t))';
rule4 = 'IF f1(t) is "Short" and f2(t) is "Extended", THEN _x(t) = A4*x(t) + B1*x(t) + B2*sat(u(t) - s(t))';
% Définition des gains de contrôle
P1 = [1 0 0; 0 1 0; 0 0 1];
P2 = [0 1 0; 0 0 1; 0 0 0];
P3 = [0 0 1; 0 0 0; 0 0 1];
P4 = [1 0 0; 0 1 0; 0 0 1];
% Définition de la fonction de contrôle
control = @(t, x) P1*x + P2*x + P3*sat(u(t) - s(t));
% Simulation
t = 0:0.01:10;
x0 = [1; 0; 0];
u = zeros(size(t));
s = 0.5*t;
z1 = zeros(size(t));
z2 = zeros(size(t));
for i = 1:length(t)
x = x0;
for j = 1:length(t)
if f1(t(j)) > 0.5 && f2(t(j)) > 0.5
x = A1*x + B1*x + B2*sat(u(j) - s(j));
elseif f1(t(j)) < 0.5 && f2(t(j)) < 0.5
x = A2*x + B1*x + B2*sat(u(j) - s(j));
elseif f1(t(j)) > 0.5 && f2(t(j)) < 0.5
x = A3*x + B1*x + B2*sat(u(j) - s(j));
else
x = A4*x + B1*x + B2*sat(u(j) - s(j));
end
end
z1(i) = C1*x;
z2(i) = C2*x;
u(i) = control(t(i), x);
end
Unrecognized function or variable 'A2'.
% Affichage des résultats
plot(t, z1, t, z2);
xlabel('Time (s)');
ylabel('State');
title('Active Suspension System');
And this the error message ' paper1
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of
rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in paper1 (line 42)
x = A2*x + B1*x + B2*sat(u(j) - s(j)); '

Réponses (3)

Umar
Umar le 7 Juin 2024
Modifié(e) : Walter Roberson le 11 Juin 2024
A = [0 0 1 -1 0 0; 0 1 0 0 1 0; 0 0 0 0 0 1]; B1 = [0 1 0; 0 0 0; 0 0 0]; B2 = [0 0 1; 0 0 0; 0 0 0]; C1 = [1 0 0; 0 1 0; 0 0 1]; C2 = [0 0 1; 0 0 0; 0 0 0]; D1 = [1 0 0; 0 1 0; 0 0 1];
% Definition of fuzzy membership functions f1 = @(t) (t - 0.5)^2; f2 = @(t) t;
% Definition of fuzzy rules rule1 = 'IF f1(t) is "Long" and f2(t) is "Extended", THEN _x(t) = A1*x(t) + B1*x(t) + B2*sat(u(t) - s(t))'; rule2 = 'IF f1(t) is "Short" and f2(t) is "Compressed", THEN _x(t) = A2*x(t) + B1*x(t) + B2*sat(u(t) - s(t))'; rule3 = 'IF f1(t) is "Long" and f2(t) is "Compressed", THEN _x(t) = A3*x(t) + B1*x(t) + B2*sat(u(t) - s(t))'; rule4 = 'IF f1(t) is "Short" and f2(t) is "Extended", THEN _x(t) = A4*x(t) + B1*x(t) + B2*sat(u(t) - s(t))';
% Definition of control gains P1 = [1 0 0; 0 1 0; 0 0 1]; P2 = [0 1 0; 0 0 1; 0 0 0]; P3 = [0 0 1; 0 0 0; 0 0 1]; P4 = [1 0 0; 0 1 0; 0 0 1];
% Definition of control function control = @(t, x) P1*x + P2*x + P3*sat(u(t) - s(t));
% Simulation t = 0:0.01:10; x0 = [1; 0; 0]; u = zeros(size(t)); s = 0.5*t; z1 = zeros(size(t)); z2 = zeros(size(t));
for i = 1:length(t) x = x0; for j = 1:length(t) if f1(t(j)) > 0.5 && f2(t(j)) > 0.5 x = A*x + B1*x + B2*sat(u(j) - s(j)); elseif f1(t(j)) < 0.5 && f2(t(j)) < 0.5 x = A*x + B1*x + B2*sat(u(j) - s(j)); elseif f1(t(j)) > 0.5 && f2(t(j)) < 0.5 x = A*x + B1*x + B2*sat(u(j) - s(j)); else x = A*x + B1*x + B2*sat(u(j) - s(j)); end end z1(i) = C1*x; z2(i) = C2*x; u(i) = control(t(i), x); end
% Display the results plot(t, z1, t, z2); xlabel('Time (s)'); ylabel('State'); title('Active Suspension System');
  14 commentaires
Umar
Umar le 12 Juin 2024
Hi Sam, Thank you for reaching out. I appreciate your effort in revisiting the problem. As team working together, we can help resolve issues to make difference in people lives. Again, great teamwork.
yousra yahia
yousra yahia le 14 Juin 2024
hello @Umar and @Sam Chak this is the paper which i am trying to simulate its système .

Connectez-vous pour commenter.


Umar
Umar le 14 Juin 2024
Hi Yousra, I don’t have access to R2024a but I tried my best to resolve your issue. The error message suggests that there is an issue with the dimensions of the matrices involved in the multiplication operation A2*x. To resolve this error, we need to ensure that the dimensions of the matrices are compatible for matrix multiplication.
Looking at the provided code, it seems that the matrix A2 is not defined. This is likely the cause of the error. To fix this, we need to define the matrix A2 before using it in the multiplication operation.
I will provide modified code with the addition of the missing matrix A2 in a separate comment.
Please note that I have assumed that the matrices A1, A3, and A4 are defined elsewhere in the code, as they are used in the if-else conditions. If they are not defined, you will need to define them as well.
By adding the missing matrix A2 and ensuring that all required matrices are defined, the error related to matrix multiplication should be resolved.
  10 commentaires
Sam Chak
Sam Chak le 17 Juin 2024
Hi @Umar,
No worries at all. If the OP doesn't proactively respond to our request for info (RFI) or technical advice, there's not a whole lot we can do about that. The onus is really on her to take the data or information from the paper and incorporate it into the MATLAB code, so we can run tests and check for any programming errors.
In fact, our back-and-forth has been way more engaged than what we've seen from the OP so far. Oh well, I guess we can only wish her the best.
Umar
Umar le 17 Juin 2024
Hi Sam, Understood. Moving forward, we will continue to monitor the situation and remain open to any further input or feedback from the OP. Thank you once again for keeping me informed and for your efforts.

Connectez-vous pour commenter.


Umar
Umar le 14 Juin 2024
The code is structured as follows:
Definition of the system matrices A, B1, B2, C1, C2, and D1. Definition of fuzzy membership functions using anonymous functions. Definition of fuzzy rules using strings. Definition of control gains using matrices. Definition of the control function using an anonymous function. Simulation setup: time vector, initial state, and preallocation of variables. Simulation loop: iterates over time steps and applies fuzzy control based on the fuzzy rules. Calculation of system outputs and control inputs. Plotting of the results.
The above code provides a simulation of an active suspension system using fuzzy control. By modeling the system using differential equations and applying fuzzy logic for control, the code allows for the adjustment of the suspension in real-time to improve ride comfort and handling. The code structure is organized and easy to understand, making it a useful tool for studying and analyzing active suspension systems.

Catégories

En savoir plus sur Fuzzy Logic in Simulink dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by