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

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

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


From: Marc Tfardy
Subject: Re: How to use calc functions in own lisp program?
Date: Tue, 02 Jun 2009 11:01:47 +0200
User-agent: Thunderbird 2.0.0.21 (Windows/20090302)

Jay Belanger schrieb:
>
> Marc Tfardy <no@email.com> writes:
>
>> The polynomial fit ist the first step. Now I must to compute
>> values on this curve at certain points. In Calc it is very easy.
>> I put the value for 'x' on the stack and press 'sl', Calc ask for
>> variable, I type 'x RET' and get a polynom value for my 'x'. But how can
>> I do in my lisp code?
>
> You could use `subst', which takes the form "subst(expr,var,value)".
> (Actually, instead of a variable any expression can be substituted for.)
> So, if you want the best fit curve evaluated at x=2, for example,
> you could use
>  (calc-eval
>      
"simplify(subst(fit(a*x^2+b*x+c,[x],[a,b,c],[[1,2,3,4],[1.1,4.2,9.2,15.8]]),x,2))")
> or even
>   (string-to-number ...above...)

It works! Great! Very big thanks again! :-)

Now I can do:

(setq fit (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]])"))
"0.874999999998 x^2 + 0.53500000001 x - 0.325000000011"

(string-to-number (calc-eval (format "simplify(subst(%s,x,2))" fit)))
4.245

or:

(string-to-number (calc-eval (concat "simplify(subst(" fit ",x,2))")))
4.245

and this exactly what I want!


> (Of course, depending on what else you want Calc to do, it might be more
> efficient to write a lisp program that will do the curve fitting.)

Yes, definitely, but this calculation will be started very occasionally
and computes only some hundreds polynome values. For this task this is
more then enough. Thanks again!

regards
Marc




reply via email to

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