📄 distributed_synch.m
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Simulation of distributed sychronization% Author: K. Passino% Version: Sept. 1, 2001%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clear allglobal N K omegaN=10; % The number of oscillators% To study phase transitions:omega=pi*rand(1,N); % Set natural frequencies of the oscillators%g0=(1/(sqrt((2*pi)^N))); % The g0 function for a Gaussiang0=1/piKc=(2/(pi*g0))epsilon=Kc*0.1; % So can add or subtract 10% of the value to see the phase transitionK=Kc-epsilon; % The strength of coupling% For now just use:omega=ones(1,N); % Keep them all the sameK=1;% Define simulation parameters:Tfinal=20; % Units are seconds (keep it even)%Tspan=[0 Tfinal];Tspan=0:0.01:Tfinal+0.01;% Define initial conditions:theta0=2*pi*rand(1,N) % Pick a uniform distribution of inital phase angles for the oscillators[t,z]=ode45('distributed_sych_func',Tspan,[theta0]);%[t,z]=ode15s('distributed_sych_func',Tspan,[theta0]);theta=z; % Oscillator statesthetabar=mean(theta');% Convert for plotting%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Plot the plant signals%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(1) % Not really useful:clfsubplot(211)polar(theta(:,1),1+0*theta(:,1),'k-');hold onfor i=1:N polar(theta(:,i),1+0*theta(:,i),'r-'); hold onendhold offxlabel('Position')ylabel('Velocity')title('Oscillator trajectory')subplot(212)plot(sin(theta(:,1)),cos(theta(:,1)),'k-')xlabel('Position')ylabel('Velocity')title('Oscillator trajectory')%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(2) clf % To get insights into dynamics+Lyap. functionssubplot(211)for i=1:N-1 plot(t,theta(:,i)-theta(:,i+1),'k-') hold onendylabel('Phase error')title('Phase errors (black)')subplot(212)for k=1:length(t) V(k,1)=0; for i=1:N-1 V(k,1)=V(k,1)+(0.5)*((theta(k,i)-theta(k,i+1)))*((theta(k,i)-theta(k,i+1))); endend plot(t,V,'b-')xlabel('Time, sec.')ylabel('V')title('Lyap. candidate (blue)')%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% To get insights into dynamicsfigure(3) clfsubplot(211)for i=1:N-1 plot(t,sin(theta(:,i)-theta(:,i+1)),'r-') hold onendtitle('Sin and sin^2 of phase errors')ylabel('sin(diff)')subplot(212)for i=1:N-1 plot(t,sin(theta(:,i)-theta(:,i+1)).^2,'g-.') hold onendhold offxlabel('Time, sec.')ylabel('sin^2(diff)')%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% To get insights into stability analysis, plot potential Lyapunov functionsfigure(4) clfVtot=0*ones(length(t),1);% The following goes to zero, but can increase (sin of difference)for i=1:N-1 V(:,i)=(sin(theta(:,i)-theta(:,i+1))); Vtot=Vtot+V(:,i);endfor i=1:N-1 plot(t,V(:,i),'r-') hold onendplot(t,Vtot,'b-')ylabel('sum of sin(diff) (blue) and sin of each diff (red) (but only in order of numbers)')xlabel('Time,sec.')hold on%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(5) clfclear V% Try another (sum of sin of differences)Vsum=0*ones(length(t),1);for k=1:length(t); for i=1:N summ=0; for j=1:N % Create the sum for each oscillator that links the oscillators difftheta=theta(k,j)-theta(k,i); summ=summ+sin(difftheta); end V(k,i)=summ; endVsum(k,1)=sum(V(k,i));endfor i=1:N plot(t,V(:,i),'r-') hold onendplot(t,Vsum(:,1),'b--')hold offxlabel('Time, sec.')ylabel('Phase error')title('Indiv. sums (red) and total (blue)')pause%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Try to do it simultaneously... interleave the points, but only for two of themfor i=1:2:length(t) xx(i)=sin(theta(i,1)); xx(i+1)=sin(theta(i,2)); yy(i)=cos(theta(i,1)); yy(i+1)=cos(theta(i,2));end figure(7) % This does it - - with a line between the phase angles, but with some kind of errorclfplot(sin(theta(:,1)),cos(theta(:,1)),'k.')hold onxlabel('Position')ylabel('Velocity')title('Oscillator trajectory')comet(xx,yy,10) % Ok, this is odd, but it was the only way I could get it to keep the comet plot for printing.% Note that if you do not really need the motion of the comet trajectory you could simply take the points and draw a line% between them for each iteration.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End of program%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -