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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to use calc functions in own lisp program?


From: Marc Tfardy
Subject: How to use calc functions in own lisp program?
Date: Fri, 29 May 2009 19:29:38 +0200
User-agent: Thunderbird 2.0.0.21 (Windows/20090302)

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:

(defun calc-curve-fit (arg &optional calc-curve-model
                           calc-curve-coefnames calc-curve-varnames)
...)

(calc-curve-fit is defined .../emacs/lisp/calc/calcalg3.el)

calc-curve-fit works well when called interactively in Calc. Assume, we
have following vector an the top of the stack:

[ [  1,   2,   3,   4   ]
  [ 1.1, 4.2, 9.2, 15.8 ] ]

and we want to compute polynomial fitting of a second degree. In Calc
we press 'aF' and Calc ask for Model ("Fit to model" query) and then for
variable to fitting ("Fitting variables" query). When we enter '2' and
RET (default is 'x') we get as result:

0.874999999996 x^2 + 0.535000000019 x - 0.325000000019


To understand how Calc works I added to calc-curve-fit this four lines:

     (message (format "%S" arg))
     (message (format "%S" calc-curve-model))
     (message (format "%S" calc-curve-coefnames))
     (message (format "%S" calc-curve-varnames))

and the result was:

nil
(+ (+ (* (var c var-c) (^ (var x var-x) 2)) (* (var b var-b) (var x var-x))) 
(var a var-a))
(vec (var a var-a) (var b var-b) (var c var-c))
(vec (var x var-x))

The Calc representation of input data (vector) I got with:
(calc-top 1)

The result was:
(vec (vec 1 2 3 4) (vec (float 11 -1) (float 42 -1) (float 92 -1) (float 158 
-1)))


But, when I try to evaluate following code:

(calc-curve-fit '(vec (vec 1 2 3 4) (vec (float 11 -1) (float 42 -1) (float 92 
-1) (float 158 -1)))
                '(+ (+ (* (var c var-c) (^ (var x var-x) 2)) (* (var b var-b) 
(var x var-x))) (var a var-a))
                '(vec (var a var-a) (var b var-b) (var c var-c))
                '(vec (var x var-x)))

I run in error. The debugger shows:

Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
  signal(wrong-type-argument (number-or-marker-p nil))
  byte-code("...blablabla...
  calc-do((lambda nil (setq calc-aborted-prefix nil) (let (... key ... n calc-curve-nvars temp data 
... ...) (message "----") (message ...) (while ... ... ... ...) (message ...) (message 
...) (message ...) (message ...) (message "----") (let ... ... ...))) 3639)
  (calc-slow-wrapper (setq calc-aborted-prefix nil) (let (... key ... n calc-curve-nvars temp data 
... ...) (message "----") (message ...) (while ... ... ... ...) (message ...) (message 
...) (message ...) (message ...) (message "----") (let ... ... ...)))
  calc-curve-fit((vec (vec 1 2 3 4) (vec (float 11 -1) (float 42 -1) (float 92 
-1) (float 158 -1))) (+ (+ (* ... ...) (* ... ...)) (var a var-a)) (vec (var a 
var-a) (var b var-b) (var c var-c)) (vec (var x var-x)))
  eval((calc-curve-fit (quote (vec ... ...)) (quote (+ ... ...)) (quote (vec 
... ... ...)) (quote (vec ...))))
  eval-last-sexp-1(t)
  eval-last-sexp(t)
  eval-print-last-sexp()
  call-interactively(eval-print-last-sexp)

What is wrong? Please, help me!

regards
Marc



reply via email to

[Prev in Thread] Current Thread [Next in Thread]