[MATLAB] Approximating nonlinear functions with simple neural networks

I. Introduction

Table of contents

I. Introduction

2. Simple explanation

3. Code

Four. Summary


        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{\color{Blue} g(x_{1},x_{2})=0.52*0.1x_{1}+0.28x_{2}-0.06x_{1}x_{2}}

        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!

Guess you like

Origin blog.csdn.net/ClushioAqua/article/details/130643869