calc: Standard Nonlinear Models

 
 11.8.4 Standard Nonlinear Models
 --------------------------------
 
 The ‘a F’ command also accepts other kinds of models besides lines and
 polynomials.  Some common models have quick single-key abbreviations;
 others must be entered by hand as algebraic formulas.
 
    Here is a complete list of the standard models recognized by ‘a F’:
 
 ‘1’
      Linear or multilinear.  a + b x + c y + d z.
 ‘2-9’
      Polynomials.  a + b x + c x^2 + d x^3.
 ‘e’
      Exponential.  a exp(b x) exp(c y).
 ‘E’
      Base-10 exponential.  a 10^(b x) 10^(c y).
 ‘x’
      Exponential (alternate notation).  exp(a + b x + c y).
 ‘X’
      Base-10 exponential (alternate).  10^(a + b x + c y).
 ‘l’
      Logarithmic.  a + b ln(x) + c ln(y).
 ‘L’
      Base-10 logarithmic.  a + b log10(x) + c log10(y).
 ‘^’
      General exponential.  a b^x c^y.
 ‘p’
      Power law.  a x^b y^c.
 ‘q’
      Quadratic.  a + b (x-c)^2 + d (x-e)^2.
 ‘g’
      Gaussian.  (a / b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2).
 ‘s’
      Logistic _s_ curve.  a/(1 + exp(b (x - c))).
 ‘b’
      Logistic bell curve.  a exp(b (x - c))/(1 + exp(b (x - c)))^2.
 ‘o’
      Hubbert linearization.  (y/x) = a (1 - x/b).
 
    All of these models are used in the usual way; just press the
 appropriate letter at the model prompt, and choose variable names if you
 wish.  The result will be a formula as shown in the above table, with
 the best-fit values of the parameters substituted.  (You may find it
 easier to read the parameter values from the vector that is placed in
 the trail.)
 
    All models except Gaussian, logistics, Hubbert and polynomials can
 generalize as shown to any number of independent variables.  Also, all
 the built-in models except for the logistic and Hubbert curves have an
 additive or multiplicative parameter shown as ‘a’ in the above table
 which can be replaced by zero or one, as appropriate, by typing ‘h’
 before the model key.
 
    Note that many of these models are essentially equivalent, but
 express the parameters slightly differently.  For example, ‘a b^x’ and
 the other two exponential models are all algebraic rearrangements of
 each other.  Also, the “quadratic” model is just a degree-2 polynomial
 with the parameters expressed differently.  Use whichever form best
 matches the problem.
 
    The HP-28/48 calculators support four different models for curve
 fitting, called ‘LIN’, ‘LOG’, ‘EXP’, and ‘PWR’.  These correspond to
 Calc models ‘a + b x’, ‘a + b ln(x)’, ‘a exp(b x)’, and ‘a x^b’,
 respectively.  In each case, ‘a’ is what the HP-48 identifies as the
 “intercept,” and ‘b’ is what it calls the “slope.”
 
    If the model you want doesn’t appear on this list, press ‘'’ (the
 apostrophe key) at the model prompt to enter any algebraic formula, such
 as ‘m x - b’, as the model.  (Not all models will work, though—see the
 next section for details.)
 
    The model can also be an equation like ‘y = m x + b’.  In this case,
 Calc thinks of all the rows of the data matrix on equal terms; this
 model effectively has two parameters (‘m’ and ‘b’) and two independent
 variables (‘x’ and ‘y’), with no “dependent” variables.  Model equations
 do not need to take this ‘y =’ form.  For example, the implicit line
 equation ‘a x + b y = 1’ works fine as a model.
 
    When you enter a model, Calc makes an alphabetical list of all the
 variables that appear in the model.  These are used for the default
 parameters, independent variables, and dependent variable (in that
 order).  If you enter a plain formula (not an equation), Calc assumes
 the dependent variable does not appear in the formula and thus does not
 need a name.
 
    For example, if the model formula has the variables ‘a,mu,sigma,t,x’,
 and the data matrix has three rows (meaning two independent variables),
 Calc will use ‘a,mu,sigma’ as the default parameters, and the data rows
 will be named ‘t’ and ‘x’, respectively.  If you enter an equation
 instead of a plain formula, Calc will use ‘a,mu’ as the parameters, and
 ‘sigma,t,x’ as the three independent variables.
 
    You can, of course, override these choices by entering something
 different at the prompt.  If you leave some variables out of the list,
 those variables must have stored values and those stored values will be
 used as constants in the model.  (Stored values for the parameters and
 independent variables are ignored by the ‘a F’ command.)  If you list
 only independent variables, all the remaining variables in the model
 formula will become parameters.
 
    If there are ‘$’ signs in the model you type, they will stand for
 parameters and all other variables (in alphabetical order) will be
 independent.  Use ‘$’ for one parameter, ‘$$’ for another, and so on.
 Thus ‘$ x + $$’ is another way to describe a linear model.
 
    If you type a ‘$’ instead of ‘'’ at the model prompt itself, Calc
 will take the model formula from the stack.  (The data must then appear
 at the second stack level.)  The same conventions are used to choose
 which variables in the formula are independent by default and which are
 parameters.
 
    Models taken from the stack can also be expressed as vectors of two
 or three elements, ‘[MODEL, VARS]’ or ‘[MODEL, VARS, PARAMS]’.  Each of
 VARS and PARAMS may be either a variable or a vector of variables.  (If
 PARAMS is omitted, all variables in MODEL except those listed as VARS
 are parameters.)
 
    When you enter a model manually with ‘'’, Calc puts a 3-vector
 describing the model in the trail so you can get it back if you wish.
 
    Finally, you can store a model in one of the Calc variables ‘Model1’
 or ‘Model2’, then use this model by typing ‘a F u’ or ‘a F U’
 (respectively).  The value stored in the variable can be any of the
 formats that ‘a F $’ would accept for a model on the stack.
 
    Calc uses the principal values of inverse functions like ‘ln’ and
 ‘arcsin’ when doing fits.  For example, when you enter the model ‘y =
 sin(a t + b)’ Calc actually uses the easier form ‘arcsin(y) = a t + b’.
 The ‘arcsin’ function always returns results in the range from -90 to 90
 degrees (or the equivalent range in radians).  Suppose you had data that
 you believed to represent roughly three oscillations of a sine wave, so
 that the argument of the sine might go from zero to 3*360 degrees.  The
 above model would appear to be a good way to determine the true
 frequency and phase of the sine wave, but in practice it would fail
 utterly.  The righthand side of the actual model ‘arcsin(y) = a t + b’
 will grow smoothly with ‘t’, but the lefthand side will bounce back and
 forth between -90 and 90.  No values of ‘a’ and ‘b’ can make the two
 sides match, even approximately.
 
    There is no good solution to this problem at present.  You could
 restrict your data to small enough ranges so that the above problem
 doesn’t occur (i.e., not straddling any peaks in the sine wave).  Or, in
 this case, you could use a totally different method such as Fourier
 analysis, which is beyond the scope of the ‘a F’ command.
 (Unfortunately, Calc does not currently have any facilities for taking
 Fourier and related transforms.)