help-gnu-emacs
[Top][All Lists]

## Re: How to use calc functions in own lisp program?

 From: Jay Belanger Subject: Re: How to use calc functions in own lisp program? Date: Fri, 29 May 2009 20:34:07 -0500 User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.94 (gnu/linux)

```

> I want to compute the polynomial fitting in my own lisp program. The
> Emacs Calc offers this functionality so my first thought was to use this
> code. But I don't understand the call arguments to calc-curve-fit:

calc-curve-fit is designed to be used as an interactive Calc function,
so it assumes it is being used from the Calc buffer and interacts with
the stack, hence all your error messages.
If you want to use a Calc function from outside of Calc, you'll need to
use the algebraic form of the function; in this case `fit', which is Calc
shorthand for `calcFunc-fit'.  `fit' takes as arguments the curve
model, the variables in the curve model, the coefficients and the data.
(The Calc manual will tell the possibilities for these.)
The Calc functions will assume that their input is in internal Calc
form.  The function `math-read-expr' will take a string and put it in
internal Calc form.  But you probably want to use `calc-eval', which
will take as an argument an algebraic expression (as a string) that you
could use in a Calc session and return the string that represents the
Calc output. For your problem, in Calc you could enter
fit(a*x^2+b*x+c,x,[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]])
and get your result, so outside of Calc you can evaluate

(calc-eval
"fit(a*x^2+b*x+c,[x],[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]])")

Evaluating the above gives me

"0.874999999998 x^2 + 0.53500000001 x - 0.325000000011"

```