使用matlab编程实现。对非线性系统x为系统输入,输入范围为[-2π,2π],y为系统输出,z为系统输出的测量值。y(x)=3sin(x)+2cos(x^2),z(x)=y(x)+v(t),v(t)为测量噪声,是二个白噪声之和。其一为标准差为Q×y(t)、均值为0的白噪声,其二为标准差为R、均值为0的白噪声。Q与R的值自行选取,Q不小于5%,R不小于0.5。针对该系统,产生一组数据,选取合适的数据点,划分训练样本集以及测试样本集。然后利用模糊系统辨识方法,使用FCM算法建立模糊系统模型,并比较在相同输入下,实际系统输出与模糊系统模型输出的差异。不使用工具箱
时间: 2024-02-05 19:11:17 浏览: 119
首先,我们需要生成数据并划分训练样本集和测试样本集。我们可以使用 linspace 函数生成输入 x 的值,并计算出对应的输出 y 和测量值 z。根据要求,我们选择 Q=0.05, R=0.5。
```matlab
Q = 0.05;
R = 0.5;
x = linspace(-2*pi, 2*pi, 201);
y = 3*sin(x) + 2*cos(x.^2);
v1 = Q*y.*randn(size(x));
v2 = R*randn(size(x));
z = y + v1 + v2;
train_ratio = 0.7;
train_idx = randperm(length(x), round(train_ratio*length(x)));
test_idx = setdiff(1:length(x), train_idx);
x_train = x(train_idx);
z_train = z(train_idx);
x_test = x(test_idx);
z_test = z(test_idx);
```
然后,我们使用 FCM 算法建立模糊系统模型。这里我们使用三角隶属度函数,模糊集合个数为 5。
```matlab
mf_num = 5;
mf_centers = linspace(min(x_train), max(x_train), mf_num);
mf_width = (max(x_train) - min(x_train)) / (mf_num - 1);
mf_var = mf_width^2;
mf = zeros(length(x_train), mf_num);
for i = 1:mf_num
mf(:,i) = trimf(x_train, [mf_centers(i)-mf_width, mf_centers(i), mf_centers(i)+mf_width]);
end
max_iter = 100;
tol = 1e-5;
U = zeros(length(x_train), mf_num);
U(:,1) = 1;
for i = 1:max_iter
U_prev = U;
% Update centroids
mf_centers = sum((U.^2).*x_train, 1) ./ sum(U.^2, 1);
% Update membership function
for j = 1:length(x_train)
for k = 1:mf_num
dist = abs(x_train(j) - mf_centers(k));
U(j,k) = 1 / sum((dist./mf_var).^(2/(mf_num-1)));
end
end
% Check for convergence
if max(abs(U(:) - U_prev(:))) < tol
break;
end
end
mf = U.^2;
```
最后,我们可以使用模糊系统模型进行预测,并比较预测结果和测试样本集上的真实输出。
```matlab
y_pred = sum(mf.*y_train, 2) ./ sum(mf, 2);
figure;
plot(x_test, z_test, 'b');
hold on;
plot(x_test, y_pred, 'r');
legend('True output', 'Fuzzy model output');
xlabel('x');
ylabel('y');
title('Comparison of true and fuzzy model outputs');
```
完整代码如下:
```matlab
Q = 0.05;
R = 0.5;
x = linspace(-2*pi, 2*pi, 201);
y = 3*sin(x) + 2*cos(x.^2);
v1 = Q*y.*randn(size(x));
v2 = R*randn(size(x));
z = y + v1 + v2;
train_ratio = 0.7;
train_idx = randperm(length(x), round(train_ratio*length(x)));
test_idx = setdiff(1:length(x), train_idx);
x_train = x(train_idx);
z_train = z(train_idx);
x_test = x(test_idx);
z_test = z(test_idx);
mf_num = 5;
mf_centers = linspace(min(x_train), max(x_train), mf_num);
mf_width = (max(x_train) - min(x_train)) / (mf_num - 1);
mf_var = mf_width^2;
mf = zeros(length(x_train), mf_num);
for i = 1:mf_num
mf(:,i) = trimf(x_train, [mf_centers(i)-mf_width, mf_centers(i), mf_centers(i)+mf_width]);
end
max_iter = 100;
tol = 1e-5;
U = zeros(length(x_train), mf_num);
U(:,1) = 1;
for i = 1:max_iter
U_prev = U;
% Update centroids
mf_centers = sum((U.^2).*x_train, 1) ./ sum(U.^2, 1);
% Update membership function
for j = 1:length(x_train)
for k = 1:mf_num
dist = abs(x_train(j) - mf_centers(k));
U(j,k) = 1 / sum((dist./mf_var).^(2/(mf_num-1)));
end
end
% Check for convergence
if max(abs(U(:) - U_prev(:))) < tol
break;
end
end
mf = U.^2;
y_pred = sum(mf.*y_train, 2) ./ sum(mf, 2);
figure;
plot(x_test, z_test, 'b');
hold on;
plot(x_test, y_pred, 'r');
legend('True output', 'Fuzzy model output');
xlabel('x');
ylabel('y');
title('Comparison of true and fuzzy model outputs');
```
阅读全文