How to run lsqcurvefit with a function handle with 2 array-variables?

For an easy reference I placed a provided Mathworks example below.
xdata = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
fun = @(x,xdata)x(1)*exp(x(2)*xdata);
x0 = [100,-1];
x = lsqcurvefit(fun,x0,xdata,ydata)
I'll start off by mentioning that I am unfamiliar with creating a function handle with an array as a variable as you see with "xdata" which partially leads to my confusion. My issue is that my function has an additional array variable. "k" are my coefficients I'm solving for using lsqcurvefit while "C_TBA_H2O," "C_H2O" are arrays of data points. My x-axis for evaluation is in terms of "C_TBA_H2O." Any clarification or explanation on how to set this up would be greatly appreciated.
rao_H2O = @(k, C_TBA_H2O, C_H2O) k(1)*k(2)*C_TBA_H2O/(1+k(2)*C_TBA_H2O+(k(2)*C_H2O)^gamma(2))+k(3)*C_TBA_H2O;
K = lsqcurvefit(rao_H2O,guess,C_TBA_H2O,rao_norm)
My error:
Error in Lab3_155 (line 17)
K = lsqcurvefit(rao_H2O,guess,C_TBA_H2O,rao_norm)
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.

回答(2 个)

rao_H2O = @(k,x) k(1)*k(2)*C_TBA_H2O./(1+k(2)*C_TBA_H2O+(k(2)*C_H2O).^gamma(2))+k(3)*C_TBA_H2O;
xdata = zeros(size(rao_norm));
ydata = rao_norm;
K = lsqcurvefit(rao_H2O,guess,xdata,ydata);
Even
xdata = [];
might work, but I'm not 100% sure.
Best wishes
Torsten.

2 个评论

No because you overwrote my "xdata" to be array of zeros which is weird.
Since you don't use "xdata" in the definition of "rao_H2O", the array is not necessary for your fitting task.

请先登录,再进行评论。

I figured out my issue. It was the lack of use of periods when multiplying or dividing arrays.

类别

帮助中心File Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by