what should i do for this error ?

clear all;
close all;
clc;
% Define system parameters
g = 9.81; % Acceleration due to gravity
l = 1; % Length of the pendulum
m = 1; % Mass of the pendulum
% Define the fuzzy controller
fis = mamfis('Name','InvertedPendulumController');
% Add input variables and membership functions
fis = addInput(fis, [-pi/4, pi/4], 'Name', 'Error');
fis = addMF(fis, 'Error', 'trimf', [-pi/4, -pi/8, 0]);
fis = addMF(fis, 'Error', 'trimf', [-pi/8, 0, pi/8]);
fis = addMF(fis, 'Error', 'trimf', [0, pi/8, pi/4]);
fis = addInput(fis, [-pi/2, pi/2], 'Name', 'ChangeInError');
fis = addMF(fis, 'ChangeInError', 'trimf', [-pi/2, -pi/4, 0]);
fis = addMF(fis, 'ChangeInError', 'trimf', [-pi/4, 0, pi/4]);
fis = addMF(fis, 'ChangeInError', 'trimf', [0, pi/4, pi/2]);
% Add output variable and membership functions
fis = addOutput(fis, [-10, 10], 'Name', 'ControlInput');
fis = addMF(fis, 'ControlInput', 'trimf', [-10, -5, 0]);
fis = addMF(fis, 'ControlInput', 'trimf', [-5, 0, 5]);
fis = addMF(fis, 'ControlInput', 'trimf', [0, 5, 10]);
% Define rule base
ruleList = [
1 1 1 2 1;
2 2 1 1 1;
3 3 1 3 1;
2 1 2 2 1;
3 2 2 1 1;
1 3 2 3 1;
3 1 3 3 1;
1 2 3 1 1;
2 3 3 2 1;
];
fis = addRule(fis, ruleList);
Error using FuzzyInferenceSystem/addRule
Rule weight must be a scalar between 0 and 1, inclusive.
% Simulation parameters
tspan = 0:0.01:10; % Time span
x0 = [0; 0]; % Initial conditions
% Define the system dynamics function
sys = @(t, x, u) [x(2); (g/l)*sin(x(1)) + (1/(m*l^2))*(u)];
% Simulate the system with control
[t, x] = ode45(@(t, x) sys(t, x, evalfis([x(1), x(2)], fis)), tspan, x0);
% Plot the results
figure;
plot(t, x(:,1), 'b', 'LineWidth', 1.5);
xlabel('Time');
ylabel('Angle');
title('Inverted Pendulum Control');
grid on;
figure;
plot(t, x(:,2), 'r', 'LineWidth', 1.5);
xlabel('Time');
ylabel('Angular Velocity');
title('Inverted Pendulum Control');
grid on;
% Function to evaluate the control input using the fuzzy controller
function u = evalfis(inputs, fis)
error = inputs(1);
changeInError = inputs(2);
u = evalfis([error, changeInError], fis);
end

回答 (1 件)

Cris LaPierre
Cris LaPierre 2023 年 5 月 17 日

0 投票

Your second input to addRule does not appear to use the syntax expected by Numeric Rule descriptions in the addRule function in the Fuzzy Inference System. You can read about the expected syntax here: https://www.mathworks.com/help/fuzzy/mamfis.addrule.html?s_tid=doc_ta#mw_b4c719d8-51ee-44de-b7f5-70bb6b1a3f90
Specifically, your column 4 values indicate the rule weight, and as the error message is stating, these must be between 0 and 1.

カテゴリ

ヘルプ センター および File ExchangeFuzzy Logic in Simulink についてさらに検索

質問済み:

2023 年 5 月 17 日

回答済み:

2023 年 5 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by