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

📄 elman.m

📁 线性神经网络
💻 M
字号:
function varargout = Elman(varargin)
% ELMAN M-file for Elman.fig
%      ELMAN, by itself, creates a new ELMAN or raises the existing
%      singleton*.
%
%      H = ELMAN returns the handle to a new ELMAN or the handle to
%      the existing singleton*.
%
%      ELMAN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ELMAN.M with the given input arguments.
%
%      ELMAN('Property','Value',...) creates a new ELMAN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Elman_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Elman_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help Elman

% Last Modified by GUIDE v2.5 26-May-2006 21:30:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Elman_OpeningFcn, ...
                   'gui_OutputFcn',  @Elman_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Elman is made visible.
function Elman_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Elman (see VARARGIN)

% Choose default command line output for Elman
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Elman wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Elman_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in PB_T.
function PB_T_Callback(hObject, eventdata, handles)
% hObject    handle to PB_T (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

user_data=get(findobj('Tag','PB_T'),'userdata');
switch  user_data.chose
    case 1
        set(findobj('Tag','PB_T'),'String','Next>>');
        t=1:20;
        p1=sin(t);
        p2=sin(t)*2;
        set(findobj('Tag','axes2'),'NextPlot','add');
        plot(findobj('Tag','axes2'),t,p1,'r');
        hold on;
        plot(findobj('Tag','axes2'),t,p2,'b');
        hold on;
        
        str1=sprintf('>> t=1:20;\n>> p1=sin(t);\n>> p2=sin(t)*2;\n>> plot(findobj(''Tag'',''axes1''),t,p1,''r'');');
        str2=sprintf('\n>> hold on;\n>> plot(findobj(''Tag'',''axes1''),t,p2,''b'');\n>> hold on;');
        str=strcat(str1,str2);
        set(findobj('Tag','edit1'),'String',str);
        
        str='代码定义了两个正弦波信号p1和p2作为Elman网络的输入,其中p1的幅值为1,p2的幅值为2。信号波形绘制在绘图区';
        set(findobj('Tag','edit2'),'String',str);
        
        set(findobj('Tag','text1'),'String','网络的设计');
        
        user_data.chose=user_data.chose+1;
        user_data.p1=p1;
        user_data.p2=p2;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        set(findobj('Tag','text2'),'String','step1');
    case 2
        t1=ones(1,20);
        t2=ones(1,20)*2;
        
        str=sprintf('>> t1=ones(1,20);\n>> t2=ones(1,20)*2;');
        set(findobj('Tag','edit1'),'String',str);
        
        str='代码产生两组向量t1和t2作为Elman网络的目标输出向量';
        set(findobj('Tag','edit2'),'String',str);
        
        user_data.chose=user_data.chose+1;
        user_data.t1=t1;
        user_data.t2=t2;
        set(findobj('Tag','PB_T'),'userdata',user_data);
                
        set(findobj('Tag','text2'),'String','step2');
    case 3
        p1=user_data.p1;
        p2=user_data.p2;
        t1=user_data.t1;
        t2=user_data.t2;
        
        p=[p1 p2 p1 p2];
        t=[t1 t2 t1 t2];
        Pseq=con2seq(p);
        Tseq=con2seq(t);
        
        str=sprintf('>> p=[p1 p2 p1 p2];\n>> t=[t1 t2 t1 t2];\n>> Pseq=con2seq(p);\n>> Tseq=con2seq(t);');
        set(findobj('Tag','edit1'),'String',str);
        
        str='p1和p2联合起来建立一个新的输入向量序列p;t1和t2也可以以同样的方式联合起来建立一个新的目标向量t。并利用函数con2seq将其转换为序列形式';
        set(findobj('Tag','edit2'),'String',str);
        
        user_data.chose=user_data.chose+1;
        user_data.Pseq=Pseq;
        user_data.Tseq=Tseq;
        user_data.p=p;
        user_data.t=t;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        set(findobj('Tag','text2'),'String','step3');
    case 4
        R=1;
        S2=1;
        S1=10;
        net=newelm([-2 2],[S1,S2],{'tansig','purelin'});
        
        str=sprintf('>> R=1;\n>> S2=1;\n>> S1=10;\n>> net=newelm([-2 2],[S1,S2],{''trasig'',''purelin''}');
        set(findobj('Tag','edit1'),'String',str);
        
        str='R为输入元素的数目,S2为网络输出神经元的数目,S1是网络中间层的神经元数目,newelm函数用来设计一个Elman神经网络';
        set(findobj('Tag','edit2'),'String',str);
        
        user_data.chose=user_data.chose+1;
        user_data.net=net;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        set(findobj('Tag','text2'),'String','step4');
    case 5
        Pseq=user_data.Pseq;
        Tseq=user_data.Tseq;
        net=user_data.net;
        
        set(findobj('Tag','text1'),'String','网络训练');
        
        str=sprintf('>> net.trainParam.epochs=500;\n>> net=trian(net,Pseq,Tseq);');
        set(findobj('Tag','edit1'),'String',str);
        
        str='网络的训练步数设定为500次,trian函数用来训练网络';
        set(findobj('Tag','edit2'),'String',str);
        
        net.trainParam.epochs=300;
        net=train(net,Pseq,Tseq);
        
        user_data.chose=user_data.chose+1;
        user_data.net=net;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        set(findobj('Tag','text2'),'String','step5');
    case 6
        Pseq=user_data.Pseq;
        net=user_data.net;
        Tseq=user_data.Tseq
        
        set(findobj('Tag','text1'),'String','网络测试');
        
        str=sprintf('>> y=sim(net,Pseq);\n>> t5=1:80;\n>> plot(findobj(''Tag'',''axes1''),t5,cat(2,y{:}),t5,cat(2,Tseq{:}),''b--'');');
        set(findobj('Tag','edit1'),'String',str);
        
        str='利用训练数据对网络进行测试';
        set(findobj('Tag','edit2'),'String',str);
 
        y=sim(net,Pseq);
        t5=1:80;
    %    h=subplot('position',[17.0 16.077 80.2 23.154]);
  %      set(findobj('Tag','axes1'),'NextPlot','replace');
   %     set(findobj('Tag','axes1'),'NextPlot','add');
        h=findobj('Tag','axes1');
        axes(h);
        plot(t5,cat(2,y{:}),t5,cat(2,Tseq{:}),'b--');
        
        user_data.chose=user_data.chose+1;
        user_data.net=net;
        set(findobj('Tag','PB_T'),'userdata',user_data);
        
        set(findobj('Tag','text2'),'String','step6');
    case 7
        p3=sin(1:20)*1.6;
        t3=ones(1,20)*1.6;
        p4=sin(1:20)*1.2;
        t4=ones(1,20)*1.2;
        pg=[p3 p4 p3 p4];
        tg=[t3 t4 t3 t4];
        pgseq=con2seq(pg);
        
        str1=sprintf('>> p3=sin(1:20)*1.6;\n>> t3=ones(1,20)*1.6;\n>> p4=sin(1:20)*1.2;\n>> t4=ones(1,20)*1.2;');
        str2=sprintf('\n>> pg=[p3 p4 p3 p4];\n>> tg=[t3 t4 t3 t4];\n>> pgseq=con2seq(pg);');
        str=strcat(str1,str2);
        set(findobj('Tag','edit1'),'String',str);
        
        str='用和训练样本相似的方法建立新的测试样本';
        set(findobj('Tag','edit2'),'String',str);
        
        user_data.tg=tg;
        user_data.pgseq=pgseq;
        user_data.chose=user_data.chose+1;
        set(hObject,'userdata',user_data);
        
        set(findobj('Tag','text1'),'String','step 7');
    case 8
        pgseq=user_data.pgseq;
        tg=user_data.tg;
        net=user_data.net;
        t5=1:80;
        
        a=sim(net,pgseq);
      %  h=subplot('position',[17.0 16.077 80.2 23.154]);
        h=findobj('Tag','axes1');
        axes(h);
        hold off;
  %      set(h,'NextPlot','replace');
 %       set(findobj('Tag','axes1'),'NextPlot','add');
        plot(t5,cat(2,a{:}),t5,tg,'b--'); 
        
        str=sprintf('>> a=sim(net,pgseq);\n>> plot(findobj(''Tag'',''axes1''),t5,cat(2,a{:}),t5,tg,''b--'');');
        set(findobj('Tag','edit1'),'String',str);
        
        str='将测试输入样本提供给网络,利用仿真函数sim产生网络输出,并与测试目标样本相比较,观察网络的性能';
        set(findobj('Tag','edit2'),'String',str);
        
        set(hObject,'String','End');
        user_data.chose=user_data.chose+1;
        set(hObject,'userdata',user_data);
        set(findobj('Tag','text2'),'String','step 8');
    case 9
        set(hObject,'String','Start')
        set(findobj('Tag','edit2'),'String','请按‘开始‘按钮启动演示');

        str1='Elman神经网络是Elman于1990年提出的,该模型在前馈网络的隐含层增加一个承接层,作为一步延时算子,'
        str2='达到记忆的目的,从而使系统具有适应时变特性的能力,能直接反映动态过程的特性';
        str=strcat(str1,str2);
        set(findobj('Tag','edit1'),'String',str)
        
        set(findobj('Tag','text1'),'String','开始');
        set(findobj('Tag','text2'),'String','step 0');
                
        temp_data.chose=1;
        set(hObject,'userdata',temp_data);
        
end

function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function PB_T_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PB_T (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

user_data.chose=1;
set(findobj('Tag','PB_T'),'userdata',user_data);



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

close;
SysGui;


⌨️ 快捷键说明

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