Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Optimization parameters and data fitting to a simulink output using lsqcurvefit

Subject: Optimization parameters and data fitting to a simulink output using lsqcurvefit

From: Jan

Date: 25 Jun, 2012 16:38:07

Message: 1 of 3

Hello everyone, I'm trying to fit a a vector of experimental data (dataLAC) to an output of my simulink model (nadh.mdl, output LAC is sent to workspace) and through this to find a set of parameters kPL, kLP, Cu that will provide the best match.

Besides the simulink model I have following code to optimize parameters and fit the data:

function [kPL kLP Cu] = opt2
global LAC time
data = xlsread('data.xls');
dataLAC = data(:,3);
nadh %load the model
par0 = [1 1 5]; %a guess of initial values for parameters
lb = [0 0 0];
ub = [1 1 1].*1e3;
options = optimset('MaxIter',1e10,'MaxFunEvals',1e10,'TolFun',1e-6,...
    'TolX',1e-6,'DiffMinChange',10);
[par,resnorm] = lsqcurvefit(@runmodel, par0, dataLAC, LAC, lb, ub, options);
          function F = runmodel(par,dataLAC)
              kPL = par(1); kLP = par(2); Cu = par(3);
              [time,LAC] = sim('nadh',[3 6 9 12 15 18 21 24 27]);
              F=LAC;
           end %runmodel
  end %opt2
Matlab returns these errors:

??? Error using ==> optim\private\lsqncommon at 98 LSQCURVEFIT cannot continue because user supplied objective function failed with the following error: Error using ==> sim --> Error writing variable 'LAC' to workspace: Attempt to add "LAC" to a static workspace. See M_ATLAB Programming, Restrictions on Assigning to Variables for details. --> Error writing variable 'time' to workspace: Attempt to add "time" to a static workspace. See MATLAB Programming, Restrictions on Assigning to Variables for details.

