clear all
close all
clc
% clear workspace
m1 = 10;
m2 = 5.6;
l1 = 0.5;
l2 = 0.25;
g=9.81;
% basic equations
number = 1000;
th1d = 2*pi*rand(1,number)-pi;
th1dotd = 10*rand(1,number)-5;
th1ddotd = 20*rand(1,number)-10;
th2d = 2*pi*rand(1,number)-pi;
th2dotd = 10*rand(1,number)-5;
th2ddotd = 20*rand(1,number)-10;
% parameters
cth1=cos(th1d);
sth2 = sin(th2d);
cth2 = cos(th2d);
h11=1/3*m1*l1*l1+m2*l1*l1+1/3*m2*l2*l2+m2*l1*l2*cth2;
h12=1/3*m2*l2*l2+1/2*m2*l1*l2*cth2;
h22=1/3*m2*l2*l2;
h=1/2*m2*l1*l2*sth2;
h1=-(2*h*th1dotd*th2dotd+h*th2dotd*th2dotd);
h2=h*th1dotd*th1dotd;
c1=(0.5*m1+m2)*g*l1*cth1+0.5*m2*g*l2*cos(th1d+th2d);
c2=0.5*m2*g*l2*cos(th1d+th2d);
%generating the inputs
torq = [h11 h12;h12 h22]*[th1ddotd;th2ddotd]+[h1;h2]+[c1;c2];
% generating the outputs according to the euqation
net=feedforwardnet(5);
% one hidden layer with 10 neurons
%%
% Set training parameters
net.trainParam.show=50;
net.trainParam.lr=0.05;
net.trainParam.mc=0.95;
net.trainParam.epochs=1000;
net.trainParam.goal=1e-10;
net.trainParam.max_fail=20; % validation check counts
% ==============
% Start training the NN
% ==============
net=train(net,th1ddotd,th2ddotd,torq); %inputs and outputs should be 1 row for each i/p and o/p
% % save the network before it was trained just to show the difference
% netb=net;
% ==============
% Save the training results
% ==============
运行之后显示串联数组维度不一致,这是为什么,从h1就开始显示,但是h1甚至不是矩阵?
26 行 th1dotd 这种乘法,矩阵乘法要求行列匹配,比如 1x1000 1000x1
你这种要么改成 A * A' 或者 A' * A ,如果是元素相乘,则是 A.*A