## Estimation of parameters for curvefit using lsqcurvefit

Dursman Mchabe

### Dursman Mchabe (view profile)

님이 질문을 제출함. 5 Dec 2018
최근 활동 Alan Weiss

### Alan Weiss (view profile)

님이 댓글을 추가함. 6 Dec 2018
Hi everyone,
On the attached script I am trying to use lsqcurvefit to estimate parameters x(1), x(2), x(3), x(4), x(5) and x(6). This is done in lines 480 to 488, and given as
x0=[8.825e-6;8.4e-3;2e-8;4.7e-3;8.4e-4;9.598e-4];
[x,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(Results,x0,Exp_Time,...
[Exp_SO2_g;Exp_CO2_g;Exp_S_total;Exp_C_total;Exp_CCaCO3;Exp_CCaSO3;Exp_pH;Exp_pH_Int]);
fprintf(1,'\tParameters:\n')
for k1 = 1:length(x)
fprintf(1, '\t\tx(%d) = %8.5f\n', k1, x(k1))
end
I have read/studied the lsqcurve documentation in
however, the examples and descriptions given there are too simplistic. I would like to ask for help on how best can i implement lsqcurvefit on my script.
The script run with initial values, however calculated values does not fit the experimental values yet.
Kind Regards
Dursman

로그인 to comment.

## 답변 수: 1

Alan Weiss

### Alan Weiss (view profile)

님의 답변 5 Dec 2018

Apparently, you are solving an ODE by forward Euler steps. Don't do that. It is both inaccurate and wasteful of computer time. I refer to these steps in your code:
dydt = odes (t, y0);
% integrate with explicit Euler
y = y0 + dydt' * Delt;
You also have many more global variables than you need, but that is a discussion for another time.
Now for the question you asked. I get an error when running your supplied code:
Error using lsqfcnchk (line 108)
FUN must be a function, a valid character vector expression, or an inline function object.
Error in lsqnsetup (line 46)
Error in lsqcurvefit (line 201)
lsqnsetup(FUN,xCurrent,LB,UB,options,defaultopt,allDefaultOpts, ...
Error in mytry (line 483)
[x,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat]=lsqcurvefit(Results,x0,Exp_Time,...
Specifically, your Results argument is not a function handle. lsqcurvefit needs to be able to change the parameters and rerun your calculations to fit the function to the data. So I am not completely sure what you are trying to do, but you haven't supplied us with working code to check.
Have you seen the example Fit and Ordinary Differential Equation (ODE)? It is intended to guide you through using lsqcurvefit to fit an ODE.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

표시 이전 댓글 수: 1
Alan Weiss

### Alan Weiss (view profile)

6 Dec 2018
I am not 100% sure, but I believe that you can express your problem as a differential-algebraic system of equations. Really, it is best not to use forward Euler steps. Use one of the built-in solvers such as ode15s or ode15i.
To use lsqcurvefit you need to express your results as a function of the input parameters that you are trying to fit. In other words, your 6-D vector x should give you a result that is a (possibly very long) vector of differences between the output of the ODE and the data you are trying to match. In other words, whatever you do, in concept the objective function should look like this:
function resultingdifference = complicatedfunction(x,xdata,otherparameters)
where x is the vector of parameters you are trying to fit, xdata is the data, otherparameters are other problem data that you might want to pass in. The resultingdifference output is a vector predictedresponse - ydata, where ydata is the actual response that you are trying to match.
I hope that this helps orient you.
Alan Weiss
MATLAB mathematical toolbox documentation
Dursman Mchabe

6 Dec 2018
Alan Weiss

### Alan Weiss (view profile)

6 Dec 2018
I see that you have been busy. I will give you what advice I can, and hope that it helps in some way.
I think that you need to take a step back and try to figure out what things look like from a high level. Instead of being buried in code, you have a differential-algebraic system that is parametrized by x. You want to find the x that gives a solution as close as possible to some data.
So the highest-level view of your problem is to go from x to the numerical solution of your system, and compare it with the existing data. Once you can have x as an input and the numerical solution as an output, you are ready to feed the problem to lsqcurvefit.
OK, so the next step is to figure out code that goes from x to a solution. You sort of seem to have that as an Euler-stepping monstrosity. Once again, step back and try to formulate the differential equations as a first-order ODE system and the algebraic equations as an associated set of nonlinear equations in the problem variables. Once you have that, you may need to reduce the order of the algebraic system so that the entire system is first order. You can do this. Really! Once you do this, then you won't need Euler steps, but can use a real solver that will converge much, much more accurately and quickly.
So my suggestion, which you might not like, is to first fix your differential-algebraic equations so that they fall into the domain of ode15s and then write your objective function as a short wrapper around the equation solver. Then feed the whole thing to lsqcurvefit.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

로그인 to comment.