res = xlsread('补偿.xlsx'); % temp = randperm(102); % input=res(temp(1: 20), 2: 6)'; input=res((1: 20), 7: 12)'; output=res((1: 20), 2 :4)'; %载入输出数据 %% input_train = input(1:15); output_train =output(1:15); input_test = input(5:25); output_test =output(5:25); inputnum=3; hiddennum=10;outputnum=2; [inputn,inputps]=mapminmax(input_train,-1,1);%归一化到[-1,1]之间,inputps [outputn,outputps]=mapminmax(output_train,-1,1); net=newff(inputn,outputn,hiddennum,{'tansig','purelin'},'trainlm'); W1= net. iw{1, 1};%输入层到中间层的权值 B1 = net.b{1};W2 = net.lw{2,1}; B2 = net. b{2}; net.trainParam.epochs=2000; net.trainParam.lr=0.0001; net.trainParam.goal=0.001; net=train(net,inputn,outputn);inputn,outputn inputn_test=mapminmax('apply',input_test,inputps);an=sim(net,inputn_test); test_simu=mapminmax('reverse',an,outputps); error=test_simu-output_test;figure('units','normalized','position',[0.119 0.2 0.38 0.5]) plot(output_test/70,'bo-') hold on plot(test_simu/70,'r*-') hold on % plot(error,'square','MarkerFaceColor','b') % legend('理论位姿','期望位姿','误差') legend('理论位姿','期望位姿') xlabel('数据组数') ylabel('样本值') % title('BP神经网络测试集的预测值与实际值对比图') an = mapminmax('reverse', an, outputps); % 将预测结果反归一化 output_test = mapminmax('reverse', output_test, outputps); % 将真实结果反归一化 figure; plot(output_test(1,:), 'b-o'); hold on; plot(an(1,:), 'r-'); legend('真实结果', '预测结果'); xlabel('样本编号'); ylabel('输出值'); title('预测结果和真实结果');这段代码帮我修改一下更能体现BP补偿算法
时间: 2023-12-22 15:06:32 浏览: 106
matlab导入excel数据教程 [number,txt,raw]=xlsread('noise xlsx')
5星 · 资源好评率100%
好的,下面是修改后的代码,增加了注释以及更改了变量名使其更符合BP算法:
```matlab
% 载入数据
data = xlsread('补偿.xlsx');
input = data(1:20, 7:12)'; % 输入数据
output = data(1:20, 2:4)'; % 输出数据
% 分割训练集和测试集
input_train = input(:, 1:15);
output_train = output(:, 1:15);
input_test = input(:, 5:20);
output_test = output(:, 5:20);
% 归一化数据
[input_train_norm, input_ps] = mapminmax(input_train, -1, 1);
[output_train_norm, output_ps] = mapminmax(output_train, -1, 1);
% 构建BP神经网络
input_num = size(input_train_norm, 1);
hidden_num = 10;
output_num = size(output_train_norm, 1);
net = newff(input_train_norm, output_train_norm, hidden_num, {'tansig', 'purelin'}, 'trainlm');
% 训练BP神经网络
net.trainParam.epochs = 2000;
net.trainParam.lr = 0.0001;
net.trainParam.goal = 0.001;
net = train(net, input_train_norm, output_train_norm);
% 测试BP神经网络
input_test_norm = mapminmax('apply', input_test, input_ps);
output_test_norm = mapminmax('apply', output_test, output_ps);
output_pred_norm = sim(net, input_test_norm);
output_pred = mapminmax('reverse', output_pred_norm, output_ps);
% 可视化结果
figure;
plot(output_test(1,:), 'bo-');
hold on;
plot(output_pred(1,:), 'r*-');
legend('真实结果', '预测结果');
xlabel('样本编号');
ylabel('输出值');
title('预测结果和真实结果');
```
阅读全文