Help with least squares on non-linear function [message #85657] |
Sat, 24 August 2013 12:14  |
stefan.meingast
Messages: 30 Registered: June 2012
|
Member |
|
|
Hey
I finally want to enjoy at least some time of the weekend, but I am stuck at one problem tonight for which I still would like to find a solution.
I have a bunch of measurements in an array y which depending basically on another variable x. I want to do a least-squares fit to a non-linear function, which would be very easy if I just wanted e.g. a polynomial fit. My problem is now that there is another parameter coming into play (here: k), so the function I want to fit in the end looks like this (though the order should be variable in the end which in this example is set to 3):
https://dl.dropboxusercontent.com/u/16787607/formula.jpg
where the a_i are the coefficients I want to determine.
I have been trying to figure out what the best way might be to do such a fit, but this turns out to be much more complicated than I thought...
I hope some of you may have a hint
many thanks!!!
:)
|
|
|
|
|
|
|
Re: Help with least squares on non-linear function [message #85664 is a reply to message #85661] |
Sun, 25 August 2013 05:40  |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
On Sat, 24 Aug 2013 14:53:39 -0700 (PDT), Phillip Bitzer wrote:
> Yep, that was the next piece of advice :-) MPFIT is highly recommended....
I would say, this is a linear function. Here is a way how to calculate
the fit parameters.
Put your x, k and y values into the arrays
x=dblarr(npoints)
k=dblarr(npoints)
y=dblarr(npoints)
With npars=3, create the matrix
fx=dblarr(npoints,npars)
for i=1,npars do fx[0,i-1]=((1+k)^i-k^i)*x^i
and do the calculation
temp=transpose(fx)
a=temp#fx
b=temp#y
ludc,a,indx
par=lusol(a,indx,b)
par is an array with npars elements and should contain the fit
parameters named by a_i within your function.
I hope, that there is no error in this code. I couldn't test it,
because I have no example values for x, k and y.
Cheers, Heinz
|
|
|