MATLAB: Can I use lsqcurvefit for a function of multiple variables

lsqcurvefitmultiple variables

Here is the code I am trying to run:
coeff0 = [1 0.5 2];
svData(:,1) = varStruct(indVar).sv1vals'
svData(:,2) = varStruct(indVar).sv2vals'
zData = varStruct(indVar).linearB'
b = lsqcurvefit(klaCorr,coeff0,svData,zData);
The function klaCorr, which I have in a separate function m-file:
function klaX = klaCorr(coeff,sv)
klaX = coeff(1) + (sv(1)^coeff(2))*(sv(2)^coeff(3));
end
And the output:
svData =
248.8431 1.7500
400.4011 1.7500
400.4011 2.0000
103.7757 1.7500
103.7757 1.7500
103.7757 1.7500
zData =
1.0e-04 *
0.0637
0.1115
0.1272
0.0468
0.0504
0.0321
Not enough input arguments.
Error in klaCorr (line 2)
klaX = coeff(1) + (sv(1)^coeff(2))*(sv(2)^coeff(3));
Error in klascripts (line 609)
b = lsqcurvefit(klaCorr,coeff0,svData,zData);
Does anyone know why I am getting this error message?
Also, if there is a better way to do this with a function other than lsqcurvefit, I am open to suggestions!
Feel free to ask for more details 🙂
Thanks

Best Answer

Your objective function ‘klaCorr’ needs to address the appropriate columns of ‘sv’:
klaCorr = @(coeff,sv) coeff(1) + (sv(:,1).^coeff(2)).*(sv(:,2).^coeff(3));
I used an anonymous function for it here.
The entire code:
svData = [248.8431 1.7500
400.4011 1.7500
400.4011 2.0000
103.7757 1.7500
103.7757 1.7500
103.7757 1.7500];
zData = 1.0e-04 * [0.0637
0.1115
0.1272
0.0468
0.0504
0.0321];
coeff0 = [1 0.5 2];
klaCorr = @(coeff,sv) coeff(1) + (sv(:,1).^coeff(2)).*(sv(:,2).^coeff(3));
b = lsqcurvefit(klaCorr,coeff0,svData,zData)
produces:
b =
-1.00002742240113 4.42835377098216e-06 1.96276255078078e-05