# Pseudo Code for non linear power function fit needed

I am struggling finding pseudo Code for a non-linear fit of the following function:

$y = a\, x^b$

Package NLS in R does perform well, but utilizing external software is not practicable in my program anymore. I would like to avoid this by implementing a short class by myself. Literature links including the solution are also welcome.

Update: I already have implemented the log transform with linear square fit. That is not the solution I am looking for. I need to implement NLS fit.

Update 2, giving reason why the log transformed method is not sufficient:

My original data: The log transformed data with a linear model line in red, one can see that the log transformed residuals are a heavy influence: The original data again, the red line is the power function with parameters derived from the log transformed method, the blue line is the power function predicted with package NLS2 in R: Clearly the log-transformed residuals have too much influence, the NLS method forces the prediction on the stable part of the model.

• Does this help? mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html – Christian Clason Apr 26 '16 at 16:08
• One can simply do this by first performing the log of your function, resulting in: $log(y) = log(a) + b*log(x)$. Using this representation, the problem becomes a Linear Least Square to find $log(a)$ and $b$. Obviously then you can obtain $a$ alone. – spektr Apr 26 '16 at 18:50
• @choward yes I am absolutely aware of this technique and have it already implemented. But I need a NLS fit as I do not want to minimize log transformed residuals... – Jan Hackenberg Apr 26 '16 at 22:24
• Question: what in your mind is the problem with minimizing the log transformed residuals? Do you think some finite precision issue by computing a bunch of logs will make the result less accurate? – spektr Apr 29 '16 at 15:38
• @choward That is exactly the point, added three R plots to my question which should clearify. Gauss Newton prediction seems to solve efficiently the problem in my C++ code. – Jan Hackenberg Apr 29 '16 at 17:44

## 1 Answer

Chapter 10 of Numerical Analysis (Richard L. Burden, J. Douglas Faires) gives good readable pseudo code for Newtons method. The start parameters are taken from the solution of the linear problem of the log transformed data. (see comment of Christian Clason giving a link (Linear Solution, Mathworld Wolfram) )

while not reached convergence criteria
calucate vector of residuals F(x) and Jacobian matrix J(x)
solve J(x)y = -F(x)
set x = x + y


Here $x = a,b$

$-F(x)$ is for $n$ points a vector with length $n$:

the $ith$ element is $- a\, x_i^b + y_i$

and the Jacobian $J(x)$ is a $n,2$ Matrix with the $ith$ row:

$J_{i,1} = x_i^b$

and

$J_{i,2} = a\,log(x_i)\,x_i^b$