⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nnd17lls.m

📁 《神经网络设计》英文版的配套源代码
💻 M
📖 第 1 页 / 共 2 页
字号:
function nnd17lls(cmd,arg1)
%NND17LLS Linear Least Squares.
%  This demonstration requires the Neural Network Toolbox.

% Copyright 1994-2002 PWS Publishing Company and The MathWorks, Inc.
% $Revision: 1.8 $
% First Version, 8-31-95.

%==================================================================

% CONSTANTS
me = 'nnd17lls';
max_t = 0.5;
w_max = 10;
p_max = 2;
circle_size = 6;

% FLAGS
change_func = 0;

% DEFAULTS
if nargin == 0, cmd = ''; else cmd = lower(cmd); end

% FIND WINDOW IF IT EXISTS
fig = nndfgflg(me);
if length(get(fig,'children')) == 0, fig = 0; end
  
% GET WINDOW DATA IF IT EXISTS
if fig
  H = get(fig,'userdata');
  fig_axis = H(1);            % window axis
  desc_text = H(2);           % handle to first line of text sequence
  s1_bar = H(3);              % lr slider bar
  s1_text = H(4);             % lr text
  fa_axis = H(5);             % function approximation axis
  fa_ptr = H(6);              % function approximation plot handles
  fa_ptr2 = H(7);              % function approximation plot handles
  fa_ptr3 = H(8);              % function approximation plot handles
  fa_ptr4 = H(9);              % function approximation plot handles
  fb_axis = H(10);             % function approximation axis
  fb_ptr = H(11);              % function approximation plot handles
  sigma_bar = H(12);               % Difficulty slider bar
  sigma_text = H(13);              % Difficuly text
  npts_bar = H(14);               % Difficulty slider bar
  npts_text = H(15);              % Difficuly text
  freq_bar = H(16);               % Difficulty slider bar
  freq_text = H(17);              % Difficuly text
  ro_bar = H(18);               % Difficulty slider bar
  ro_text = H(19);              % Difficuly text
  phase_bar = H(20);               % Difficulty slider bar
  phase_text = H(21);              % Difficuly text
  bias_bar = H(22);
  bias_text = H(23);
  bias_checkbox = H(24);
  w1_1_bar = H(25);
  w1_1_text = H(26);
  rand_ptr = H(27);
end

%==================================================================
% Activate the window.
%
% ME() or ME('')
%==================================================================

if strcmp(cmd,'')
  if fig
    figure(fig)
    set(fig,'visible','on')
  else
    feval(me,'init')
  end

%==================================================================
% Close the window.
%
% ME() or ME('')
%==================================================================

elseif strcmp(cmd,'close') & (fig)
  delete(fig)

%==================================================================
% Initialize the window.
%
% ME('init')
%==================================================================

