📄 demogrn1.html
字号:
<!--This HTML is auto-generated from an m-file.Your changes will be overwritten.--><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:x-large">GRNN Function Approximation</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">This demonstration uses functions NEWGRNN and SIM.</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Copyright 1992-2002 The MathWorks, Inc.$Revision: 1.7 $ $Date: 2002/03/29 19:36:24 $</p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Here are eight data points of a function we would like to fit. The functionsinputs P should result in target outputs T.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">P = [1 2 3 4 5 6 7 8];T = [0 1 2 3 2 1 2 1];plot(P,T,<span style="color:#B20000">'.'</span>,<span style="color:#B20000">'markersize'</span>,30)axis([0 9 -1 4])title(<span style="color:#B20000">'Function to approximate.'</span>)xlabel(<span style="color:#B20000">'P'</span>)ylabel(<span style="color:#B20000">'T'</span>)</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="demogrn1_img02.gif"><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">We use NEWGRNN to create a generalized regression network. We use a SPREADslightly lower than 1, the distance between input values, in order, to get afunction that fits individual data points fairly closely. A smaller spreadwould fit data better but be less smooth.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">spread = 0.7;net = newgrnn(P,T,spread);A = sim(net,P);hold onoutputline = plot(P,A,<span style="color:#B20000">'.'</span>,<span style="color:#B20000">'markersize'</span>,30,<span style="color:#B20000">'color'</span>,[1 0 0]);title(<span style="color:#B20000">'Create and test a network.'</span>)xlabel(<span style="color:#B20000">'P'</span>)ylabel(<span style="color:#B20000">'T and A'</span>)</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="demogrn1_img03.gif"><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">We can use the network to approximate the function at a new input value.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">p = 3.5;a = sim(net,p);plot(p,a,<span style="color:#B20000">'.'</span>,<span style="color:#B20000">'markersize'</span>,30,<span style="color:#B20000">'color'</span>,[1 0 0]);title(<span style="color:#B20000">'New input value.'</span>)xlabel(<span style="color:#B20000">'P and p'</span>)ylabel(<span style="color:#B20000">'T and a'</span>)</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="demogrn1_img04.gif"><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="color:#990000; font-weight:bold; font-size:medium; page-break-before: auto;"><a name=""></a></p><p xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">Here the network's response is simulated for many values, allowing us to seethe function it represents.</p><pre xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" style="position: relative; left:30px">P2 = 0:.1:9;A2 = sim(net,P2);plot(P2,A2,<span style="color:#B20000">'linewidth'</span>,4,<span style="color:#B20000">'color'</span>,[1 0 0])title(<span style="color:#B20000">'Function to approximate.'</span>)xlabel(<span style="color:#B20000">'P and P2'</span>)ylabel(<span style="color:#B20000">'T and A2'</span>)</pre><img xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" src="demogrn1_img05.gif"><originalCode xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" code="%% GRNN Function Approximation
% This demonstration uses functions NEWGRNN and SIM.
%
% Copyright 1992-2002 The MathWorks, Inc.
% $Revision: 1.7 $ $Date: 2002/03/29 19:36:24 $

%%
% Here are eight data points of a function we would like to fit. The functions
% inputs P should result in target outputs T.

P = [1 2 3 4 5 6 7 8];
T = [0 1 2 3 2 1 2 1];

plot(P,T,'.','markersize',30)
axis([0 9 -1 4])
title('Function to approximate.')
xlabel('P')
ylabel('T')


%%
% We use NEWGRNN to create a generalized regression network. We use a SPREAD
% slightly lower than 1, the distance between input values, in order, to get a
% function that fits individual data points fairly closely. A smaller spread
% would fit data better but be less smooth.

spread = 0.7;
net = newgrnn(P,T,spread);
A = sim(net,P);

hold on
outputline = plot(P,A,'.','markersize',30,'color',[1 0 0]);
title('Create and test a network.')
xlabel('P')
ylabel('T and A')

%%
% We can use the network to approximate the function at a new input value.

p = 3.5;
a = sim(net,p);
plot(p,a,'.','markersize',30,'color',[1 0 0]);
title('New input value.')
xlabel('P and p')
ylabel('T and a')


%%
% Here the network's response is simulated for many values, allowing us to see
% the function it represents.

P2 = 0:.1:9;
A2 = sim(net,P2);
plot(P2,A2,'linewidth',4,'color',[1 0 0])
title('Function to approximate.')
xlabel('P and P2')
ylabel('T and A2')
"></originalCode>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -