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.

Antworten (2)

Torsten
Torsten am 7 Mär. 2019
Bearbeitet: Torsten am 7 Mär. 2019

0 Stimmen

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 Kommentare

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.

Melden Sie sich an, um zu kommentieren.

Robert Vigil
Robert Vigil am 8 Mär. 2019

0 Stimmen

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

Kategorien

Mehr zu Function Creation finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 7 Mär. 2019

Kommentiert:

am 11 Mär. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by