📄 bp222.m
字号:
%%%%%%%%%%%%%学习样本
%function o1o2=xyo1o2(a,b)
%由循环程序给出机器人关节变量(o1,02),通过正解运算求出相应的末端位姿(x,y),从而得到学习样本
clear
clc
l1=1; %连杆长度50cm
l2=0.8;
n=100;
g=zeros(2,n);
o1=pi/20:pi/20:pi/2;
o2=pi/40:pi/40:pi/4;
for i=1:1:10;
for j=1:1:10;
g(1,j+10*(i-1))=o1(i);
g(2,j+10*(i-1))=o2(j);
end
end
for i=1:1:n
o1=g(1,i);
o2=g(2,i);
x=l1*cos(o1)+l2*cos(o1+o2);
y=l1*sin(o1)+l2*sin(o1+o2);
s(1,i)=x;
s(2,i)=y;
end
g ; %关节变量
s ; %末端位姿
x1=s(1,:);
x2=s(2,:);
th1=g(1,:);
th2=g(2,:);
th=[th1;th2]; %期望值
s=[x1;x2]; %样本输入
%%%%%%%%%%%%%%%BP神经网络的仿真
n=10;%设置网络隐单元的神经元数目
%建立相应的BP网络 传递函数分别为tansig、purelin,训练函数(反向传递函数)是trainlm
net=newff(minmax(s),[n 2],{'tansig' 'purelin'},'trainlm');
%训练网络
net.trainParam.epochs=2000;
net.trainParam.goal=0.0001;
tic
net=train(net,s,th)
%对训练后的网络进行仿真
y2=sim(net,s)
y21=y2(1,:);
y22=y2(2,:);
figure;
plot(th1,'ko')
hold on;
plot(y21,'b*')
legend('期望输出','网络输出');
xlabel('样本值')
ylabel('th1')
title('th1的期望与仿真输出曲线')
pause
figure;
e1=th1-y21;
plot(e1,'black-')
axis([0 100 -0.5 0.5])
%legend('期望输出','网络输出');
xlabel('样本点')
ylabel('e1')
title('o1的e1仿真输出曲线')
pause
figure;
plot(th2,'ko')
hold on;
plot(y22,'r*')
legend('期望输出','网络输出');
xlabel('样本值')
ylabel('th2')
title('th2的期望与仿真输出曲线')
pause
figure;
e2=th2-y22;
plot(e2,'black-')
axis([0 100 -0.5 0.5])
%legend('期望输出','网络输出');
xlabel('样本点')
ylabel('e2')
title('o2的e2仿真输出曲线')
a=input('enter x(-0.5 to 1.7):')
while (x>1.5) | (x<-1.5)
x=input('x must in -1.5 to 1.5,Please re-type:')
end
b=input('enter y(0.3 to 1.7):')
while (y>1.6) | (y<0)
y=input('y must in 0 to 1.6,Please re-type:')
end
s.test=[a;b];
o1o2=sim(net,s.test)
tic
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -