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

% 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)); '

Answers (3)

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 Comments

Hi Umar, please use proper format for the code so that the OP (yousra yahia) can run the Takagi-Sugeno Fuzzy parallel distributed compensated system.
Hi Sam: Could you please help, I will appreciate that.
I noticed you have posted code in the Answer section. Is this intended as a solution to the "undisclosed" error mentioned in the query? If it is not, I can help by moving your Post to the Comment section. Let me know, please.
Additionally, the OP has posed an Unclosed Question related to fuzzy logic a few months ago. Perhaps you could refer to solution of how the inferences of the rules are typically performed in such systems.
Unfortunately, without more complete information from the OP and lack of constructive feedback, I am unable to offer further assistance at this time.
Dear Sam, Thank you for bringing this to my attention. I apologize for the confusion regarding the code posted in the Answer section. It was not intended as a solution to the "undisclosed" error mentioned in the query. I appreciate your offer to move it to the Comment section. Regarding the Unclosed Question related to fuzzy logic that was posed by the OP a few months ago, I will refer to solutions on how inferences of rules are typically performed in such systems. Thank you for providing the link for reference. I understand that without more complete information from the OP and lack of constructive feedback, further assistance may be limited at this time. Please feel free to reach out if there are any updates or additional details provided by the OP. Thank you again for your attention to detail and willingness to help with clarifying these matters.
In fuzzy logic systems, the inference process involves determining the degree to which each rule contributes to the overall output. This process is crucial for making decisions based on fuzzy rules and input data. Let's delve into the detailed steps involved in the inference process:
1. Fuzzification
The first step in the inference process is fuzzification, where crisp input values are converted into fuzzy sets. This conversion allows for the representation of uncertainty and imprecision in the input data.
2. Rule Evaluation
After fuzzification, each rule in the rule base is evaluated to determine the degree to which it is satisfied by the input data. This evaluation involves calculating the degree of membership of the input values in the antecedent of each rule.
3. Aggregation
Once the rules are evaluated, the next step is to aggregate the outputs of all rules to obtain a combined output. This aggregation process can involve methods like max-min, max-product, or other aggregation operators.
4. Defuzzification
The final step in the inference process is defuzzification, where the aggregated fuzzy output is converted back into a crisp value. This step is essential for obtaining a clear and interpretable output from the fuzzy system.
% Fuzzification input_value = 7; fuzzy_set = trimf(input_value, [5 7 9]);
% Rule Evaluation rule_antecedent = [fuzzy_set_A fuzzy_set_B]; degree_of_membership = min(rule_antecedent);
% Aggregation aggregated_output = max(aggregated_output, rule_output);
% Defuzzification defuzzified_output = centroid_method(aggregated_output);
By following these detailed steps in the inference process of a fuzzy logic system, one can effectively make decisions based on fuzzy rules and input data.
Hello @Sam Chak and @Sam Chak , thank you both for your effort to try to help me. What kind of informations do you need about the code ?
@Umar@Sam Chak , i tried your anwer below with matlab , i have the same error message.
Hi Yousra, what kind of error message are you getting?
this is the error message "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 38)
elseif f1(t(j)) < 0.5 && f2(t(j)) < 0.5 x = A*x + B1*x + B2*sat(u(j) - s(j));
Your A is 3 x 6. Your x is 3 x 1. You cannot use "*" to multiply a 3 x 6 and a 3 x 1.
You could use ".*" to perform the multiplication, but the result would be 3 x 6.
It is not clear what size of result you want. Perhaps you want A*x' which would give 3 x 3. Or perhaps you want x'*A which would give 1 x 6.
Hi Yousra: please let us know if you are still having trouble, we will be more happy to assist you with answer to best of our knowledge.
I revisited this problem and believe it is unrelated to the unclosed answered question (Fuzzy system):
Moreover, the OP has not mentioned that the incomplete code is related to the Takagi-Sugeno Fuzzy system. At first glance, the following expression in the conditional If-Else section appears to be a form of state-space representation , but it is not, because the matrix A in the code is not a square matrix:
If you genuinely want to help the OP, please ensure you have access to the FULL mathematical description of the Active Suspension System. Only then can you effectively translate the mathematical equations into MATLAB code.
Additionally, the code uses a custom function 'sat()' which is not provided; I assume it is a saturation function to constrain the output of the control law u. Good engineering practice suggests simulating systems in their unconstrained form first. If successful, one should then proceed to implement more advanced features.
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.
hello @Umar and @Sam Chak this is the paper which i am trying to simulate its système .

