-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z2Neg.m
137 lines (104 loc) · 3.65 KB
/
Z2Neg.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
%% I. 清空环境变量
clear
clc
%% II. 训练集/测试集产生
% 1. 导入数据
data = load('PyCodes/Z2Neg.txt');
for i = 1:20
% 2. 随机产生训练集和测试集
temp = randperm(size(data,1)); % randperm 整数的随机排列
% 训练集
% P_train = data(1:324*75,1:4)';
% T_train = data(1:324*75,5:7)';
% 加入随机效果
P_train = data(temp(1:324*85),1:4)';
T_train = data(temp(1:324*85),5:7)';
% 测试集
% P_test = data(324*75+1:324*100,1:4)';
% T_test = data(324*75+1:324*100,5:7)';
% 加入随机效果
P_test = data(temp(324*85+1:324*100),1:4)';
T_test = data(temp(324*85+1:324*100),5:7)';
% 有了随机,效果更好
%% IV. ELM创建/训练
L = 600;
[IW,B,LW,TF,TYPE] = elmtrain(P_train,T_train,L,'sig',0);
%% V. ELM仿真测试
T_sim = elmpredict(P_test,IW,B,LW,TF,TYPE);
%% VI. 结果对比
result = [T_test' T_sim'];
% 1. 均方误差
E = mse(T_sim - T_test);
disp(string(i) +'---'+ string(E))
if E<65000
break
end
end
% 2. 决定系数
N = length(T_test);
% R2=(N*sum(T_sim.*T_test)-sum(T_sim)*sum(T_test))^2/((N*sum((T_sim).^2)-(sum(T_sim))^2)*(N*sum((T_test).^2)-(sum(T_test))^2));
R2 = 1;
%% VII. 绘图
figure(1)
subplot(2,2,1)
plot(1:N,T_test(1,:),'red-*',1:N,T_sim(1,:),'blue:o');
grid on
legend('真实值','预测值')
xlabel('样本编号')
string = {'X';['(mse = ' num2str(E) ' R^2 = ' num2str(R2) ')']};
title(string)
subplot(2,2,2)
plot(1:N,T_test(2,:),'red-*',1:N,T_sim(2,:),'blue:o');
grid on
legend('真实值','预测值')
subplot(2,2,3)
plot(1:N,T_test(3,:),'red-*',1:N,T_sim(3,:),'blue:o');
grid on
legend('真实值','预测值')
%% 附件 2 数据及验证结果
data6 = [2230 3230 4910 5180]';
data7 = [4520 1990 5600 3360]';
data8 = [2480 3530 4180 5070]';
data9 = [4220 2510 4670 3490]';
data10= [5150 2120 5800 2770]';
alldata = [data6 data7 data8 data9 data10];
%% 加载权重矩阵
TF = 'sig';
TYPE = 0;
for i = 1:5
data = alldata(:,i);
predict(:,i) = elmpredict(data,IW,B,LW,TF,TYPE);
end
%%
A0 = [ 0, 0, 1300];
A1 = [5000, 0, 1700];
A2 = [ 0, 5000, 1700];
A3 = [5000, 5000, 1300];
figure(2)
scatter3(A0(1),A0(2),A0(3), 300, 'p', 'linewidth', 1.5); hold on; % Anchor A0
scatter3(A1(1),A1(2),A1(3), 300, 'p', 'linewidth', 1.5); % Anchor A1
scatter3(A2(1),A2(2),A2(3), 300, 'p', 'linewidth', 1.5); % Anchor A2
scatter3(A3(1),A3(2),A3(3), 300, 'p', 'linewidth', 1.5); % Anchor A3
scatter3(predict(1,1), predict(2,1), predict(3,1), 200, 'linewidth', 1.5); hold on; % data1
scatter3(predict(1,2), predict(2,2), predict(3,2), 200, 'linewidth', 1.5); hold on; % data2
scatter3(predict(1,3), predict(2,3), predict(3,3), 200, 'linewidth', 1.5); hold on; % data3
scatter3(predict(1,4), predict(2,4), predict(3,4), 200, 'linewidth', 1.5); hold on; % data4
scatter3(predict(1,5), predict(2,5), predict(3,5), 200, 'linewidth', 1.5); hold on; % data5
axis([-2000 7000 -2000 7000 0000 4000]); % 设置图显示范围
text(A0(1),A0(2),A0(3), ' A0'); % 添加文字标识
text(A1(1),A1(2),A1(3), ' A1');
text(A2(1),A2(2),A2(3), ' A2');
text(A3(1),A3(2),A3(3), ' A3');
text(predict(1,1), predict(2,1), predict(3,1), ' data6');
text(predict(1,2), predict(2,2), predict(3,2), ' data7');
text(predict(1,3), predict(2,3), predict(3,3), ' data8');
text(predict(1,4), predict(2,4), predict(3,4), ' data9');
text(predict(1,5), predict(2,5), predict(3,5), ' data10');
line([A0(1),A1(1)], [A0(2),A1(2)], [A0(3),A1(3)]); % 绘制线段
line([A0(1),A2(1)], [A0(2),A2(2)], [A0(3),A2(3)]);
line([A2(1),A3(1)], [A2(2),A3(2)], [A2(3),A3(3)]);
line([A3(1),A1(1)], [A3(2),A1(2)], [A3(3),A1(3)]);
xlabel('X/mm');
ylabel('Y/mm');
zlabel('Z/mm');
title('附件2有信号干扰时模型定位图');