MATLAB Answers

Estimation of parameters for curvefit using lsqcurvefit

Dursman Mchabe 님이 질문을 제출함. 5 Dec 2018
최근 활동 Alan Weiss 님이 댓글을 추가함. 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
for k1 = 1:length(x)
fprintf(1, '\t\tx(%d) = %8.5f\n', k1, x(k1))
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

  댓글 수: 0

로그인 to comment.


답변 수: 1

Alan Weiss 님의 답변 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;
Instead, you should call ode45 to solve your ODE.
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)
funfcn = lsqfcnchk(FUN,caller,lengthVarargin,funValCheck,flags.grad);
Error in lsqcurvefit (line 201)
lsqnsetup(FUN,xCurrent,LB,UB,options,defaultopt,allDefaultOpts, ...
Error in mytry (line 483)
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

  댓글 수: 4

표시 이전 댓글 수: 1
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
Thanks a lot once more Alan. I have been stuck for more than 7 months on this problem. I completely agree that the inbuilt solvers are accurate and reliable. I tried various approaches with the guidence from mathworks members. Ofcourse, I could have been doing it wrongly. But here are some of the thinks I have tried and help/guidence I have received:
I am truly stuck, but I'll keep on trying.
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.

Translated by