I. Introduction
Table of contents
In the robot control system design and matlab simulation class, the teacher assigned a homework, using neural network and fuzzy method to approximate nonlinear functions
The fuzzy method can be obtained by directly writing codes based on the formulas obtained by the product inference machine, single-value fuzzer and central average defuzzifier, but the neural network has no idea. After consulting the information, a simple neural network approximation function was written.
2. Simple explanation
But using matlab to train yourself does not need to be so troublesome. We mainly use the newff() function.
You can read other blogs about the newff function, for example: newff
Create a neural network and use the train function to input and output to start training the model, and then use the sim function to input and output. Finally, the two models are expressed to indicate the error.
3. Code
clc;
close all;
x1=-1:0.05:1; %生成训练数据x
x2=-1:0.05:1;
[X1,X2]=meshgrid(x1,x2);
% P = [X1,X2];
yd=0.52+0.1*X1+0.28*X2-0.06*X1.*X2;
net = newff([X1;X2],yd,[8 8],{},"trainbr");
net.trainParam.lr=0.01; %学习率设置
net.trainParam.show=25; %显示的间隔次数
net.trainParam.epochs = 500; %训练最大次数
net.trainParam.goal=10^-10; %训练目标设置
net.trainParam.max_fail = 30;
net = train(net,[X1;X2],yd);
Y = sim(net,[X1;X2]);
subplot(211);
surf(X1,X2,yd);
title("原来函数0.52+0.1*X1+0.28*X2-0.06*X1.*X2");
subplot(212);
surf(X1,X2,Y);
title("神经网络逼近");
figure;
surf(X1,X2,Y-yd);
title("误差");
Four. Summary
Because what I listened to in this course is also in the fog, and I just started learning, if I make any mistakes, please correct me!