Difference between revisions of "Greatfit.m (Matlab program fot fitting)"
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | fid = fopen('datafile.m','r'); | ||
− | |||
z = fscanf(fid, '%g'); | z = fscanf(fid, '%g'); | ||
+ | |||
fclose(fid); | fclose(fid); | ||
+ | |||
x=[0:4095]; | x=[0:4095]; | ||
+ | |||
time=[979:1999]; | time=[979:1999]; | ||
+ | |||
counts=([z(979:1999)])'; | counts=([z(979:1999)])'; | ||
plot(x,z,'r'); | plot(x,z,'r'); | ||
+ | |||
xlabel('Channel'); ylabel('Number of Counts'); | xlabel('Channel'); ylabel('Number of Counts'); | ||
+ | |||
hold on | hold on | ||
+ | |||
modelFun = @(p,x) p(1).*exp((-(x-p(2)).^2)/(2*p(3).^2))+p(4).*exp((-(x-p(5)).^2)/(2*p(6).^2))+p(7).*exp((-(x-p(8)).^2)/(2*p(9).^2)); | modelFun = @(p,x) p(1).*exp((-(x-p(2)).^2)/(2*p(3).^2))+p(4).*exp((-(x-p(5)).^2)/(2*p(6).^2))+p(7).*exp((-(x-p(8)).^2)/(2*p(9).^2)); | ||
+ | |||
startingVals = [800 1150 100 500 1250 100 100 1450 300]; | startingVals = [800 1150 100 500 1250 100 100 1450 300]; | ||
+ | |||
coefEsts = nlinfit(time, counts, modelFun, startingVals); | coefEsts = nlinfit(time, counts, modelFun, startingVals); | ||
+ | |||
xgrid = linspace(900,2000,1000); | xgrid = linspace(900,2000,1000); | ||
+ | |||
line(xgrid, modelFun(coefEsts, xgrid), 'Color','m'); | line(xgrid, modelFun(coefEsts, xgrid), 'Color','m'); | ||
+ | |||
plot(x, coefEsts(1).*exp((-(x-coefEsts(2)).^2)/(2*coefEsts(3).^2))+coefEsts(4).*exp((-(x-coefEsts(5)).^2)/(2*coefEsts(6).^2))+coefEsts(7).*exp((-(x-coefEsts(8)).^2)/(2*coefEsts(9).^2)),'g','LineWidth',1.5) | plot(x, coefEsts(1).*exp((-(x-coefEsts(2)).^2)/(2*coefEsts(3).^2))+coefEsts(4).*exp((-(x-coefEsts(5)).^2)/(2*coefEsts(6).^2))+coefEsts(7).*exp((-(x-coefEsts(8)).^2)/(2*coefEsts(9).^2)),'g','LineWidth',1.5) | ||
+ | |||
axis([700 2500 0 1000]) | axis([700 2500 0 1000]) | ||
+ | |||
xsmallarea=0:976; | xsmallarea=0:976; | ||
+ | |||
xlargearea=976:4095; | xlargearea=976:4095; | ||
+ | |||
ysmallarea=coefEsts(1).*exp((-(xsmallarea-coefEsts(2)).^2)/(2*coefEsts(3).^2))+coefEsts(4).*exp((-(xsmallarea-coefEsts(5)).^2)/(2*coefEsts(6).^2))+coefEsts(7).*exp((-(xsmallarea-coefEsts(8)).^2)/(2*coefEsts(9).^2)); | ysmallarea=coefEsts(1).*exp((-(xsmallarea-coefEsts(2)).^2)/(2*coefEsts(3).^2))+coefEsts(4).*exp((-(xsmallarea-coefEsts(5)).^2)/(2*coefEsts(6).^2))+coefEsts(7).*exp((-(xsmallarea-coefEsts(8)).^2)/(2*coefEsts(9).^2)); | ||
+ | |||
ylargearea=coefEsts(1).*exp((-(xlargearea-coefEsts(2)).^2)/(2*coefEsts(3).^2))+coefEsts(4).*exp((-(xlargearea-coefEsts(5)).^2)/(2*coefEsts(6).^2))+coefEsts(7).*exp((-(xlargearea-coefEsts(8)).^2)/(2*coefEsts(9).^2)); | ylargearea=coefEsts(1).*exp((-(xlargearea-coefEsts(2)).^2)/(2*coefEsts(3).^2))+coefEsts(4).*exp((-(xlargearea-coefEsts(5)).^2)/(2*coefEsts(6).^2))+coefEsts(7).*exp((-(xlargearea-coefEsts(8)).^2)/(2*coefEsts(9).^2)); | ||
+ | |||
SmallArea=trapz(xsmallarea,ysmallarea) | SmallArea=trapz(xsmallarea,ysmallarea) | ||
+ | |||
LargeArea=trapz(xlargearea,ylargearea) | LargeArea=trapz(xlargearea,ylargearea) | ||
+ | |||
Ratio_percent=100*SmallArea/(SmallArea+LargeArea) | Ratio_percent=100*SmallArea/(SmallArea+LargeArea) | ||
+ | |||
+ | modelFun1 = @(s,x) s(1).*exp((-(x-s(2)).^2)/(2*s(3).^2)); | ||
+ | |||
+ | startingVals = [800 1150 100 ]; | ||
+ | |||
+ | coefEsts = nlinfit(timegamma, countsgamma, modelFun1, startingVals); | ||
+ | |||
+ | xgrid = linspace(0,900,1000); | ||
+ | |||
+ | line(xgrid, modelFun1(coefEsts, xgrid), 'Color','m'); | ||
+ | |||
+ | plot(x, coefEsts(1).*exp((-(x-coefEsts(2)).^2)/(2*coefEsts(3).^2)),'b','LineWidth',1.1) | ||
+ | |||
+ | |||
+ | [http://www.iac.isu.edu/mediawiki/index.php/Peaks_fitting Go Back] |
Latest revision as of 04:53, 30 October 2008
fid = fopen('datafile.m','r');
z = fscanf(fid, '%g');
fclose(fid);
x=[0:4095];
time=[979:1999];
counts=([z(979:1999)])';
plot(x,z,'r');
xlabel('Channel'); ylabel('Number of Counts');
hold on
modelFun = @(p,x) p(1).*exp((-(x-p(2)).^2)/(2*p(3).^2))+p(4).*exp((-(x-p(5)).^2)/(2*p(6).^2))+p(7).*exp((-(x-p(8)).^2)/(2*p(9).^2));
startingVals = [800 1150 100 500 1250 100 100 1450 300];
coefEsts = nlinfit(time, counts, modelFun, startingVals);
xgrid = linspace(900,2000,1000);
line(xgrid, modelFun(coefEsts, xgrid), 'Color','m');
plot(x, coefEsts(1).*exp((-(x-coefEsts(2)).^2)/(2*coefEsts(3).^2))+coefEsts(4).*exp((-(x-coefEsts(5)).^2)/(2*coefEsts(6).^2))+coefEsts(7).*exp((-(x-coefEsts(8)).^2)/(2*coefEsts(9).^2)),'g','LineWidth',1.5)
axis([700 2500 0 1000])
xsmallarea=0:976;
xlargearea=976:4095;
ysmallarea=coefEsts(1).*exp((-(xsmallarea-coefEsts(2)).^2)/(2*coefEsts(3).^2))+coefEsts(4).*exp((-(xsmallarea-coefEsts(5)).^2)/(2*coefEsts(6).^2))+coefEsts(7).*exp((-(xsmallarea-coefEsts(8)).^2)/(2*coefEsts(9).^2));
ylargearea=coefEsts(1).*exp((-(xlargearea-coefEsts(2)).^2)/(2*coefEsts(3).^2))+coefEsts(4).*exp((-(xlargearea-coefEsts(5)).^2)/(2*coefEsts(6).^2))+coefEsts(7).*exp((-(xlargearea-coefEsts(8)).^2)/(2*coefEsts(9).^2));
SmallArea=trapz(xsmallarea,ysmallarea)
LargeArea=trapz(xlargearea,ylargearea)
Ratio_percent=100*SmallArea/(SmallArea+LargeArea)
modelFun1 = @(s,x) s(1).*exp((-(x-s(2)).^2)/(2*s(3).^2));
startingVals = [800 1150 100 ];
coefEsts = nlinfit(timegamma, countsgamma, modelFun1, startingVals);
xgrid = linspace(0,900,1000);
line(xgrid, modelFun1(coefEsts, xgrid), 'Color','m');
plot(x, coefEsts(1).*exp((-(x-coefEsts(2)).^2)/(2*coefEsts(3).^2)),'b','LineWidth',1.1)