MATLAB Answers

How to use lsqcurvefit to estimate parameters while using fsolve to solve an equation

Hi ebveryone,
On the attached code I am trying to use lsqcurvefit to estimate the following constants:
% Constants
KSO2 = Constant(1);
KHSO3 = Constant(2);
KCO2 = Constant(3);
KHCO3 = Constant(4);
Kw = Constant(5);
whilst using fsolve to solve an equation. Some I get an error message:
Error using pHCalcFeb25>pHCalculation
Too many output arguments.
Error in lsqcurvefit (line 213)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in pHCalcFeb25 (line 67)
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
How can I resolve this error. Please help

  댓글 수: 0

로그인 to comment.


답변 수: 1

Walter Roberson 님의 답변 25 Feb 2019
 채택된 답변

Your function calculates ph as a vector of 31 values. However, your function is declared as if it has no return values at all. At the very least you need to change your function declaration to return ph .
It was easier to debug by splitting your function into two files, which I attach.

  댓글 수: 7

표시 이전 댓글 수: 4
So sorry, I already had the files @pHCalculation and @pHCalcFeb25 in the folder, and when you sent those two m-files saved them as pHCalculation1 and pHCalc1Feb25, just to quickly test them.
Apologies, I have now moved them to a different folder, and saved them as pHCalculation and pHCalcFeb25.
Sorry for causing confusion.
Thank you , thank you , thank you. It works perfectly.

로그인 to comment.

Translated by