1;

function parms=fitgaussians(x,y,inparms)
  parms=nonlin_curvefit(@gaussians,inparms,x,y);
  plot_results(x,y,parms);
endfunction

function y=gaussians(parms,x)
  y=x*0;
  nsets=(numel(parms)-1)/2;
  for i=0:nsets-1
    y=y+exp(-((x-parms(i*2+3)) ./ (0.6005615*parms(1))) .^2) *parms(i*2+2);
  endfor
endfunction

function plot_results(x,y,parms)
  close
  plot(x,y);
  hold all;
  nsets=(numel(parms)-1)/2;
  lcol=zeros(nsets,3);
  lcol(:,2)=1;
  for i=1:nsets
    lcol(i,1)=i/nsets;
    lcol(i,2)=(nsets-i+1)/nsets;
  endfor
  for i=0:nsets-1
    lparms=[parms(1);parms(i*2+2);parms(i*2+3)];
    plpr=sprintf("--;a=%f x=%f w=%f;",lparms(2),lparms(3),lparms(1)); 
    plot(x,gaussians(lparms,x),plpr,"color",lcol(i+1,:));
  endfor
  plot(x,gaussians(parms,x));
  hold off;
  lcol
endfunction

