fit nonlinear model to data - better way to code it?




The following works:

[p1,p2,p3]: ans[1];
trendline: ''f(x);

... but the cure is worse than the disease:

With that approach, I have to change the code in 4 separate places to add parameters to the model:

1) f(x):=p1+p2/(x-p3)$
2) guess: [-.05,-1,2]$

3) ans: minpack_lsquares(
f(x)-y,
[p1,p2,p3],
guess)$


4) [p1,p2,p3]: ans[1];



... whereas in the original code I had to change it in only 3 places,
all of which were contiguous:

1) p:[p1,p2,p3]$
2) f(x):=p[1]+p[2]/(x-p[3])$
3) guess:
 [-.05,-1,2]$



What I'm looking for is a "cleaner" way to "declare" p as a 3-element list of variables; using three dummy variables seems awkward, especially as the number of parameters increases.




________________________________
 From: Jaime Villate <villate at fe.up.pt>
To: maxima at math.utexas.edu; rswarbrick at gmail.com 
Sent: Friday, May 18, 2012 8:03 AM
Subject: Re: [Maxima] fit nonlinear model to data - better way to code it?
 
On 05/18/2012 09:24 AM, Rupert Swarbrick wrote:
>
> Hmm, I was going to suggest that
>
>? ? ev(f(x), [p1,p2,p3] = ans[1])
>
> should work, but surprisingly it doesn't. For example:
>
>? ? (%i14) p1, [p1,p2,p3]=[3,4,5];
>? ? (%o14)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? p1
>
> Does anyone know if there is a reason for this? It seems at odds with
>
>? ? (%i15) [p1,p2,p3]: ans[1];
>? ? (%o15)?  [- .2694298415835076, - .4248176441471781, 1.331747718996615]
>? ? (%i16) p1;
>? ? (%o16)? ? ? ? ? ? ? ? ? ? ? ? - .2694298415835076
>
If I remember correctly, parallel substitution

[p1,p2,p3]: ans[1]

was added a few years ago by Robert, but
 function ev() has not been modified
to accept a similar syntax:

p1, [p1,p2,p3]=[3,4,5];

You still have to use the syntax:

?  p1, [p1=3, p2=4, p=5];

and the same goes for subst:

?  subst([p1=3, p2=4, p=5], p1);

rather than

?  subst([p1,p2,p3]=[3,4,5],p1);

which is not implemented yet.

Regards,
Jaime

_______________________________________________
Maxima mailing list
Maxima at math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima