www.gusucode.com > Matlab动力系统和时间序列分析工具箱 > Matlab动力系统和时间序列分析工具箱/lab432/common/lyap_exp_of_discrete_map.m
function varargout = lyap_exp_of_discrete_map(varargin) % LYAP_EXP_OF_DISCRETE_MAP computes Lyapunov exponents of discrete map % gui-mode using: % LyapExps=lyap_exp_of_discrete_map; % batch-mode using: % LyapExps=lyap_exp_of_discrete_map(Fx,init,len,trans) % Parameters: % Fx - cell array of right-side map equations % init - cell array of initial conditions % len - number of iterations % trans - number of transient iterations % % LyapExps - Lyapunov exponents % % Example: % LyapExps=lyap_exp_of_discrete_map({'3.42*x1*(1-x1-x2)' '3.42*x1*x2'},{'0.1' '0.1'},'5000','100') % Last Modified by GUIDE v2.5 26-Oct-2004 10:52:04 % last modified 8.12.05 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @lyap_exp_of_discrete_map_OpeningFcn, ... 'gui_OutputFcn', @lyap_exp_of_discrete_map_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 lyap_exp_of_discrete_map is made visible. function lyap_exp_of_discrete_map_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to lyap_exp_of_discrete_map (see VARARGIN) % Choose default command line output for lyap_exp_of_discrete_map handles.output = hObject; % Update handles structure guidata(hObject, handles); handles.auto=0; try N=length(varargin{1}); x={}; for i=1:N x{i}=['x' num2str(i)]; end set(handles.x,'string',x); set(handles.f,'string',varargin{1}); set(handles.init,'string',varargin{2}); set(handles.edit_length,'string',varargin{3}); set(handles.e_tr_steps,'string',varargin{4}); catch end if length(varargin)==4 % batch mode OK handles.auto=1; end guidata(hObject, handles); % --- Outputs from this function are returned to the command line. function varargout = lyap_exp_of_discrete_map_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure global ledm if handles.auto ok_Callback(handles.ok, [], handles); if nargout varargout{1} =ledm; end close(handles.figure); else if nargout uiwait; if nargout varargout{1} =ledm; end end end % --- Executes on button press in ok. function ok_Callback(hObject, eventdata, handles) % hObject handle to ok (see GCBO) % handles structure with handles and user data (see GUIDATA) global ledm ledm=[]; try syms asddaewrrjfsd; catch warndlg('Symbolic toolbox not installed. Computation aborted.','Symbolic toolbox needed'); return end clear tmp_map x=get(handles.x,'string'); L=str2num(get(handles.edit_length,'string')); init=get(handles.init,'string'); f=get(handles.f,'string'); Dim=length(f); if isempty(x) msgbox('No data for map'); return end s = which('lyap_exp_of_discrete_map'); [pathstr,name,ext,versn] = fileparts(s); fid = fopen([pathstr '/tmp_map.m'],'w'); fprintf(fid,'function Y=tmp_map(X); \n'); fprintf(fid,'Y=zeros(1,%s); \n',num2str(length(x))); for i=1:Dim fprintf(fid,'%s=X(%i); \n',x{i},i); end for i=1:Dim fprintf(fid,'Y(%s)=%s; \n',num2str(i),f{i}); end fclose(fid); clear fid for i=1:Dim Init(i)=str2double(init{i}); end for i=1:str2num(get(handles.e_tr_steps,'string')) Init=tmp_map(Init); end F=[]; X=[]; for i=1:Dim F=[F; sym(f{i})]; X=[X sym(['x' num2str(i)])]; end J=jacobian(F,X); Xer=[]; for i=1:Dim^2 Xer=[Xer; sym(['x' num2str(i) 'er'])]; end system=[]; for i=1:Dim system=[system; J*Xer(1+(i-1)*Dim:i*Dim)]; end fid = fopen([pathstr '/tmp_map.m'],'a'); for i=1:length(Xer) fprintf(fid,'%s=X(%i); \n',char(Xer(i)),Dim+i); end fprintf(fid,'Y=[Y zeros(1,%s)]; \n',num2str(length(system))); for i=1:length(system) fprintf(fid,'Y(%s)=%s; \n',num2str(Dim+i),char(system(i))); end fclose(fid); clear fid clear tmp_map Vect=eye(Dim); S=zeros(Dim,1); Y=Init'; Y=[Y; Vect(1:end)']; M=str2num(get(handles.edit_length,'string')); for i=1:M Y=tmp_map(Y); vectors=Y(end-Dim^2+1:end); for t=1:Dim Vect(:,t)=vectors(1+(t-1)*Dim:t*Dim)'; end S(1)=S(1)+log(sum(Vect(:,1).^2)^.5); Vect(:,1)=Vect(:,1)./sum(Vect(:,1).^2)^.5; for j=2:Dim tmp_v=zeros(Dim,1); for k=1:j-1 tmp_v=tmp_v+sum(Vect(:,j).*Vect(:,k)).*Vect(:,k); end Vect(:,j)=Vect(:,j)-tmp_v; S(j)=S(j)+log(sum(Vect(:,j).^2)^.5); Vect(:,j)=Vect(:,j)./sum(Vect(:,j).^2)^.5; end Y=[Y(1:Dim) Vect(1:end)]'; end fprintf('\nLyapunov exponents:\n '); Le=S./M; for i=1:Dim fprintf(' %f\n',Le(i)); end ledm=Le; % --- Executes on button press in cancel. function cancel_Callback(hObject, eventdata, handles) % hObject handle to cancel (see GCBO) % handles structure with handles and user data (see GUIDATA) close(handles.figure); % --- Executes during object creation, after setting all properties. function edit_length_CreateFcn(hObject, eventdata, handles) % hObject handle to edit_length (see GCBO) % handles empty - handles not created until after all CreateFcns called if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end function edit_length_Callback(hObject, eventdata, handles) % hObject handle to edit_length (see GCBO) % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in load. function load_Callback(hObject, eventdata, handles) % hObject handle to load (see GCBO) % handles structure with handles and user data (see GUIDATA) [file,path] = uigetfile2('*.mat','Load Map'); if file~=0 try S=load([path file]); set([handles.x handles.f handles.init],'value',1); set(handles.x,'string',S.map.x); set(handles.f,'string',S.map.f); set(handles.init,'string',S.map.init); set(handles.edit_info,'string',S.map.info); set(handles.e_tr_steps,'string',S.map.tr_steps); set(handles.edit_length,'string',S.map.length); catch errordlg('Wrong file format','File error'); end end % --- Executes on button press in save. function save_Callback(hObject, eventdata, handles) % hObject handle to save (see GCBO) % handles structure with handles and user data (see GUIDATA) [file,path] = uiputfile('*.mat','Save Map As'); if file~=0 map.x=get(handles.x,'string'); map.f=get(handles.f,'string'); tmp=map.x; for i=1:length(tmp) tmp{i}='0'; end map.ignv=tmp; map.innv=tmp; map.agnv=tmp; map.annv=tmp; map.init=get(handles.init,'string'); map.info=get(handles.edit_info,'string'); map.tr_steps=get(handles.tr_steps,'string'); map.length=get(handles.edit_length,'string'); save([path file],'map'); end % --- Executes during object creation, after setting all properties. function edit_info_CreateFcn(hObject, eventdata, handles) % hObject handle to edit_info (see GCBO) % handles empty - handles not created until after all CreateFcns called if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end function edit_info_Callback(hObject, eventdata, handles) % hObject handle to edit_info (see GCBO) % handles structure with handles and user data (see GUIDATA) % --- Executes during object creation, after setting all properties. function x_CreateFcn(hObject, eventdata, handles) % hObject handle to x (see GCBO) % handles empty - handles not created until after all CreateFcns called if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes on selection change in x. function x_Callback(hObject, eventdata, handles) % hObject handle to x (see GCBO) % handles structure with handles and user data (see GUIDATA) set([handles.x handles.f handles.init],'value',get(hObject,'Value')); % --- Executes during object creation, after setting all properties. function f_CreateFcn(hObject, eventdata, handles) % hObject handle to f (see GCBO) % handles empty - handles not created until after all CreateFcns called if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes during object creation, after setting all properties. function init_CreateFcn(hObject, eventdata, handles) % hObject handle to init (see GCBO) % handles empty - handles not created until after all CreateFcns called if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes on button press in add. function add_Callback(hObject, eventdata, handles) % hObject handle to add (see GCBO) % handles structure with handles and user data (see GUIDATA) N=length(get(handles.x,'string')); x={}; for i=1:N x{i}=['x' num2str(i)]; end set(handles.x,'string',x); prompt = {['Enter equation x' num2str(N+1) '(n+1)=...'],... 'Enter initial condition:'}; dlg_title = 'Add new equation'; num_lines= 1; def = {'','0.5'}; answer = inputdlg(prompt,dlg_title,num_lines,def); if ~isempty(answer) x=get(handles.x,'string'); f=get(handles.f,'string'); init=get(handles.init,'string'); x{N+1}=['x' num2str(N+1)]; f{N+1}=answer{1}; init{N+1}=answer{2}; set([handles.x, handles.f, handles.init],'value',N+1); set(handles.x,'string',x); set(handles.f,'string',f); set(handles.init,'string',init); end % --- Executes on button press in modify. function modify_Callback(hObject, eventdata, handles) % hObject handle to modify (see GCBO) % handles structure with handles and user data (see GUIDATA) x=get(handles.x,'string'); f=get(handles.f,'string'); init=get(handles.init,'string'); selection=get(handles.x,'value'); if isempty(x) x{1}='x1'; f{1}=''; init{1}='0.7'; end prompt = {['Enter equation ' x{selection} '(n+1)=...'],... 'Enter initial condition:'}; dlg_title = 'Modify equation'; num_lines= 1; def = {f{selection},init{selection}}; answer = inputdlg(prompt,dlg_title,num_lines,def); if ~isempty(answer) f{selection}=answer{1}; init{selection}=answer{2}; set(handles.x,'string',x); set(handles.f,'string',f); set(handles.init,'string',init); end % --- Executes on button press in delete. function delete_Callback(hObject, eventdata, handles) % hObject handle to delete (see GCBO) % handles structure with handles and user data (see GUIDATA) x=get(handles.x,'string'); f=get(handles.f,'string'); init=get(handles.init,'string'); selection=get(handles.x,'value'); if isempty(x) return end n_f={}; n_init={}; n_x={}; k=1; for i=1:length(x) if i~=selection n_f{k}=f{i}; n_init{k}=init{i}; n_x{k}=x{i}; k=k+1; end end set([handles.x, handles.f, handles.init],'value',max([1 selection-1])); set(handles.x,'string',n_x); set(handles.f,'string',n_f); set(handles.init,'string',n_init); % --- Executes during object creation, after setting all properties. function e_tr_steps_CreateFcn(hObject, eventdata, handles) % hObject handle to e_tr_steps (see GCBO) % 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 function e_tr_steps_Callback(hObject, eventdata, handles) % hObject handle to e_tr_steps (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of e_tr_steps as text % str2double(get(hObject,'String')) returns contents of e_tr_steps as a double