function[AMP, freq, phase] = mcmc(A,f,phi,AMP,freq,phase,z,k) % step is the step size step = 0.01 + exp(-k/1000); counteramp=0; while counteramp == 0 Anew = abs(A + 0.5*step*randn); if Anew>=1 counteramp=1; end end counterfreq=0; while counterfreq ==0 fnew = abs(f + (step/100)*randn); if fnew >= 0.01 counterfreq=1; end end counterphi = 0; while counterphi ==0 phinew = abs(phi +2*pi*step*randn); if (phinew > 0) & (phinew <2*pi) counterphi = 1; end end oldprob = probabilityphi(AMP,freq,phase); trialA=AMP;trialf=freq;trialphi=phase; trialA(1,z)=Anew;trialf(1,z)=fnew;trialphi(1,z)=phinew; newprob = probabilityphi(trialA,trialf,trialphi); if (newprob-oldprob) > 0 AMP = trialA;freq=trialf;phase=trialphi; else if ((k>0)& ((newprob/oldprob)>=rand)) AMP = trialA;freq=trialf;phase=trialphi; end end