function sd_demo(cmd, data) % sd_demo: Method comparison demonstration. % BRING UP FIGURE IF IT EXISTS me = 'sd_demo'; fig = nnfgflag(me); if length(get(fig,'children')) == 0, fig = 0; end if nargin == 0, cmd = ''; end % CONSTANTS xlim = [-2 2]; dx = 0.2; ylim = [-2 2]; dy = 0.2; zlim = [0 12]; xpts = xlim(1):dx:xlim(2); ypts = ylim(1):dy:ylim(2); [X,Y] = meshgrid(xpts,ypts); xtick = [-2 0 2]; ytick = [-2 0 2]; ztick = [0 6 12]; circle_size = 10; a=[2 1;1 2]; b=[0 0]; c=0; % CREATE FIGURE if fig == 0 % CONSTANTS lr = 0.03; lr_min = 0; lr_max = 0.2; F = (a(1,1)*X.^2 + (a(1,2)+a(2,1))*X.*Y + a(2,2)*Y.^2)/2 ... + b(1)*X + b(2)*Y +c; % STANDARD DEMO FIGURE fig = nndemof(me,'DESIGN','Comparison of Methods','','example 1.23'); str = [me '(''down'',get(0,''pointerloc''))']; set(fig,'windowbuttondownfcn',str); % UNLOCK AND GET HANDLES set(fig,'nextplot','add','pointer','watch') H = get(fig,'userdata'); fig_axis = H(1); desc_text = H(2); % ICON nndicon(9,458,363,'shadow') % LEFT AXES left = nnsfo('a2','Steepest Descent','x(1)','x(2)'); set(left, ... 'xlim',xlim,'xtick',xtick, ... 'ylim',ylim,'ytick',ytick,... 'colororder',[nnblack; nnred; nngreen]); contour(xpts,ypts,F,[1.01 2 3 4 6 8 10]); text(0,1.7,'< CLICK ON ME >',... 'horiz','center', 'fontweight','bold', 'color',nndkblue); % RIGHT AXES right = nnsfo('a3','Conjugate Gradient','x(1)','x(2)'); set(right, 'xlim',xlim,'xtick',xtick, ... 'ylim',ylim,'ytick',ytick,... 'colororder',[nnblack; nnred; nngreen]); contour(xpts,ypts,F,[1.01 2 3 4 6 8 10]); text(0,1.7,'< CLICK ON ME >',... 'horiz','center', 'fontweight','bold', 'color',nndkblue); % TEXT nnsettxt(desc_text, 'COMPARISON OF METHODS','',... 'Click in either graph to create an initial search point.',... 'Then watch the two algorithms attempt to find the minima.','',... 'The two algorithms are:','',... ' Steepest Descent using line search','',... ' Conjugate Gradient using line search') % CREATE BUTTONS drawnow % Let everything else appear before buttons set(nnsfo('b4','Contents'), 'callback','nndtoc') nnsfo('b5','Close'); % DATA POINTERS path1_ptr = uicontrol('visible','off','userdata',[]); path2_ptr = uicontrol('visible','off','userdata',[]); % SAVE HANDLES, LOCK FIGURE H = [fig_axis desc_text left right path1_ptr path2_ptr]; set(fig,'userdata',H) % LOCK FIGURE AND RETURN set(fig,'nextplot','new','pointer','arrow','color',nnltgray) nnchkfs; return end % SERVICE COMMANDS ============ % UNLOCK FIGURE AND GET HANDLES set(fig,'nextplot','add','pointer','watch') H = get(fig,'userdata'); desc_text = H(2); left = H(3); right = H(4); path1_ptr = H(5); path2_ptr = H(6); % COMMAND: DOWN cmd = lower(cmd); if strcmp(cmd,'down') % FIND CLICK POSITION axes(left) click_pt = get(left,'currentpoint'); x = click_pt(1); y = click_pt(3); if (x >= xlim(1)) & (x <= xlim(2)) & (y >= ylim(1)) & (y <= ylim(2)) clicked = 1; else click_pt = get(right,'currentpoint'); x = click_pt(1); y = click_pt(3); if (x >= xlim(1)) & (x <= xlim(2)) &... (y >= ylim(1)) & (y <= ylim(2)) clicked = 1; else clicked = 0; end end if clicked % REMOVE PATHS path1 = get(path1_ptr,'userdata'); path2 = get(path2_ptr,'userdata'); delete(path1); delete(path2); % CIRCLES axes(left) path1 = plot(x,y,'o', 'markersize',circle_size); axes(right) path2 = plot(x,y,'o', 'markersize',circle_size); % OPTIMIZE x1 = [x; y]; % Steepest descent point x2 = x1; % Conjugate gradient point F1 = 0.5*x1'*a*x1 + b*x1 + c;; % Steepest descent error F2 = F; % Conjugate gradient error % STEEPEST DESCENT axes(left) for i=1:5 grad = a*x1+b'; p = -grad; hess = a; lr = -grad'*p/(p'*hess*p); dx1 = -lr*grad; nx1 = x1 + dx1; h1 = plot([x1(1) nx1(1)],[x1(2) nx1(2)],'color',nnred); h2 = plot([x1(1) nx1(1)],[x1(2) nx1(2)],'o','color',nndkblue); x1 = nx1; path1 = [path1; h1; h2]; end % CONJUGATE GRADIENT axes(right) for i=1:2 if (i==1) grad = a*x2+b'; p = -grad; else grad_old = grad; grad = a*x2+b'; beta = (grad'*grad)/(grad_old'*grad_old); p = -grad + beta*p; end hess=a; lr = -grad'*p/(p'*hess*p); dx2 = lr*p; nx2 = x2 + dx2; h1 = plot([x2(1) nx2(1)],[x2(2) nx2(2)],'color',nnred); h2 = plot([x2(1) nx2(1)],[x2(2) nx2(2)],'o','color',nndkblue); x2 = nx2; path2 = [path2; h1; h2]; end % SAVE PATHS set(path1_ptr,'userdata',path1); set(path2_ptr,'userdata',path2); end end % LOCK FIGURE set(fig,'nextplot','new','pointer','arrow') function f=nnfgflag(n) % NNFGFLAG Neural Network Design utility function. % NNFGFLAG(N) % N - Name of figure (string). % Returns handle to figure with name N if it exists. % Returns 0, otherwise. z = get(0,'children'); for i=1:length(z) typ = get(z(i),'type'); if strcmp(typ,'figure') nam = get(z(i),'name'); if strcmp(nam,n) f = z(i); return end end end f = 0; function fig = nndemof(s1,s2,s3,s4,s5) % NNDEMOF Standard demo figure for Neural Network Toolbox GUI. % NNDEMOF(S1,S2,S3) % S1: Name of window (default = 'NNDEMOF'). % S2: Identifer in window (default = 'TOOLBOX'). % S3: Title (default = 'Demonstration'). % S4: first name; S5: last name. % Returns handle to figure. % NNDEMOF stores the following handles: % H(1) = Axis covering entire figure in pixel coordinates. % H(2) = First line of text at bottom of figure. % Where H is the user data of the demo figure. % DEFAULTS if nargin < 1,s1 = 'NNDEMOF';end if nargin < 2,s2 = 'TOOLBOX';end if nargin < 3,s3 = 'Demonstration'; end if nargin < 4,s4 = ''; end if nargin < 5,s5 = ''; end % BACKGROUND fig = nnbg(s1,NaN); % UNLOCK AND GET HANDLES set(fig,'nextplot','add') H = get(fig,'userdata'); h1 = H(1); % TITLES text(35,376,'Neural Network', 'color',nnblack, ... 'fontname','times', 'fontsize',16, ... 'fontangle','italic', 'fontweight','bold'); text(35,356,s2, 'color',nnblack, ... 'fontname','times', 'fontsize',20, 'fontweight','bold'); text(415,356,s3, 'color',nnblack, ... 'fontname','times', 'fontsize',18, 'HorizontalAlignment','right'); % TOP LINE nndrwlin([0 415],[340 340],4,nndkblue); % MIDDLE LINE nndrwlin([0 415],[122 122],4,nndkblue); % TEXT h2 = text(35,108,'', 'color',nnblack, ... 'fontname','helvetica', 'fontsize',10); text1 = h2; for i=1:17 text2 = text(35,108-6*i,'', 'color', nnblack, ... 'fontname','helvetica', 'fontsize',10); set(text1,'userdata', text2); text1 = text2; end set(text1,'userdata','end'); % AUTHOR & BOTTOM LINE text(415,54,s4, 'color',nnblack, ... 'fontname','times', 'fontsize',12, 'fontweight','bold'); text(415,38,s5, 'color',nnblack, ... 'fontname','times', 'fontsize',12, 'fontweight','bold'); nndrwlin([415 501],[24 24],4,nndkblue); % SAVE HANDLES AND LOCK FIGURE set(fig,'userdata',[h1 h2]) set(fig,'color',nndkgray,'color',nnltgray) set(fig,'nextplot','new') function h = nndrwlin(x,y,w,c) % NNDRWLIN Neural Network Design utility function. % NNDRWLIN(X,Y,W,C) % X, Y: Vector of horizontal & vertical coordinates; % W: Width of line; C: Color of line. % Draws a line. % STORE AXIS STATE NP = get(gca,'nextplot'); set(gca,'nextplot','add'); % MAKE SURE NO LINE IS EXACTLY VERTICAL points = length(x); lines = points-1; for i=1:lines if x(i) == x(i+1), x(i) = x(i) + 1e-6; end end w = 0.5*w; x1 = x(1:lines); y1 = y(1:lines); x2 = x(2:lines+1); y2 = y(2:lines+1); m = zeros(1,lines); b = m; b1 = m; b2 = m; xa = zeros(1,points); xb = xa; ya = xa; yb = xa; % CALCULATE LINE EDGES for i=1:lines % CALCULATE SLOPE OF LINE if (y1(i) == y2(i)) b(i) = y1(i); if x1(i) < x2(i) m(i) = 0; invm = -inf; b1(i) = b(i)+w; b2(i) = b(i)-w; else m(i) = 0; invm = inf; b1(i) = b(i)-w; b2(i) = b(i)+w; end elseif (x2(i) == x1(i)) if y1(i) < y2(i) m(i) = inf; else m(i) = -inf; end invm = 0; b(i) = y1(i); b1(i) = b(i); b2(i) = b(i); else m(i) = (y2(i)-y1(i))/(x2(i)-x1(i)); signy = sign((y2(i)-y1(i))); signx = sign((x2(i)-x1(i))); signboth = signx*signy; b(i) = y1(i)-m(i)*x1(i); invm = -(1/m(i)); db = w*(1/sqrt(m(i)^2+1) + signboth*m(i)/sqrt(invm^2+1)); b1(i) = b(i)+db * signx; b2(i) = b(i)-db * signx; end % FIRST POINT if i == 1 if finite(m(1)) if (m(1) ~= 0),... dx = signy*w/sqrt(invm^2+1); else dx = 0; end xa(1) = x1(1)-dx; xb(1) = x1(1)+dx; ya(1) = m(1)*xa(1)+b1(1); yb(1) = m(1)*xb(1)+b2(1); else xa(1) = x1(1)-sign(m(1))*w; xb(1) = x1(1)+sign(m(1))*w; ya(1) = y1(1); yb(1) = y1(1); end % plot([xa(1) xb(1)],[ya(1) yb(1)]) % MID POINTS else if finite(m(i)) if finite(m(i-1)) xa(i) = (b1(i-1)-b1(i))/(m(i)-m(i-1)); xb(i) = (b2(i-1)-b2(i))/(m(i)-m(i-1)); ya(i) = m(i)*xa(i)+b1(i); yb(i) = m(i)*xb(i)+b2(i); else xa(i) = x1(i)-w; xb(i) = x1(i)+w; ya(i) = m(i)*xa(i)+b1(i); yb(i) = m(i)*xb(i)+b2(i); end else xa(i) = x1(i)-w; xb(i) = x1(i)+w; ya(i) = m(i-1)*xa(i)+b1(i-1); yb(i) = m(i-1)*xb(i)+b2(i-1); end % plot([xa(i) xb(i)],[ya(i) yb(i)]) end % LAST POINT if i == lines if finite(m(lines)) if (m(lines) ~= 0),... dx = signy*w/sqrt(invm^2+1); else dx = 0; end xa(points) = x2(lines)-dx; xb(points) = x2(lines)+dx; ya(points) = m(lines)*xa(points)+b1(lines); yb(points) = m(lines)*xb(points)+b2(lines); else xa(points) = x2(lines)+sign(m(lines))*w; xb(points) = x2(lines)-sign(m(lines))*w; ya(points) = y2(lines); yb(points) = y2(lines); end % plot([xa(points) xb(points)],[ya(points) yb(points)]) end end % DRAW THE LINE xx = [xb(1) xa fliplr(xb)]; yy = [yb(1) ya fliplr(yb)]; g = fill(xx,yy,c,'edgecolor','none'); % RESTORE AXIS STATE set(gca,'nextplot',NP); % RETURN THE LINE if nargout, h = g; end function h = nndicon(c,x,y,f,s) % NNDICON Icons for Neural Network Design GUI. % NNDICON(C,'book') % C - number from Neural Network Design. % Draws the icon for C. % % NNDICON(C,X,Y,F,S) % X - Horizontal position; Y - Vertical position. % F - Shadow box flag (default = 0); S - Size (optional). % Draws the icon for C at position (X,Y) with size S. % If F is nonzero, then the icon casts a shadow. % DEFAULTS visible = 'on'; book = 0; mini = 0; if nargin == 0, error('Not enough input arguments.'); end if nargin < 4, f = 0; else f = 1; end if nargin < 3, y = 0; end if nargin < 2, x = 0; mini = 1; end if nargin == 2,if isstr(x) if strcmp(lower(x),'invisible'),... visible = 'off'; else book = 1; end x = 0; mini = 1; end, end % FIGURE if mini pos = get(0,'defaultfigureposition'); fig = figure('units','points',... 'pos',[pos(1) (pos(2)+pos(4)-60) 60 60],... 'resize','off', 'number','off', 'name','',... 'colormap',nncolor, 'visible',visible); set(gca, 'units','points',... 'pos',[0 0 60 60], 'xlim',[-30 29.9],... 'ylim',[-30 29.9], 'nextplot','add') axis('equal') axis('off') else fig = gcf; end % COLORS if book red = [1 1 1]*0.5; blue = [0 0 1]*0.5; yellow = [1 1 1]; orange = [1 1 1]*0.5; else red = nnred; blue = nndkblue; yellow = nnyellow; orange = [1 0.5 0]; end % STORE FIGURE STATE NPF = get(fig,'nextplot'); NPA = get(gca,'nextplot'); set(fig,'nextplot','add'); set(gca,'nextplot','add'); hold on % SHADOW rect_x = [-1 1 1 -1]; rect_y = [-1 -1 1 1]; if f shadow_h = fill(x+rect_x*25+4,y+rect_y*25-4,nndkgray,... 'edgecolor','none', 'erasemode','none'); else shadow_h = []; end % ICON FRAME frame_h1 = fill(x+rect_x*25,y+rect_y*25,red, ... 'edgecolor','none', 'erasemode','none'); frame_h2 = fill(x+rect_x*20,y+rect_y*20,blue, ... 'edgecolor','none', 'erasemode','none'); % INTRO if c == 1 % ARCHITECTURES elseif c == 2 line_h1 = nnline(x+[10 9 8 8],y+[8 7 6 -14],4,yellow,'none'); line_h2 = nnline(x+[-10 -9 -8 -8],y+[8 7 6 -14],4,yellow,'none'); line_h3 = nnline(x+[14 -14],y+[14 14],4,yellow,'none'); line_h4 = nnline(x+[0 0],y+[8 -14],4,yellow,'none'); icon_h = [line_h1; line_h2; line_h3; line_h4]; % ILLUSTRATIVE EXAMPLE elseif c == 3 orange_x = [5 0 0 5 10 15 15 10]-7+x; orange_y = [15 10 5 0 0 5 10 15]-7+y; apple_x = [7.5 5 0 0 5 7.5 10 15 15 10]-7+x; apple_y = [13 15 10 5 0 2 0 5 10 15]-7+y; orange = fill(orange_x-11, orange_y+7,orange,... 'edgecolor',yellow, 'linewidth',2, 'erasemode','none'); apple = fill(apple_x+8, apple_y-7,red,... 'edgecolor',yellow, 'linewidth',2, 'erasemode','none'); slash = nnline(x+[-14 14],y+[-14 14],1,red,'none'); icon_h = [orange; apple; slash]; % PERCEPTRONS elseif c == 4 line_h1 = nnline(x+[-14 14],y+[-14 -14],3,red,'none'); line_h2 = nnline(x+[-14 0 0 14],y+[-14 -14 14 14], 4,yellow,'none'); icon_h = [line_h1; line_h2]; % SIGNAL AND WEIGHT VECTOR SPACES elseif c == 5 vec_h1 = nnarrow(x+[-14 14],y+[-14 14],2,yellow,'none'); %vec_h2 = nnarrow(x+[-14 2],y+[-14 14],2,yellow,'none'); icon_h = [vec_h1]; % LINEAR TRANSFORMATIONS elseif c == 6 angle = [10:2:80]*pi/180; circle_x = cos(angle); circle_y = sin(angle); patch_h1 = patch(x-14+20*circle_x,y-14+20*circle_y,yellow,... 'erasemode','none', 'edgecolor','none'); patch_h2 = patch(x-14+18*circle_x,y-14+18*circle_y,blue,... 'erasemode','none', 'edgecolor','none'); vec_h1 = nnarrow(x+[-14 14],y+[-14 -7],2,yellow,'none'); vec_h2 = nnarrow(x+[-14 -7],y+[-14 14],2,yellow,'none'); icon_h = [patch_h1; patch_h2; vec_h1; vec_h2]; % SUPERVISED HEBB elseif c == 7 half_angle = [90:2.5:270]*pi/180; half_x = cos(half_angle)*8; half_y = sin(half_angle)*8; line_h1 = nnline(x+[-15 -5],y+[0 0],3,yellow,'none'); line_h2 = nnline(x+[5 15],y+[0 0],3,yellow,'none'); half_h1 = patch(x+half_x-2,y+half_y,yellow,... 'erasemode','none', 'edgecolor','none'); half_h2 = patch(x-half_x+2,y+half_y,yellow,... 'erasemode','none', 'edgecolor','none'); icon_h = [line_h1; line_h2; half_h1; half_h2]; % SURFACES AND MINIMA elseif c == 8 angle = [0:10:360]*pi/180; circle_x = cos(angle); elipse_y = sin(angle); elipse_x = circle_x + elipse_y*0.4; patch_h1 = patch(x-15*elipse_x,y+15*elipse_y,yellow,... 'erasemode','none', 'edgecolor','none'); patch_h2 = patch(x-11*elipse_x,y+11*elipse_y,blue,... 'erasemode','none', 'edgecolor','none'); patch_h3 = patch(x+3*circle_x,y+3*elipse_y,yellow,... 'erasemode','none', 'edgecolor','none'); icon_h = [patch_h1; patch_h2; patch_h3]; % OPTIMIZATION elseif c == 9 angle = [0:10:360]*pi/180; circle_x = cos(angle); elipse_y = sin(angle); elipse_x = circle_x + elipse_y*0.4; patch_h1 = patch(x-14*elipse_x,y+14*elipse_y,yellow,... 'erasemode','none', 'edgecolor','none'); patch_h2 = patch(x-11.8*elipse_x,y+11.8*elipse_y,blue,... 'erasemode','none', 'edgecolor','none'); patch_h3 = patch(x-8*elipse_x,y+8*elipse_y,yellow,... 'erasemode','none', 'edgecolor','none'); patch_h4 = patch(x-6*elipse_x,y+6*elipse_y,blue,... 'erasemode','none', 'edgecolor','none'); patch_h5 = patch(x+3*circle_x,y+3*elipse_y,yellow,... 'erasemode','none', 'edgecolor','none'); line_h = nnline(x+[-20 -16 -14 -10 -5 0],... y+[8 10 10 8 4 0],2,red,'none'); icon_h = [patch_h1; patch_h2; patch_h3;... patch_h4; patch_h5; line_h]; % LINEAR NETWORKS elseif c == 10 t=0:.05*2*pi:2*pi; yy=30*exp(-0.4*t).*sin(t); xx=1:length(yy); xx=30*xx/length(yy)-15; line_h1 = nnline(x+[-14 14],y+[0 0],2,red,'none'); line_h2 = nnline(x+xx,y+yy,2,yellow,'none'); icon_h = [line_h1; line_h2]; % BACKPROPAGATION elseif c == 11 line_h1 = nnline(x+[-14 14],y+[0 0],3,red,'none'); n = -5:0.25:5; a = 2./(1+exp(-2*n))-1; xx = n/5*15; yy = a/1*15; line_h2 = nnline(x+xx,y+yy,4,yellow,'none'); icon_h = [line_h1; line_h2]; % VARIATIONS ON BACKPROPAGATION elseif c == 12 patch_h1 = patch(x+1+[-17 -10 -4 0 14 0 -4],... y+[11 16 0 8 -15 -2 -7],yellow,... 'erasemode','none', 'edgecolor','none'); icon_h = [patch_h1]; % ASSOCIATIVE LEARNING RULES elseif c == 13 radius = 6.5; angle = [0:10:360]*pi/180; full_x = cos(angle)*radius; full_y = sin(angle)*radius; half_angle = [90:10:270]*pi/180; half_x = cos(half_angle)*radius; half_y = sin(half_angle)*radius; line_h1 = nnline(x+[-15 -8],y+[-5 -5],4,yellow,'none'); half_h1 = patch(x+half_x-6,y+half_y-5,yellow,... 'erasemode','none', 'edgecolor','none'); line_h2 = nnline(x+[6 15],y+[-5 -5],4,yellow,'none'); full_h2 = patch(x-full_x+3,y+full_y-5,yellow,... 'erasemode','none', 'edgecolor','none'); line_h3 = nnline(x+[3 3],y+[15 7],4,yellow,'none'); half_h3 = patch(x-half_y+3,y-half_x+5,yellow,... 'erasemode','none', 'edgecolor','none'); icon_h = [line_h1; line_h2; line_h3; half_h1; full_h2; half_h3]; % COMPETITIVE elseif c == 14 angle = [0:20:360] * pi/180; cx = cos(angle); cy = sin(angle); paddle1 = patch(x-11+6*cx,y+4+6*cy,yellow,... 'erasemode','none', 'edgecolor','none'); paddle2 = patch(x+11+6*cx,y+4+6*cy,yellow,... 'erasemode','none', 'edgecolor','none'); handle1 = patch(x+[-13 -10 -5 -8],y+[4 5 -5 -6],yellow,... 'erasemode','none', 'edgecolor','none'); handle2 = patch(x+[13 10 5 8],y+[4 5 -5 -6],yellow,... 'erasemode','none', 'edgecolor','none'); ball = patch(x+2*cx,y+5+2*cy,yellow,... 'erasemode','none', 'edgecolor','none'); icon_h = [paddle1; paddle2; handle1; handle2; ball;]; % GROSSBERG elseif c == 15 angle = [0:10:360] * pi/180; cx = cos(angle); cy = sin(angle); eyeball = plot(x+15*cx,y+15*cy,... 'color',yellow, 'linewidth',2, 'erasemode','none'); angle = [120:10:250] * pi/180; cx = cos(angle); cy = sin(angle); iris = plot(x+15+8.7*cx,y+8.7*cy,... 'color',yellow, 'linewidth',2, 'erasemode','none'); angle = [135 180 215] * pi/180; cx = cos(angle); cy = sin(angle); for i=1:3 iris = [iris; plot(x+15+cx(i)*[0 8.7],y+cy(i)*[0 8.7],... 'color',yellow, 'linewidth',1, 'erasemode','none')]; end pupil = plot(x+15+3*cx,y+2*cy,... 'color',yellow, 'linewidth',3, 'erasemode','none'); icon_h = [eyeball; iris; pupil]; % ART elseif c == 16 angle = [0:10:360] * pi/180; scale1 = [0.8 0.85 0.9 0.95 1.0 1.0 0.95 0.9 0.85 0.85]; scale2 = scale1(9:-1:1); scale3 = [0.75 0.7 0.65 0.65 0.65 0.675 0.725 0.8 0.9]; scale4 = fliplr(scale3); scale = 1.25*[scale1 scale2 scale3 scale4]; cx = scale.*cos(angle); cy = scale.*sin(angle); face = plot(x+15*cx,y+15*cy,... 'color',yellow, 'linewidth',2, 'erasemode','none'); cx = cos(angle); cy = 0.5*sin(angle); eye1 = plot(x-8+4*cx,y+10+4*cy,... 'color',yellow, 'linewidth',1, 'erasemode','none'); cx = cos(angle); cy = 0.5*sin(angle); rotate = [.707 -.707; .707 .707]; ctot = rotate*[cx; cy]; cx = ctot(1,:); cy = ctot(2,:); eye2 = plot(x+8+4*cx,y+8+4*cy,... 'color',yellow, 'linewidth',1, 'erasemode','none'); cx = cos(angle); cy = 0.5*sin(angle); rotate = [.707 .707; -.707 .707]; ctot = rotate*[cx; cy]; cx = ctot(1,:); cy = ctot(2,:); mouth = plot(x-2+4*cx,y-9+4*cy,... 'color',yellow, 'linewidth',1, 'erasemode','none'); nose = nnline(x+[-1 0 1 0 -2 -4 0 2 3], ... y+[9 6 4.5 3 -1 -3 -3 -4 -6],1,yellow,'none'); icon_h = [face; eye1; eye2; mouth; nose]; % STABILITY elseif c == 17 angle = [0:10:180] * pi/180; hillx = cos(angle); hilly = sin(angle); hill = plot(x+hillx*15,y+hilly*25-15,... 'color',yellow, 'linewidth',3, 'erasemode','none'); angle = [0:20:360] * pi/180; circx = cos(angle); circy = sin(angle); ball = patch(x+3*circx,y+14+3*circy,yellow,... 'erasemode','none', 'edgecolor','none'); icon_h = [hill; ball]; % HOPFIELD elseif c == 18 xx = [0 0 -12 0 0 0 12 0]; yy = [15 10 10 -10 -15 -10 10 10]; triangle = plot(x+xx, y+yy, 'color',yellow,... 'linewidth',2, 'erasemode','none'); angle = [0:20:360] * pi/180; xx = cos(angle); yy = sin(angle); circle = plot(x-5+xx*3,y-6+yy*3,... 'color',yellow, 'linewidth',2, 'erasemode','none'); xx = [-5 -5]; yy = [-9 -15]; line = plot(x+xx,y+yy,... 'color',yellow, 'linewidth',2, 'erasemode','none'); icon_h = [triangle; circle; line]; % ICON 100: SUMMATION elseif c == 100 line_h1 = nnline(x+[10 -10 4 -10 10], y+[14 14 0 -14 -14],3,yellow,'none'); icon_h = [line_h1]; % ICON 101: F elseif c == 101 line_h1 = nnline(x+[10 -10 -10 10],y+[14 14 0 0],4,yellow,'none'); line_h2 = nnline(x+[-10 -10],y+[0 -14],4,yellow,'none'); icon_h = [line_h1; line_h2]; end % BOOK STUFF if book axis('off') axis('equal') set(gcf,'color',[1 1 1]) set(gcf,'inverthardcopy','off') end % RESTORE FIGURE STATE set(gca,'nextplot',NPA); set(fig,'nextplot',NPF); % RETURN ICON if nargout, h = [shadow_h; frame_h1; frame_h2; icon_h]; end function h = nnsfo(id,n,x,y,z) % NNSFO Neural Network Design utility function. % NNSFO(ID, N) % ID: Standard object id (string); N: Name (default = ID). % Creates a standard object given one of the following id'ID: % Full figure pixel coordinate axis: 'a0' % Single large axis: 'a1' % Two small axes: 'a2','a3' % Four small axes: 'a4','a7' % Buttons on side of figure: 'b0',...,'b6' % Buttons on bottom of figure: 'b7',...,'b10' % Second row of buttons on bottom: 'b11',...,'b14' % DEFAULT if nargin < 1,error('Call to NNSFO with no arguments.'),end if nargin < 2,n = id; end % FIND OBJECT ID = lower(id); % STANDARD AXES if strcmp(ID,'a0') obj = 'axis'; figpos = get(gcf,'position'); pos = [0 0 figpos(3:4)]; elseif strcmp(ID,'a1') obj = 'axis'; pos = [38 153 377 160]; % TWO SMALL AXES elseif strcmp(ID,'a2') obj = 'axis'; pos = [48 153 160 160]; elseif strcmp(ID,'a3') obj = 'axis'; pos = [254 153 160 160]; % FOUR SMALL AXES elseif strcmp(ID,'a4') obj = 'axis'; pos = [48 208 130 130]; elseif strcmp(ID,'a5') obj = 'axis'; pos = [224 208 130 130]; elseif strcmp(ID,'a6') obj = 'axis'; pos = [48 30 130 130]; elseif strcmp(ID,'a7') obj = 'axis'; pos = [224 30 130 130]; % BUTTONS ON RIGHT SIDE elseif strcmp(ID,'b0') obj = 'button'; pos = [430 302 60 20]; elseif strcmp(ID,'b1') obj = 'button'; pos = [430 266 60 20]; elseif strcmp(ID,'b2') obj = 'button'; pos = [430 230 60 20]; elseif strcmp(ID,'b3') obj = 'button'; pos = [430 194 60 20]; elseif strcmp(ID,'b4') obj = 'button'; pos = [430 158 60 20]; elseif strcmp(ID,'b5') obj = 'button'; pos = [430 122 60 20]; elseif strcmp(ID,'b6') obj = 'button'; pos = [430 86 60 20]; % BUTTONS ON BOTTOM elseif strcmp(ID,'b7') obj = 'button'; pos = [80 10 60 20]; elseif strcmp(ID,'b8') obj = 'button'; pos = [150 10 60 20]; elseif strcmp(ID,'b9') obj = 'button'; pos = [220 10 60 20]; elseif strcmp(ID,'b10') obj = 'button'; pos = [290 10 60 20]; % BUTTONS ABOVE BOTTOM elseif strcmp(ID,'b11') obj = 'button'; pos = [80 46 60 20]; elseif strcmp(ID,'b12') obj = 'button'; pos = [150 46 60 20]; elseif strcmp(ID,'b13') obj = 'button'; pos = [220 46 60 20]; elseif strcmp(ID,'b14') obj = 'button'; pos = [290 46 60 20]; % DATA OBJECT elseif strcmp(ID,'data') g = uicontrol('visible','off'); if nargout, h = g; end return % DEFAULT OBJECT else error(['Call to NNFO with unrecognized id: ' id]) end % CREATE OBJECT if strcmp(obj,'axis') color_order = [nnred; nngreen; nndkblue; [1 0 1]]; g = axes('units','points', 'position',pos, 'box','on', ... 'color',nnltyell, 'xcolor',nndkblue, ... 'ycolor',nndkblue, 'zcolor',nndkblue, ... 'fontsize',10, 'nextplot','add', 'colororder',color_order); if strcmp(ID,'a0') set(g,'xlim',[0 pos(3)],'ylim',[0 pos(4)]) axis('off') end if nargin < 3 xlabel('x'); else xlabel(x); end if nargin < 4 ylabel('y'); else ylabel(y); end if nargin < 5 zlabel('z'); else zlabel(z); end set(get(g,'xlabel'),'fontw','bold','color',nndkblue) set(get(g,'ylabel'),'fontw','bold','color',nndkblue) set(get(g,'zlabel'),'fontw','bold','color',nndkblue) title(n); set(get(g,'title'),'color',nndkblue,'fontw','bold','fontsize',12) view(2); elseif strcmp(obj,'button') g = uicontrol('units','points', 'position',pos, ... 'callback','disp(''NOT YET IMPLEMENTED'')', 'string',n); if strcmp(lower(n),'close') set(g,'callback','delete(gcf)') end end if nargout == 1, h = g; end function nnsettxt(h,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,... t11,t12,t13,t14,t15,t16,t17,t18,t19,t20) % NNSETTXT Neural Network Design utility function. % NNSETTXT(H,T1,T2,T3,T4,T5,...T7) % H: Handle to first line of linked texts, Ti: Strings. % Places strings into text fields. % GET FIRST TEXT HANDLE text_obj = h; if length(t1) > 10 & all(upper(t1) == t1) set(text_obj,'fontweight','bold') else set(text_obj,'fontweight','normal') end for i=1:(nargin-1) line = deblank(eval(sprintf('t%g',i))); set(text_obj,'string',line); text_obj = get(text_obj,'userdata'); if strcmp(text_obj,'end'), return, end if length(line) > 0 set(text_obj,'string',''); text_obj = get(text_obj,'userdata'); if strcmp(text_obj,'end'), return, end end end while ~strcmp(text_obj,'end') set(text_obj,'string','') text_obj = get(text_obj,'userdata'); end function h = nnline(x,y,w,c,e) % NNLINE Draw line connecting series of points. % NNLINE(X,Y,W,C,E) % X: Vector of horizontal coordinates; % Y: Vector of vertical coordinates. % W: Width of line (default=1); C: Color of line (default=white). % E: Erase mode (default = 'normal'). % DEFAULTS if nargin < 2, error('Not enough input arguments.'), end if nargin < 3, w = 1; end if nargin < 4, c = [1 1 1]; end if nargin < 5, e = 'normal'; end % STORE AXIS STATE NP = get(gca,'nextplot'); set(gca,'nextplot','add'); rect = [-1 1 1 -1; -1 -1 1 1]*(w/2); lines = length(x)-1; X = zeros(6,lines); Y = zeros(6,lines); % DRAW EACH LINE SEGMENT for i=1:lines x1 = x(i); x2 = x(i+1); y1 = y(i); y2 = y(i+1); rect1 = [x1+rect(1,:); y1+rect(2,:)]; rect2 = [x2+rect(1,:); y2+rect(2,:)]; % CALCULATE LINE EDGES if (y1 <= y2) % FIRST QUADRANT if (x1 <= x2) the_line = [rect1(:,[1 2]) rect2(:,[2 3 4]) rect1(:,4)]; % SECOND QUADRANT else the_line = [rect1(:,[1 2 3]) rect2(:,[3 4 1])]; end else % THIRD QUADRANT if (x1 >= x2) the_line = [rect1(:,[2 3 4]) rect2(:,[4 1 2])]; % FOURTH QUADRANT else the_line = [rect1(:,1) rect2(:,[1 2 3]) rect1(:,[3 4])]; end end X(:,i) = the_line(1,:)'; Y(:,i) = the_line(2,:)'; end % FOR LOOP g = fill(X,Y,c,'edgecolor','none','erasemode',e); % RESTORE AXIS STATE set(gca,'nextplot',NP); % RETURN THE LINE if nargout, h = g; end function h = nnarrow(x,y,w,c,t,e) % NNARROW Neural Network Design utility function. % NNARROW(X,Y,W,C,T,E) % X - Horizontal coordinate; Y - Vertical coordinate. % W - Width of line (in points); C - Color of line. % T - Tag string (default = ''); E - Erase mode (default = 'normal'); % DEFAULTS if nargin < 6, t = ''; end if nargin < 7, e = 'normal'; end if length(x) == 1,x0 = 0; else x0 = x(1); end if length(y) == 1,y0 = 0; else y0 = y(1); end x = x(length(x)); y = y(length(y)); % LINE WIDTH set(gcf,'units','points') set(gca,'units','points') axis_pos = get(gca,'pos'); axis_points = axis_pos(3); xlim = get(gca,'xlim'); axis_width = xlim(2)-xlim(1); w = w * axis_width/axis_points; line_h1 = nnline([x0 x],[y0 y],w,c,e); l = 3*w; angle = atan2(y-y0,x-x0); angle1 = angle+3.001*pi/4; angle2 = angle-3.001*pi/4; xx = [x+l*cos(angle1) x x+l*cos(angle2)]; yy = [y+l*sin(angle1) y y+l*sin(angle2)]; line_h2 = nnline(xx,yy,w,c,e); if ~strcmp(t,'') text_h = text(x+l*1.5*cos(angle),y+l*1.5*sin(angle),t, ... 'color',nndkblue, 'fontsize',10, ... 'fontname','geneva', 'horizontal','center', 'erasemode',e); else g3 = []; end if nargout, h = [line_h1; line_h2; text_h]; end