# Failed attempts at fitting nonlinear Hill function (biochemistry) to data

I am trying to fit some data in Matlab to a Hill function of the form $y = \dfrac{1}{1+(K/r)^n}.$ I have data for $r,y$ and I need to find $K,n$.

I have tried following the approach shown here in Demo $3$ at this link: http://www.math.ubc.ca/~keshet/MCB2012/SlidesDodo/DataFitLect3.pdf

but I'm getting wrong results. Please help!

Here is my code:

Function file:

function response = func(x,dose)
EC50 = x(1);
n = x(2);
response = 1./(1+(EC50./dose).^n);
end


Script file:

xdata = (logspace(-2,2,101))';
ydata = [0.0981 0.1074 0.1177 0.1289 0.1411 0.1545 0.1692 0.1852 0.2027 ...
0.2219 0.2428 0.2656 0.2905 0.3176 0.3472 0.3795 0.4146 0.4528 ...
0.4944 0.5395 0.5886 0.6418 0.6994 0.7618 0.8293 0.9022 0.9808 ...
1.0655 1.1566 1.2544 1.3592 1.4713 1.5909 1.7183 1.8537 1.9972 ...
2.1490 2.3089 2.4770 2.6532 2.8371 3.0286 3.2272 3.4324 3.6437 ...
3.8603 4.0815 4.3065 4.5344 4.7642 4.9950 5.2258 5.4556 5.6833 ...
5.9082 6.1292 6.3457 6.5567 6.7616 6.9599 7.1511 7.3347 7.5105 ...
7.6783 7.8379 7.9893 8.1324 8.2675 8.3946 8.5139 8.6257 8.7301 ...
8.8276 8.9184 9.0029 9.0812 9.1539 9.2212 9.2834 9.3408 9.3939 ...
9.4427 9.4877 9.5291 9.5672 9.6022 9.6343 9.6638 9.6909 9.7157 ...
9.7384 9.7592 9.7783 9.7957 9.8117 9.8263 9.8397 9.8519 9.8630 ...
9.8732 9.8826]';
guess = [1 1];
betaHat = nlinfit(xdata,ydata,@func,guess);
semilogx(xdata,ydata,'ro','MarkerSize',8)
hold on
plot(xdata,func(betaHat,xdata));


I am getting the Warning: Imaginary parts of complex X and/or Y arguments ignored

and here is my abysmal graph: Red = data, blue = bad fit

I have used nlinfit from the Statistics toolbox.

• Can you figure out which line of your program triggers the warning (by just running each line one after the other interactively)? You are probably doing something simple wrong. – Ian Feb 3 '16 at 22:30
• Ok, I will try running it in debug mode. – user289771 Feb 3 '16 at 22:32
• Ah, I see the problem now: mathworks.com/matlabcentral/answers/… So instead of using the function $f(r;K,n)=\frac{1}{1+(K/r)^n}$, try the function $g(r;a,n)=\frac{1}{1+a/r^n}$. Then at the end you can recompute $K$ from $a$ and $n$. – Ian Feb 3 '16 at 22:42
• It's not exactly that your math was wrong, it's that you needed the assumption that $K$ remains nonnegative throughout the computation. But nlinfit does not guarantee this, which created the problems that you saw here. You can bypass this by switching to a parametrization of the problem in which no constraints are necessary, like the one I suggested. – Ian Feb 3 '16 at 22:49
• Actually, I think you have a problem in your setup: in the Hill equation, the $y$ values are between $0$ and $1$, but your $y$ data is not between $0$ and $1$. The only way for the $y$ data to not be between $0$ and $1$ is to have $K<0$. nlinfit figured that out, but also was trying to use floating point values of $n$, which is where the complex numbers came from. Is it possible you need another parameter, such as a factor in the numerator? Or is it possible that your data is wrong? – Ian Feb 3 '16 at 22:56

## 1 Answer

Try transforming your equation to a different form? Often fitting can go wrong because of errors piling up in working with very small/large numbers. Sometimes, it can be fixed by transforming the equation to a different form.

I would suggest going in for fitting the logarithmic form of this equation, i.e. getting rid of the exponent. This is a pretty common practice in biochem.

the new form would be -

$log(y/1-y)=nlog(r)-nlog(k)$