www.gusucode.com > Matlab动力系统和时间序列分析工具箱 > Matlab动力系统和时间序列分析工具箱/lab432/common/poincare_section.m
function varargout = poincare_section(varargin) % POINCARE_SECTION computes Poincare section for 3rd-order ODE system % GUI-mode using: % data=poincare_section; % batch-mode using: % data=poincare_section(Fx,init,time,tr_time,solver,addit_param,C,direction,pl) % Parameters % Fx - cell array of right-side ODE equations % init - cell array of initial conditions % time - a vector specifying the interval of integration. % tr_time - a vector specifying the interval of preliminary integration % solver - solver type: % 1 - ode45; % 2 - ode23; % 3 - ode113; % 4 - ode15s; % 5 - ode23s; % 6 - ode23t; % 7 - ode23tb; % addit_param - additional parameters of ODE solver, such as 'RelTol', 'AbsTol' and others. % C - Poincare plane: C(1)x+C(2)y+C(3)z=C(4) % direction - taked into account direction of crossing the plane: % 'both' - both directions taked into account % '-+' - only direction from negative to positive half-spaces % '+-' - only direction from positive to negative half-spaces % pl - 1 - plot projection of Poincare section on coordinate axis % 0 - do not plot projection of Poincare section on coordinate axis % % data - time of crossing (first column) and coordanates of crossing (other % columns) the plane % % Examples: % data=poincare_section({'0.032*x1+0.5*x2-x3' 'x1-0.1*x2-x1^3' '0.1*x1'},{'0.01' '-0.01' '-0.02'},'[0:0.02:250]','[-10 0]',1,'',[1 1 1 0],'both',1); % data=poincare_section({'10*(x2-x1)' '28*x1-x2-x1*x3' '-8/3*x3+x1*x2'},{'8' '5' '30'},'[0:0.005:350]','[-30 0]',1,'',[1 1 0 -20],'+-',0); % data=poincare_section({'-x2-x3' 'x1+0.25*x2+x4' '3+x1*x3' '-0.5*x3+0.05*x4'},{'-15' '11' '0.2' '23'},'[0:0.01:1350]','[-200 0]',1,'',[0 1 0 0],'+-',1); % data=poincare_section({'-x2-x3' 'x1+0.2*x2' '0.4+x3*(x1-8)'},{'3' '-4' '2'},'[0:0.02:1500]','[-20 0]',1,'',[-10 1 0.8 0],'-+',1); % author: Max Logunov, lab432@mail.ru, comments welcome % This program is a part of Lab432 software for nonlinear analysis of time % series % Last Modified by GUIDE v2.5 21-Feb-2005 17:11:32 % 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', @poincare_section_OpeningFcn, ... 'gui_OutputFcn', @poincare_section_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 discrete_map_generator is made visible. function poincare_section_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 discrete_map_generator (see VARARGIN) % Choose default command line output for discrete_map_generator handles.output = hObject; % Update handles structure guidata(hObject, handles); handles.auto=0; try N=length(varargin{1}); x={}; for i=1:N x{i}=['dx' num2str(i) '/dt']; 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.tr_time,'string',varargin{4}); set(handles.solver,'value',varargin{5}); set(handles.addit_param,'string',varargin{6}); set(handles.e_C1,'string',num2str(varargin{7}(1))); set(handles.e_C2,'string',num2str(varargin{7}(2))); set(handles.e_C3,'string',num2str(varargin{7}(3))); set(handles.e_C,'string',num2str(varargin{7}(4))); if strcmp(varargin{8},'-+') set(handles.r_mp,'value',1); set([handles.r_pm handles.r_b],'value',0); end if strcmp(varargin{8},'+-') set(handles.r_pm,'value',1); set([handles.r_mp handles.r_b],'value',0); end set(handles.plot,'value',varargin{9}); catch end if length(varargin)==9 % batch mode OK handles.auto=1; end % if length(get(handles.x,'string'))~=3 % handles.auto=0; % set(handles.ok,'enable','off'); % end guidata(hObject, handles); % --- Outputs from this function are returned to the command line. function varargout = poincare_section_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 psDATA if handles.auto ok_Callback(handles.ok, [], handles); if nargout varargout{1} =psDATA; end close(handles.figure); else if nargout uiwait; varargout{1} =psDATA; 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 psDATA clear tmp_sys psDATA=[]; x=get(handles.x,'string'); init=get(handles.init,'string'); f=get(handles.f,'string'); if isempty(x) msgbox('No data for system'); return end s = which('poincare_section'); [pathstr,name,ext,versn] = fileparts(s); init_str=''; for i=1:length(x) init_str=[init_str init{i} ' ']; end init_str=['[' init_str(1:end-1) ']']; fid = fopen([pathstr '/tmp_sys.m'],'w'); fprintf(fid,'function Y=tmp_sys(t,X); \n'); fprintf(fid,'Y=zeros(%s,1); \n',num2str(length(x))); for i=1:length(x) fprintf(fid,'%s=X(%i); \n',['x' num2str(i)],i); end for i=1:length(x) fprintf(fid,'Y(%s)=%s; \n',num2str(i),f{i}); end fclose(fid); clear fid solvers=get(handles.solver,'string'); if ~isempty(get(handles.tr_time,'string')) if ~isempty(get(handles.addit_param,'string')) [T,Y]=eval([solvers{get(handles.solver,'value')} '(@tmp_sys,' get(handles.tr_time,'string') ',' init_str ',odeset(' get(handles.addit_param,'string') '))']); else [T,Y]=eval([solvers{get(handles.solver,'value')} '(@tmp_sys,' get(handles.tr_time,'string') ',' init_str ')']); end clear tmp_sys; init_str=''; for i=1:length(x) init_str=[init_str num2str(Y(end,i)) ' ']; end init_str=['[' init_str(1:end-1) ']']; end if ~isempty(get(handles.addit_param,'string')) [T,Y]=eval([solvers{get(handles.solver,'value')} '(@tmp_sys,' get(handles.edit_length,'string') ',' init_str ',odeset(' get(handles.addit_param,'string') '))']); else [T,Y]=eval([solvers{get(handles.solver,'value')} '(@tmp_sys,' get(handles.edit_length,'string') ',' init_str ')']); end clear tmp_sys; c1=str2num(get(handles.e_C1,'string')); c2=str2num(get(handles.e_C2,'string')); c3=str2num(get(handles.e_C3,'string')); c=str2num(get(handles.e_C,'string')); R=c1*Y(:,1)+c2*Y(:,2)+c3*Y(:,3)-c; RES=zeros(floor(length(R)/2),4); k=0; if get(handles.r_b,'value') for i=2:length(R)-2 if ~(sign(R(i))+sign(R(i+1))) t1=abs(R(i)/R(i+1))*(T(i+1)-T(i))/(1+abs(R(i)/R(i+1))); nT=T(i)+t1; nD=[spline(T(i-1:i+2),Y(i-1:i+2,1),nT),spline(T(i-1:i+2),Y(i-1:i+2,2),nT), spline(T(i-1:i+2),Y(i-1:i+2,3),nT)]; k=k+1; RES(k,1)=nT; RES(k,2:end)=nD; end end end if get(handles.r_mp,'value') for i=2:length(R)-2 if sign(R(i))<sign(R(i+1)) t1=abs(R(i)/R(i+1))*(T(i+1)-T(i))/(1+abs(R(i)/R(i+1))); nT=T(i)+t1; nD=[spline(T(i-1:i+2),Y(i-1:i+2,1),nT),spline(T(i-1:i+2),Y(i-1:i+2,2),nT), spline(T(i-1:i+2),Y(i-1:i+2,3),nT)]; k=k+1; RES(k,1)=nT; RES(k,2:end)=nD; end end end if get(handles.r_pm,'value') for i=2:length(R)-2 if sign(R(i))>sign(R(i+1)) t1=abs(R(i)/R(i+1))*(T(i+1)-T(i))/(1+abs(R(i)/R(i+1))); nT=T(i)+t1; nD=[spline(T(i-1:i+2),Y(i-1:i+2,1),nT),spline(T(i-1:i+2),Y(i-1:i+2,2),nT), spline(T(i-1:i+2),Y(i-1:i+2,3),nT)]; k=k+1; RES(k,1)=nT; RES(k,2:end)=nD; end end end psDATA=RES(1:k,:); if get(handles.plot,'value') figure('Name','Poincare section','numbertitle','off','color',[1 1 1]); subplot(221) plot3(Y(:,1),Y(:,2),Y(:,3)), grid on hold on plot3(psDATA(:,2),psDATA(:,3),psDATA(:,4),'r.','markersize',6); xlabel('x1'); ylabel('x2'); zlabel('x3'); subplot(222) plot(psDATA(:,2),psDATA(:,3),'r.','markersize',2), grid on; xlabel('x1'); ylabel('x2'); subplot(223) plot(psDATA(:,2),psDATA(:,4),'r.','markersize',2), grid on; xlabel('x1'); ylabel('x3'); subplot(224) plot(psDATA(:,3),psDATA(:,4),'r.','markersize',2), grid on; xlabel('x2'); ylabel('x3'); end % --- 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 System'); if file~=0 try S=load([path file]); set([handles.x handles.f handles.init],'value',1); set(handles.x,'string',S.sys.dx); set(handles.f,'string',S.sys.f); set(handles.init,'string',S.sys.init); set(handles.edit_info,'string',S.sys.info); set(handles.tr_time,'string',S.sys.tr_time); set(handles.edit_length,'string',S.sys.time); set(handles.addit_param,'string',S.sys.addit_param); str=get(handles.solver,'string'); for i=1:length(str) if strcmp(str{i},S.sys.solver) set(handles.solver,'value',i); break end end catch errordlg('Wrong file format','File error'); end end % if length(get(handles.x,'string'))~=3 % set(handles.ok,'enable','off'); % else % set(handles.ok,'enable','on'); % 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 System As'); if file~=0 sys.dx=get(handles.x,'string'); sys.f=get(handles.f,'string'); tmp=sys.dx; for i=1:length(tmp) tmp{i}='0'; end sys.ignv=tmp; sys.innv=tmp; sys.agnv=tmp; sys.annv=tmp; sys.init=get(handles.init,'string'); sys.info=get(handles.edit_info,'string'); sys.tr_time=get(handles.tr_time,'string'); sys.time=get(handles.edit_length,'string'); str=get(handles.solver,'string'); sys.solver=str{get(handles.solver,'value')}; sys.addit_param=get(handles.addit_param,'string'); save([path file],'sys'); 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}=['dx' num2str(i) '/dt']; end set(handles.x,'string',x); prompt = {['Enter equation dx' num2str(N+1) '/dt=...'],... '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}=['dx' num2str(N+1) '/dt']; 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 if length(get(handles.x,'string'))~=3 set(handles.ok,'enable','off'); else set(handles.ok,'enable','on'); 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}='dx1/dt'; f{1}=''; init{1}='0.7'; end prompt = {['Enter equation ' x{selection} '=...'],... '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 if length(get(handles.x,'string'))~=3 set(handles.ok,'enable','off'); else set(handles.ok,'enable','on'); 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); if length(get(handles.x,'string'))~=3 set(handles.ok,'enable','off'); else set(handles.ok,'enable','on'); end N=length(get(handles.x,'string')); x={}; for i=1:N x{i}=['dx' num2str(i) '/dt']; end set(handles.x,'string',x); % --- Executes during object creation, after setting all properties. function solver_CreateFcn(hObject, eventdata, handles) % hObject handle to solver (see GCBO) % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu 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 set(hObject,'string',{'ode45' 'ode23' 'ode113' 'ode15s' 'ode23s' 'ode23t' 'ode23tb'}); % --- Executes on selection change in solver. function solver_Callback(hObject, eventdata, handles) % hObject handle to solver (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: contents = get(hObject,'String') returns solver contents as cell array % contents{get(hObject,'Value')} returns selected item from solver % --- Executes during object creation, after setting all properties. function addit_param_CreateFcn(hObject, eventdata, handles) % hObject handle to addit_param (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 addit_param_Callback(hObject, eventdata, handles) % hObject handle to addit_param (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of addit_param as text % str2double(get(hObject,'String')) returns contents of addit_param as a double % --- Executes during object creation, after setting all properties. function tr_time_CreateFcn(hObject, eventdata, handles) % hObject handle to tr_time (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 tr_time_Callback(hObject, eventdata, handles) % hObject handle to tr_time (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of tr_time as text % str2double(get(hObject,'String')) returns contents of tr_time as a double % --- Executes during object creation, after setting all properties. function e_C1_CreateFcn(hObject, eventdata, handles) % hObject handle to e_C1 (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_C1_Callback(hObject, eventdata, handles) % hObject handle to e_C1 (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of e_C1 as text % str2double(get(hObject,'String')) returns contents of e_C1 as a double % --- Executes during object creation, after setting all properties. function e_C2_CreateFcn(hObject, eventdata, handles) % hObject handle to e_C2 (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_C2_Callback(hObject, eventdata, handles) % hObject handle to e_C2 (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of e_C2 as text % str2double(get(hObject,'String')) returns contents of e_C2 as a double % --- Executes during object creation, after setting all properties. function e_C3_CreateFcn(hObject, eventdata, handles) % hObject handle to e_C3 (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_C3_Callback(hObject, eventdata, handles) % hObject handle to e_C3 (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of e_C3 as text % str2double(get(hObject,'String')) returns contents of e_C3 as a double % --- Executes during object creation, after setting all properties. function e_C_CreateFcn(hObject, eventdata, handles) % hObject handle to e_C (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_C_Callback(hObject, eventdata, handles) % hObject handle to e_C (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of e_C as text % str2double(get(hObject,'String')) returns contents of e_C as a double % --- Executes during object creation, after setting all properties. function e_E_CreateFcn(hObject, eventdata, handles) % hObject handle to e_E (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_E_Callback(hObject, eventdata, handles) % hObject handle to e_E (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of e_E as text % str2double(get(hObject,'String')) returns contents of e_E as a double % --- Executes on button press in r_mp. function r_mp_Callback(hObject, eventdata, handles) % hObject handle to r_mp (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of r_mp set(handles.r_mp,'value',1); set([handles.r_pm handles.r_b],'value',0); % --- Executes on button press in r_pm. function r_pm_Callback(hObject, eventdata, handles) % hObject handle to r_pm (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of r_pm set(handles.r_pm,'value',1); set([handles.r_mp handles.r_b],'value',0); % --- Executes on button press in r_b. function r_b_Callback(hObject, eventdata, handles) % hObject handle to r_b (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of r_b set(handles.r_b,'value',1); set([handles.r_pm handles.r_mp],'value',0); % --- Executes on button press in plot. function plot_Callback(hObject, eventdata, handles) % hObject handle to plot (see GCBO) % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of plot