elseif strcmp(cmd,'init') & (~fig)

  % CHECK FOR NNT
  % ODJ 12/23/07 Check for NN Toolbox removed
  %if ~nntexist(me), return, end

  % NEW DEMO FIGURE
  fig = nndemof2(me,'DESIGN','RBF Linear Least Squares','','Chapter 17');
  set(fig, ...
    'windowbuttondownfcn',nncallbk(me,'down'), ...
    'BackingStore','off',...
    'nextplot','add');
  H = get(fig,'userdata');
  fig_axis = H(1);
  desc_text = H(2);

  % ICON
  nndicon(17,458,363,'shadow')

  % HIDDEN NEURONS SLIDER BAR
  S1 = 5;
  x = 20;
  y = 115;
  len = 100;
  text(x,y,'Hidden Neurons:',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  s1_text = text(x+len,y,num2str(S1),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  text(x,y-38,'2',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left')
  text(x+len,y-38,'9',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  s1_bar = uicontrol(...
    'units','points',...
    'position',[x y-25 len 16],...
    'style','slider',...
    'backg',nnltgray,...
    'callback',[me '(''s1'')'],...
    'min',2,...
    'max',9,...
    'value',S1);

  % standard deviation of noise to be added to training data sliding bar.
  sigma = 0; %0;
  x = 20;
  y = 55;
  len = 100;
  text(x,y,'Stdv noise:',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  sigma_text = text(x+len,y,num2str(sigma),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  text(x,y-38,'0',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left')
  text(x+len,y-38,'1',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  sigma_bar = uicontrol(...
    'units','points',...
    'position',[x y-25 len 16],...
    'style','slider',...
    'backg',nnltgray,...
    'callback',[me '(''sigma'')'],...
    'min',0,...
    'max',1,...
    'value',sigma);

  % Number of Points SLIDER BAR
  npts = 10;
  x = 130;
  y = 115;
  len = 100;
  text(x,y,'Number of Points:',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  npts_text = text(x+len,y,num2str(npts),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  text(x,y-38,'2',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left')
  text(x+len,y-38,'20',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  npts_bar = uicontrol(...
    'units','points',...
    'position',[x y-25 len 16],...
    'style','slider',...
    'backg',nnltgray,...
    'callback',[me '(''npts'')'],...
    'min',2,...
    'max',20,...
    'value',npts);

  % frequency of the function to be fit sliding bar.
  freq = 1/2; %1/8;
  x = 130;
  y = 55;
  len = 100;
  text(x,y,'Function Freq.:',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  freq_text = text(x+len,y,num2str(freq),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  text(x,y-38,'.25',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left')
  text(x+len,y-38,'1',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  freq_bar = uicontrol(...
    'units','points',...
    'position',[x y-25 len 16],...
    'style','slider',...
    'backg',nnltgray,...
    'callback',[me '(''freq'')'],...
    'min',0.25,...
    'max',1,...
    'value',freq);

  % regularization constant SLIDER BAR
  ro = 0;
  x = 240;
  y = 115;
  len = 100;
  text(x,y,'Regularization:',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  ro_text = text(x+len,y,num2str(ro),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  text(x,y-38,'0',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left')
  text(x+len,y-38,'1',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  ro_bar = uicontrol(...
    'units','points',...
    'position',[x y-25 len 16],...
    'style','slider',...
    'backg',nnltgray,...
    'callback',[me '(''ro'')'],...
    'min',0,...
    'max',1,...
    'value',ro);

  % phase of the function to be fit sliding bar.
  phase = 90;
  x = 240;
  y = 55;
  len = 100;
  text(x,y,'Function Phase:',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  phase_text = text(x+len,y,num2str(phase),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  text(x,y-38,'0',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left')
  text(x+len,y-38,'360',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  phase_bar = uicontrol(...
    'units','points',...
    'position',[x y-25 len 16],...
    'style','slider',...
    'backg',nnltgray,...
    'callback',[me '(''phase'')'],...
    'min',0,...
    'max',360,...
    'value',phase);

  % FUNCTION APPROXIMATION
%  i=1;
%  P = -2:(.4/i):2;
%  T = 1 + sin(pi*P/4);
  
  % Initial parameters
  range = [-2 2];
  
  d1 = (range(2)-range(1))/(npts-1);
  p = range(1):d1:range(2);
  %t = sin(2*pi*(freq*p + phase/360))+1 + sigma*randn(size(p));
  randseq=randn(1,20);
  t = sin(2*pi*(freq*p + phase/360))+1 + sigma*randseq(1:length(p));
  delta = (range(2)-range(1))/(S1-1);
  b=1.6652/delta;       %sqrt(S1)/delta;
  
  total = range(2)-range(1);
  W1 = range(1):delta:range(2);
  W1 = W1(1:(end))';
  b1 = b*ones(size(W1));
  Q = length(p);
  pp = repmat(p,S1,1); 
  n1 = abs(pp-W1*ones(1,Q)).*(b1*ones(1,Q));
  a1 = exp(-n1.^2);
  Z = [a1;ones(1,Q)];
  %x=inv(Z*Z'+ro*eye(S1+1))*Z*t';
  %x=(Z*Z'+ro*eye(S1+1))\Z*t';
  x=pinv(Z*Z'+ro*eye(S1+1))*Z*t';
  W2 = x(1:end-1)';
  b2 = x(end);
  a2 = W2*a1 + b2*ones(1,Q);
  p2 = range(1):(total/1000):range(2);
  Q2 = length(p2);
  pp2 = repmat(p2,S1,1);
  n12 = abs(pp2-W1*ones(1,Q2)).*(b1*ones(1,Q2));
  a12 = exp(-n12.^2);
  a22 = W2*a12 + b2*ones(1,Q2);
  t_exact = sin(2*pi*(freq*p2 + phase/360))+1;
  temp=[(W2'*ones(1,Q2)).*a12; b2*ones(1,Q2)];
  %sse = sumsqr(t-a2);
  sse = sum(sum((t-a2).*(t-a2)));
  
  fa_axis = nnsfo('a2','Function Approximation','','a^2','');
  set(fa_axis,...
    'position',[50 220 270 120])
  %fa_plot =  plot(P,T,'+','color',nnred);
  fa_plot = plot(p,t,'ok');
  hold on
  fa_plot2 = plot(p2,temp,':k');
  fa_plot3 = plot(p2,t_exact,'b','LineWidth',2);
  fa_plot4 = plot(p2,a22,'r','LineWidth',1);
  hold off
  set(fa_axis,'ylim',[-2 4])

  fb_axis = nnsfo('a2','','p','a^1','');
  set(fb_axis,...
    'position',[50 160 270 40])
  fb_plot = plot(p2,a12,'k');
  
  % We change current axis to figure axis handle 
  axes(fig_axis)
  
  % bias of the function to be fit sliding bar.
  bias = b;
  x = 340;
  y = 290;
  len = 70;
  text(x-20,y+60,'bias:',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  bias_text = text(x+10,y+60,num2str(round(bias*100)/100),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left');
  text(x+20,y-23,'0.1',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left')
  text(x+35,y-28+len,'10',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','right');
  bias_bar = uicontrol(...
    'units','points',...
    'position',[x y-25 16 len],...
    'style','slider',...
    'backg',nnltgray,...
    'callback',[me '(''bias'')'],...
    'min',0.1,...
    'max',10,...
    'value',bias,...
    'enable','off');
  bias_checkbox = uicontrol(...
    'units','points',...
    'position',[x-15 y-50 50 16],...
    'style','checkbox',...
    'backg',nnltgray,...
    'callback',[me '(''bias_checkbox'')'],...
    'string','bias auto',...
    'value',1);

  % Initial Center for the first basis function sliding bar.
  w1_1 = W1(1);
  x = 340;
  y = 170;
  len = 70;
  text(x-25,y-35,'W1(1,1):',...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',10,...
    'horizontalalignment','left')
  w1_1_text = text(x+15,y-35,num2str(round(w1_1*100)/100),...
    'color',nndkblue,...
    'fontw','bold',...
    'fontsize',12,...
    'horizontalalignment','left');
  text(x+20,y-20,'-2',...
    'color',nndkblue,...
    'fontw','bold',...

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -