fit nonlinear model to data - better way to code it?
Subject: fit nonlinear model to data - better way to code it?
From: Ether Jones
Date: Thu, 17 May 2012 12:25:10 -0700 (PDT)
I have some XY data and I want to least-squares fit the nonlinear model p1 + p2/(x-p3) to that data.
Pasted below is the way I did it.? It works fine, but I don't like the way I coded it and am wondering if there's a better way.
Specifically, I don't like the p:[p1,p2,p3]$ statement, but it won't work without it.? The p1,p2, and p3 are dummy placeholder variables that are never used in the rest of the code.?? Is there a better way to do this?
-------------------------begin model.mac----------------------------------------
/*
5/16\7/2012 USE MINPACK_LSQUARES TO FIT A MODEL TO DATA
*/
kill(all)$
load("minpack")$
/*
Put the DATA, the MODEL, and the initial GUESS here:
*/
x:[
0,
0.140594059,
0.297029703,
0.437623762,
0.534653465,
0.594059406,
0.64950495,
0.699009901,
0.75049505,
0.776237624,
0.807920792,
0.845544554,
0.865346535,
0.887128713,
0.910891089,
0.932673267,
0.95049505,
0.97029703,
0.98019802,
1
]$
y:[
0.049004594,
0.101071975,
0.15007657,
0.199081164,
0.249617152,
0.298621746,
0.34762634,
0.399693721,
0.447166922,
0.500765697,
0.549770291,
0.600306279,
0.647779479,
0.698315467,
0.747320061,
0.797856049,
0.846860643,
0.898928025,
0.946401225,
0.998468606
]$
p:[p1,p2,p3]$
f(x):=p[1]+p[2]/(x-p[3])$
guess: [-.05,-1,2]$
/*
COMPUTE THE MODEL PARAMETERS AND PLOT THE DATA & MODEL:
*/
ans: minpack_lsquares(
f(x)-y,
p,
guess
);
trendline: ev(f(x),p=ans[1]);
errors: trendline-y;
plot2d([[discrete,x,y], [discrete,x,trendline],[discrete,x,errors]],[legend, "data","model","error"])$
-------------------------------end model.mac-------------------------------------