0001 function newring=atfittune(ring,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 if isscalar(varargin{1}) && isnumeric(varargin{1})
0020 dpp=varargin{1};
0021 [newtunes,famname1,famname2]=deal(varargin{2:end});
0022 else
0023 dpp=0;
0024 [newtunes,famname1,famname2]=deal(varargin{:});
0025 end
0026
0027 idx1=varelem(ring,famname1);
0028 idx2=varelem(ring,famname2);
0029 newtunes=newtunes-floor(newtunes);
0030
0031 kl1=atgetfieldvalues(ring(idx1),'PolynomB',{2});
0032 kl2=atgetfieldvalues(ring(idx2),'PolynomB',{2});
0033 if true
0034 delta = 1e-6;
0035
0036
0037 [lindata, tunes] = atlinopt(ring,dpp);
0038
0039
0040 [lindata, tunes1] = atlinopt(setqp(ring,idx1,kl1,delta),dpp);
0041 [lindata, tunes2] = atlinopt(setqp(ring,idx2,kl2,delta),dpp);
0042
0043
0044 J = ([tunes1(:) tunes2(:)] - [tunes(:) tunes(:)])/delta;
0045 dK = J\(newtunes(:)-tunes(:));
0046 else
0047 dK=0.01*fminsearch(@funtune,[0;0],...
0048 optimset(optimset('fminsearch'),'Display','iter','TolX',1.e-5));
0049 end
0050 newring = setqp(ring,idx1,kl1,dK(1));
0051 newring = setqp(newring,idx2,kl2,dK(2));
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 function ring2=setqp(ring,idx,k0,delta)
0065 k=k0*(1+delta);
0066 ring2=ring;
0067 ring2(idx)=atsetfieldvalues(atsetfieldvalues(ring2(idx),'K',k),'PolynomB',{2},k);
0068 end
0069
0070 function res=varelem(ring,arg)
0071 if islogical(arg)
0072 res=arg;
0073 elseif isnumeric(arg)
0074 res=false(size(ring));
0075 res(arg)=true;
0076 elseif ischar(arg)
0077 res=atgetcells(ring,'FamName',arg);
0078 elseif iscell(arg)
0079 res=false(size(ring));
0080 for i=1:length(arg)
0081 res=res|varelem(ring,arg{i});
0082 end
0083 else
0084 error('AT:GetElemList:WrongArg','Cannot parse argument');
0085 end
0086 end
0087 end