function [calcabs calcconcs rms finalrms]=mcr(obsdata,calcconcs,calcabs,firstdimension)
	% 12 May 2011
	% unconstrained multivariate curve resolution
	disp("mcr")
	convergence=1e-9;maxit=500;

	%calcabs is matrix of extinction coefficients
	%calcconcs is matrix of concentrations

	%which data set takes precendence
	if firstdimension=="col"
		calcabs=pinv(calcconcs)*obsdata;
	else 
		calcconcs=obsdata*pinv(calcabs);
	endif

	rms=ones(1,maxit+1).*NA;

	resids=obsdata-(calcconcs*calcabs);
	tempstor=sqrt(mean((resids.^2)));
	rms(1)=mean(tempstor);

	for i=1:maxit
		
		calcconcs=obsdata*pinv(calcabs);
		calcabs=pinv(calcconcs)*obsdata;

		resids=obsdata-calcconcs*calcabs;
		tempstor=sqrt(mean(resids.^2));
		rms(i+1)=mean(tempstor);
		if (abs(rms(i)-rms(i+1))< convergence)
			break
		endif
	endfor

	rms=rms(1:i+1); %residual for each iteration
	noit=i
	finalrms=abs(rms(i+1)-rms(i))
endfunction