Sign in to comment.

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 Comments

Sam Chak
Sam Chak on 15 Jun 2024
Edited: Sam Chak on 15 Jun 2024
I see what you're getting at by emphasizing the need to reiterate @Walter Roberson's comment in order to resolve the matrix dimension issue. It's a fair point, especially since the OP didn't provide any constructive feedback to address those technical concerns, much like in some of her other recent questions that were left unclosed, despite Answers were provided.
She posted a paper in this comment. Perhaps you can use your expertise to summarize the entire paper and then identify the equations or info for those missing matrices A1, A2, A3, and A4.
Thanks for your input,Sam
Hi Umar,
The major issue is that I don't see how the fuzzy rules in "strings" are used within the code. There are no membership functions.
I'd suggest to simulate the mathematical model of the Active Suspension System without the Controller. If successful, only then add the Controller in stages.
I have found this one from the paper. This should be the state-space mathematical model for Actice Suspension System. The size of the state matrix is , which is different from your fuzzy solution code in your Answer.
% Define system parameters m = 250; % mass of the car (kg) k1 = 8000; % spring constant 1 (N/m) k2 = 20000; % spring constant 2 (N/m) c1 = 350; % damping coefficient 1 (Ns/m) c2 = 1500; % damping coefficient 2 (Ns/m)
% State-space representation A = [0 1 0 0; -(k1 + k2)/m -(c1 + c2)/m k2/m c2/m; 0 0 0 1; k2/m c2/m -k2/m -c2/m]; B = [0; 1/m; 0; 0]; C = [1 0 0 0; 0 0 1 0]; D = 0;
% Create state-space model sys = ss(A, B, C, D);
% Simulate the system response to an input t = 0:0.01:5; % time vector u = 0.1*ones(size(t)); % input (road disturbance) x0 = [0; 0; 0; 0]; % initial state [y, t, x] = lsim(sys, u, t, x0);
% Plot the response figure; subplot(2,1,1); plot(t, y(:,1), 'b', 'LineWidth', 1.5); xlabel('Time (s)'); ylabel('Displacement (m)'); title('Car Body Displacement'); grid on;
subplot(2,1,2); plot(t, y(:,2), 'r', 'LineWidth', 1.5); xlabel('Time (s)'); ylabel('Velocity (m/s)'); title('Car Body Velocity'); grid on;
we first define the system parameters such as mass, spring constants, and damping coefficients of the Active Suspension System. We then construct the state-space representation of the system using these parameters.
The state-space matrices A, B, C, and D are defined based on the system dynamics. A represents the system dynamics, B the input matrix, C the output matrix, and D the feedthrough matrix.
We create a state-space model 'sys' using the ss function in Matlab. Next, we simulate the system response to a road disturbance input 'u' over a specified time vector 't' with an initial state 'x0' using the lsim function.
Finally, we plot the response of the system showing the car body displacement and velocity over time using the generated data.
This code allows for the simulation and visualization of the Active Suspension System's behavior, providing insights into its dynamic response under different conditions.
But your state space matrices A, B, C, D are incorrect. It cannot simulate the Active Suspension System because the model requires B1, B2, C1, C2, C3.
Can you enter the correct VALUES for the matrices?
Hi Sam, it was just a simulated code and suggestion to motivate OP implement rest of code. I have done my best. It would be helpful if you can help with remainder of code.
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.
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.

Sign in to comment.

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.

Categories

Find more on Fuzzy Logic Toolbox in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 7 Jun 2024

Commented:

on 17 Jun 2024

Community Treasure Hunt

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

Start Hunting!