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개)

Torsten
Torsten 2019년 3월 7일
편집: Torsten 2019년 3월 7일

0 개 추천

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.

댓글을 달려면 로그인하십시오.

Robert Vigil
Robert Vigil 2019년 3월 8일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Function Creation에 대해 자세히 알아보기

질문:

2019년 3월 7일

댓글:

2019년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by