# Lsq2topb

## Contents

### Purpose

Fits a polynomial to the top/(bottom) of data.

### Synopsis

[yi,resnorm,residual,options] = lsq2topb(x,y,order,res,options)

### Description

For order=1 and fitting to top of data cloud, LSQ2TOPB finds yi that minimizes sum( (W\*( y - yi )).\^2 ) where W is a diagonal weighting matrix given by:

```>> tsq = residual/res; % (res) is an input

>> tsqst = ttestp(1-options.tsqlim,5000,2); % T-test limit from table

>> ii = find(tsq<-tsqst); % finds residuals below the line

>> w(ii) = 1./(0.5+tsq(ii)/tsqst); %de-weights pts significantly below line```

i.e. w(ii) is smaller for residuals far below/(above) the fit line.

### Inputs

• x = independent variable Mx1 vector.
• y = dependent variable, Mx1 vector.
• order = order of polynomial [scalar] for polynomial function of input (x). If (order) is empty, (options.p) must contain a MxK matrix of basis vectors to fit in lieu of polynomials of (x).
• res = approximate fit residual [scalar].

### OPTIONAL INPUTS

• k = number of components {default = rank of X-block}.

### Outputs

• yi = the fit to input (y).
• resnorm = squared 2-norm of the residual.
• residual = y - yi.

### Options

• p: [ ] If (options.p) is empty, input (order) must be >0. Otherwise, options.p is a MxK matrix of basis vectors.
• smooth: [ ] if >0 this adds smoothing by adding a penalty to the magnitude of the 2nd derivative. (empty or <=0 means no smooth).
• display: [ 'off' | {'on'} ] governs level of display to command window.
• trbflag: [{'top'} | 'bottom' | 'middle'] flag that tells algorithm to fit (yi) to the top, bottom, or middle of the data cloud.
• tsqlim: [0.99] limit that govers whether a data point is outside the fit residual defined by input (res).
• stopcrit: [1e-4 1e-4 1000 360] stopping criteria, iteration is continued until one of the stopping criterion is met [(rel tol) (abs tol) (max \# iterations) (max time [seconds])].
• initwt: [ ] empty or Mx1 vector of initial weights (0<=w<=1).