Error in ==> lsqcurvefit at 182 [x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...

Error in ==> opt2 at 16 [par,resnorm] = lsqcurvefit(@runmodel, par0, dataLAC, LAC, lb, ub, options); ______________________________________________________________________

I tried to go through the Restrictions on Assigning to Variables but I don't get it. Can anyone help me to fix it?

Thank you in advance.

Regards, Jan

Subject: Optimization parameters and data fitting to a simulink output using lsqcurvefit

From: Steve Grikschat

Date: 27 Jun, 2012 13:52:07

Message: 2 of 3

"Jan" wrote in message <jsa45f$bs1$1@newscl01ah.mathworks.com>...
> Hello everyone, I'm trying to fit a a vector of experimental data (dataLAC) to an output of my simulink model (nadh.mdl, output LAC is sent to workspace) and through this to find a set of parameters kPL, kLP, Cu that will provide the best match.
>
> Besides the simulink model I have following code to optimize parameters and fit the data:
>
> function [kPL kLP Cu] = opt2
> global LAC time
> data = xlsread('data.xls');
> dataLAC = data(:,3);
> nadh %load the model
> par0 = [1 1 5]; %a guess of initial values for parameters
> lb = [0 0 0];
> ub = [1 1 1].*1e3;
> options = optimset('MaxIter',1e10,'MaxFunEvals',1e10,'TolFun',1e-6,...
> 'TolX',1e-6,'DiffMinChange',10);
> [par,resnorm] = lsqcurvefit(@runmodel, par0, dataLAC, LAC, lb, ub, options);
> function F = runmodel(par,dataLAC)
> kPL = par(1); kLP = par(2); Cu = par(3);
> [time,LAC] = sim('nadh',[3 6 9 12 15 18 21 24 27]);
> F=LAC;
> end %runmodel
> end %opt2
> Matlab returns these errors:
>
> ??? Error using ==> optim\private\lsqncommon at 98 LSQCURVEFIT cannot continue because user supplied objective function failed with the following error: Error using ==> sim --> Error writing variable 'LAC' to workspace: Attempt to add "LAC" to a static workspace. See M_ATLAB Programming, Restrictions on Assigning to Variables for details. --> Error writing variable 'time' to workspace: Attempt to add "time" to a static workspace. See MATLAB Programming, Restrictions on Assigning to Variables for details.
>
> Error in ==> lsqcurvefit at 182 [x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
>
> Error in ==> opt2 at 16 [par,resnorm] = lsqcurvefit(@runmodel, par0, dataLAC, LAC, lb, ub, options); ______________________________________________________________________
>
> I tried to go through the Restrictions on Assigning to Variables but I don't get it. Can anyone help me to fix it?
>
> Thank you in advance.
>
> Regards, Jan

Hi Jan,

I can't run this, so this is just a guess, but I believe the conflict is with "LAC" and "time" being global and being used in a nested function. I'd strongly recommend NOT using them as global, if possible.

Just to debug, try commenting the global line and see if it works.

Subject: Optimization parameters and data fitting to a simulink output using lsqcurvefit

From: Jan

Date: 27 Jun, 2012 14:24:07

Message: 3 of 3

"Steve Grikschat" wrote in message <jsf367$cnd$1@newscl01ah.mathworks.com>...
> "Jan" wrote in message <jsa45f$bs1$1@newscl01ah.mathworks.com>...
> > Hello everyone, I'm trying to fit a a vector of experimental data (dataLAC) to an output of my simulink model (nadh.mdl, output LAC is sent to workspace) and through this to find a set of parameters kPL, kLP, Cu that will provide the best match.
> >
> > Besides the simulink model I have following code to optimize parameters and fit the data:
> >
> > function [kPL kLP Cu] = opt2
> > global LAC time
> > data = xlsread('data.xls');
> > dataLAC = data(:,3);
> > nadh %load the model
> > par0 = [1 1 5]; %a guess of initial values for parameters
> > lb = [0 0 0];
> > ub = [1 1 1].*1e3;
> > options = optimset('MaxIter',1e10,'MaxFunEvals',1e10,'TolFun',1e-6,...
> > 'TolX',1e-6,'DiffMinChange',10);
> > [par,resnorm] = lsqcurvefit(@runmodel, par0, dataLAC, LAC, lb, ub, options);
> > function F = runmodel(par,dataLAC)
> > kPL = par(1); kLP = par(2); Cu = par(3);
> > [time,LAC] = sim('nadh',[3 6 9 12 15 18 21 24 27]);
> > F=LAC;
> > end %runmodel
> > end %opt2
> > Matlab returns these errors:
> >
> > ??? Error using ==> optim\private\lsqncommon at 98 LSQCURVEFIT cannot continue because user supplied objective function failed with the following error: Error using ==> sim --> Error writing variable 'LAC' to workspace: Attempt to add "LAC" to a static workspace. See M_ATLAB Programming, Restrictions on Assigning to Variables for details. --> Error writing variable 'time' to workspace: Attempt to add "time" to a static workspace. See MATLAB Programming, Restrictions on Assigning to Variables for details.
> >
> > Error in ==> lsqcurvefit at 182 [x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
> >
> > Error in ==> opt2 at 16 [par,resnorm] = lsqcurvefit(@runmodel, par0, dataLAC, LAC, lb, ub, options); ______________________________________________________________________
> >
> > I tried to go through the Restrictions on Assigning to Variables but I don't get it. Can anyone help me to fix it?
> >
> > Thank you in advance.
> >
> > Regards, Jan
>
> Hi Jan,
>
> I can't run this, so this is just a guess, but I believe the conflict is with "LAC" and "time" being global and being used in a nested function. I'd strongly recommend NOT using them as global, if possible.
>
> Just to debug, try commenting the global line and see if it works.

Hello Steve,
thanks for your respond. You're right, using those global variables was the mistake. Anyway I still get error such as

Error in ==> lsqcurvefit at 182
[x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...

or

Error in ==> opt at 16
[par,resnorm] = lsqcurvefit(@runmodel, par0, dataLAC, LAC, lb, ub, options);
____________________________
I'm not sure whether I use lsqcurvefit arguments correctly. Is it right that xdata (in my code dataLAC) represents an experimental set of data (e.g.temperature) while ydata (in my code LAC) represents simulated set of data? Or is ydata supposed to be values of time corresponding with xdata values?

Thanks for clearing this up!
Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us