Syntax: lsq(library_matrix, vector_to_be_fitted[, mode])
library_matrix - A two-dimensional matrix (dimensions [m, n, 1]) with the library of known samples (e.g. spectra from mineral library), and a column vector with the unknown sample (e.g. spectrum of some rock) to be fitted with the library.
vector_to_be_fitted - A column vector (dimensions [1, n, 1]) with the sample to be fitted.
The first two inputs must have the same y-dimension.
If using this function for spectral deconvolution, the first input should be a matrix where each column corresponds to a different known sample (usually a known mineral) and each row gives the emissivity or reflectivity value of the samples at a certain wavelength or wavenumber. The second input should be a vector with the unknown sample to be analyzed, and the returned coefficients will be the concentration of each known sample.
The fitted coefficients calculated by this function may be positive, negative, or zero. In some cases, such as spectral deconvolution, it does not make physical sense to have negative coefficients. (What does it mean to have a sample that is -5% quartz?) For cases where none of the coefficients are allowed to be negative, use lsqnn. For cases where some but not all of the coefficients are allowed to be negative, use lsqsn.
An optional third input (passed by value) controls the output mode. If $3=0, the function will only return the fitted coefficients. If $3 is nonzero, or if it is omitted, the function will display a summary of the fit errors and will return a structure with the fitted coefficients, the fitted spectrum, and the fit errors.
If $3=0, lsq returns a column vector containing the fitted coefficients. Otherwise, it returns a structure with the fields A (fitted coefficients), Yfit (fitted spectrum), and other fields quantifying the errors in the fitted spectrum.
This function returns A such that X*A best approximates y, such that the sum of the squared errors is minimized. The data are fit to y = a1*x1 + a2*x2 + ... + an*xn. Note that no constant offset (e.g. blackbody for emissivity spectra) is automatically included by this function; that must be added elsewhere if needed. Many spectral fitting functions (e.g. sma) have their own option to add the constant term.
If lsq() is entered with no arguments, it prints its description, as shown below.