load ../../050517_calsrc1_edit.mat plot(t,v) csfv=fit_calsrc(t,v) hist(csfv(:,4),100)
The way sinusoid is written the phase is measured in cycles. We are interested in phase as angle so we wrap it into the range 0 to 1 and mult by 360:
hist(mod(csfv(:,4),1)*360,100); xlim([0,360]);
We see four groups roughly spaced by 90 deg. The bolometers grid angles in the focal plane are separated by 45 deg steps - why do we see 90 deg seps in the output waveforms? (Hint: pol is a "headless vector".)
plot(v) [mv,j]=min(min(v)) plot(v(:,j)) [mv,i]=min(v(:,j)) v(i-4:i+15,j)=NaN; plot(v(:,j))
lsqnonlin does not treat NaN's as missing values (although regress does), so we must filter them out ahead of time inside the loop of fit_calsrc.
Make some "data" with differing errors:
x=[1:10]' p=[0.5,1] y=polyval(p,x) plot(x,y,'.') e(1:5,1)=0.1; e(6:10,1)=1; y=y+e.*randn(size(y)); errorbar(x,y,e,'.');
Fit it first ignoring our knowledge of the uncertainties and again including this knowledge - remember lsqnonlin squares and sums the vector of numbers we feed it so we are just minimizing chi-squared in the usual manner.
pf1=lsqnonlin(@(v) y-polyval(v,x),p) pf2=lsqnonlin(@(v) (y-polyval(v,x))./e,p) hold on plot(x,polyval(pf1,x),'r') plot(x,polyval(pf2,x),'g') hold off
Run it a few times and we see the benefit of down weighting the higher uncertainty points.
cd mkdir matlab cd matlab emacs startup.m
Add the lines "dbstop if error" and "addpath('home/pryke/matlab']);"
cp ~pryke/matlab/hfillc.c . cp ~pryke/matlab/makefile . emacs hfillc.c makefile & make
Now in Matlab window type "rehash" and "which hfillc" - we see it on path. At the moment to have it run we need to exit matlab, type "setenv LD_PRELOAD /lib/libgcc_s.so.1" and restart. (This is a silly complication and best not to worry about why it's needed right now.)
It's always best to put an m file wrapper around a mex file to do things like argument checking etc:
cp ~pryke/matlab/hfill.m . cp ~pryke/matlab/hplot.m .
"rehash" and "help hfill" tells us about how to use it.
x=randn(1e6,1); tic; [bc1,n1]=hfill(x,100,-3,3); toc tic; [bc1,n1]=hfill_alt(x,100,-3,3); toc
Can you get it anywhere near as fast as the C-code verison? Can you avoid a loop over bins? (I don't know how.)
cp -rp ~pryke/matlab/minuit . make
We also need the wrapper and the "figure of merit functions":
cp ~pryke/matlab/matmin.m . cp ~pryke/matlab/chisq.m . cp ~pryke/matlab/logl.m . cp ~pryke/gauss.m .
Now type "rehash" and "help matmin" to see the instructions. Finally we are all set to use MINUIT. Let's go back to the example above and check it gives same result as lsqnonlin:
x=[1:10]' p=[0.5,1] y=polyval(p,x) e(1:5,1)=0.1; e(6:10,1)=1; y=y+e.*randn(size(y)); errorbar(x,y,e,'.'); pf=lsqnonlin(@(v) (y-polyval(v,x))./e,p) pf=matmin('chisq',p,[],'polyval',y,e,x)
Except now we can get the errors on the parameters:
[pf,pfe]=matmin('chisq',p,[],'polyval',y,e,x)
Note that MINUIT prints out the correlation matrix of the parameters.