lsqcurvefit - problem with number of iterations

17 views (last 30 days)
I have experimental data "xdata" and "ydata" and am trying to fit a model simulated "y" (using simulink) to my data -- by using lsqcurvefit.
My problem is simply that my output shows 0 iterations because my initial guess is at a local minima. But when I use a drastically ridiculous initial guess I have 1 iteration and a ridiculous fit. I have played around with initial guesses and am getting the same thing over and over - either 1 iteration or 0.
I would greatly appreciate any advice.
Thanks!
Please see below for my code:
xdata = dataTime(:,1);
ydata = dataTime(:,2);
options = optimset('lsqcurvefit');
options.Algorithm = ('levenberg-marquardt');
options.MaxIter = 1e9;
options.TolFun = 1e-8;
x0 = [6, 13500, 0.00016, 28, 0.6, 1, 1, 0.1,0.02,0.25];
[x,resnorm,residual,exitflag,output] = lsqcurvefit(@myfunc,x0,xdata,ydata,[],[],options);
function F = myfunc(params,xdata)
a1=params(1);
a2=params(2);
b2=params(3);
a3=params(4);
gv1=params(5); gv1s=num2str(gv1); set_param('LNM/Gain1','Gain',gv1s);
gv2=params(6); gv2s=num2str(gv2); set_param('LNM/Gain2','Gain',gv2s);
gv3=params(7); gv3s=num2str(gv3); set_param('LNM/Gain3','Gain',gv3s);
Ta=params(8);
Tv=params(9);
Ti=params(10);
[t, xhat, y] = sim('LNM');
F = y;
end

Accepted Answer

Alan Weiss
Alan Weiss on 8 Oct 2013
You are trying to optimize a simulation. This has its own peculiar difficulties, addressed in the documentation here. For your problem, it is clear you need to increase FinDiffRelStep or DiffMinChange. Other suggestions in the documentation might help, too.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Alexandra
Alexandra on 8 Oct 2013
Edited: Alexandra on 8 Oct 2013
Thank you for your response - it has proved very helpful!

Sign in to comment.

More Answers (1)

A Jenkins
A Jenkins on 7 Oct 2013
1) As a first try, I'd recommend scaling your x0, so they are all similar order of magnitude when passed into lsqcurvefit().
x0 = [6, 13.5, 16, 28, 6, 1, 1, 1, 2, 25];
You can divide back out by the scale factors inside myfunc(), so you pass the right scaled values to your simulink model. The optimizer works by adding "a little bit" to each and re-running to take a deriviative near your point, but with such a large difference between the magnitudes of your numbers, it might not know how much "a little bit" is.
2) Next I'd take a look at:
[x,resnorm,residual,exitflag,output]
The exit flag will attempt to describe why it stopped iterating. Share the results here if you need more help.
3) Also look here for more tips:

Categories

Find more on Manual Performance Optimization in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!