Hello,
I have been using lsqcurvefit for the last weeks. Till now, I did my data fitting for a known function. But right now, I want to step forward and instead of using a known equation, I want to use a Simulink model.
Why do I want to do it?
- It's a too complex model for obtaining a equation (It's not a LTI model...).
- Versatility
- ...
Has someone succeed using x = lsqcurvefit(fun,x0,xdata,ydata) where 'fun' is a Simulink model?
Thanks in advance,
John
|
|
0
|
|
|
|
Reply
|
John
|
9/23/2010 8:51:21 PM |
|
Have you had a look in the Optimization Toolbox documentation?
There are several examples in it of using a Simulink model as part of a cost function.
Phil.
|
|
0
|
|
|
|
Reply
|
Phil
|
9/23/2010 11:29:09 PM
|
|
First of all thank you Phil.
I am getting close, but I am still having problems.
Let' me go more specific.
I have 4 files:
- findingCurve.m is the main M-file.
- Model.m is the M-file that should run the model.
- ModelSimulink.mdl is the Simulink Model.
- RealData.mat is the Data we are trying to approach.
-------------------------------------------------------------------------------------------------------------
Code findingCurve.m:
ModelSimulink
load RealData.mat
t = RealData(1,:); % Input data
y = RealData(2,:); % Input data
X0=[1 2 3 4]; % Initial approach. x=[11 3 7 15] are the good ones
LB=[0 0 0 0];
UB=[1 1 1 1].*1e3;
options = optimset('MaxIter',1e10,'MaxFunEvals',1e10,'TolFun',1e-6,'TolX',1e-6,'DiffMinChange',10);
[x,resnorm] = lsqcurvefit('Model', X0, t, y,LB,UB,options)
-------------------------------------------------------------------------------------------------------------
Code Model.m
function F =Model(X0)
opt = simset('solver','ode45');
[yout] = sim('ModelSimulink',[0 0.5],opt);
F = yout;
-------------------------------------------------------------------------------------------------------------
Matlab doesn't like something:
??? Error using ==> Model
Too many input arguments.
Error in ==> lsqcurvefit at 209
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in ==> findingCurve at 9
[x,resnorm] = lsqcurvefit('Model', X0, t, y,LB,UB,options)
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT
cannot continue.
--------------------------------------------------------------------------------------------------------------
Any idea how to make it run?
Thanks in advance,
John
|
|
0
|
|
|
|
Reply
|
John
|
9/26/2010 3:36:04 PM
|
|
"John " <hiramik@hotmail.com> wrote in message
news:i7np94$h65$1@fred.mathworks.com...
> First of all thank you Phil.
>
> I am getting close, but I am still having problems.
>
> Let' me go more specific.
>
> I have 4 files:
> - findingCurve.m is the main M-file.
> - Model.m is the M-file that should run the model.
> - ModelSimulink.mdl is the Simulink Model.
> - RealData.mat is the Data we are trying to approach.
>
> -------------------------------------------------------------------------------------------------------------
> Code findingCurve.m:
>
> ModelSimulink
> load RealData.mat
> t = RealData(1,:); % Input data
> y = RealData(2,:); % Input data
> X0=[1 2 3 4]; % Initial approach. x=[11 3 7 15] are the good ones
> LB=[0 0 0 0];
> UB=[1 1 1 1].*1e3;
> options =
> optimset('MaxIter',1e10,'MaxFunEvals',1e10,'TolFun',1e-6,'TolX',1e-6,'DiffMinChange',10);
> [x,resnorm] = lsqcurvefit('Model', X0, t, y,LB,UB,options)
Don't pass the name of the function in. Pass it in as a function handle.
> -------------------------------------------------------------------------------------------------------------
>
> Code Model.m
>
> function F =Model(X0)
> opt = simset('solver','ode45');
> [yout] = sim('ModelSimulink',[0 0.5],opt);
> F = yout;
> -------------------------------------------------------------------------------------------------------------
>
> Matlab doesn't like something:
>
> ??? Error using ==> Model
> Too many input arguments.
What does LSQCURVEFIT say it expects the function that you pass into it to
accept as input arguments?
http://www.mathworks.com/help/toolbox/optim/ug/lsqcurvefit.html
What does your function accept as input arguments? In particular, how many
inputs does LSQCURVEFIT pass into your function and how many does your
function actually accept?
--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
|
|
0
|
|
|
|
Reply
|
slord (13285)
|
9/27/2010 2:58:31 AM
|
|
"Steven_Lord" <slord@mathworks.com> wrote in message <i7p18n$fub$1@fred.mathworks.com>...
>
>
> "John " <hiramik@hotmail.com> wrote in message
> news:i7np94$h65$1@fred.mathworks.com...
> > First of all thank you Phil.
> >
> > I am getting close, but I am still having problems.
> >
> > Let' me go more specific.
> >
> > I have 4 files:
> > - findingCurve.m is the main M-file.
> > - Model.m is the M-file that should run the model.
> > - ModelSimulink.mdl is the Simulink Model.
> > - RealData.mat is the Data we are trying to approach.
> >
> > -------------------------------------------------------------------------------------------------------------
> > Code findingCurve.m:
> >
> > ModelSimulink
> > load RealData.mat
> > t = RealData(1,:); % Input data
> > y = RealData(2,:); % Input data
> > X0=[1 2 3 4]; % Initial approach. x=[11 3 7 15] are the good ones
> > LB=[0 0 0 0];
> > UB=[1 1 1 1].*1e3;
> > options =
> > optimset('MaxIter',1e10,'MaxFunEvals',1e10,'TolFun',1e-6,'TolX',1e-6,'DiffMinChange',10);
> > [x,resnorm] = lsqcurvefit('Model', X0, t, y,LB,UB,options)
>
> Don't pass the name of the function in. Pass it in as a function handle.
>
> > -------------------------------------------------------------------------------------------------------------
> >
> > Code Model.m
> >
> > function F =Model(X0)
> > opt = simset('solver','ode45');
> > [yout] = sim('ModelSimulink',[0 0.5],opt);
> > F = yout;
> > -------------------------------------------------------------------------------------------------------------
> >
> > Matlab doesn't like something:
> >
> > ??? Error using ==> Model
> > Too many input arguments.
>
> What does LSQCURVEFIT say it expects the function that you pass into it to
> accept as input arguments?
>
> http://www.mathworks.com/help/toolbox/optim/ug/lsqcurvefit.html
>
> What does your function accept as input arguments? In particular, how many
> inputs does LSQCURVEFIT pass into your function and how many does your
> function actually accept?
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
Hello Steve,
Thanks for your answer.
The model's input has to be a 'x' vector.
x(1), x(2), x(3) and x(4) are the parameters I want to solve.
Thanks in advance,
John
|
|
0
|
|
|
|
Reply
|
hiramik (1)
|
9/27/2010 9:43:04 AM
|
|
"John " <hiramik@hotmail.com> wrote in message
news:i7pov8$9ve$1@fred.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <i7p18n$fub$1@fred.mathworks.com>...
*snip*
> Hello Steve,
>
> Thanks for your answer.
>
> The model's input has to be a 'x' vector.
That's how you've defined your function, yes.
What does LSQCURVEFIT expect your function to accept? It does NOT expect
your function to only accept one vector (the reference page is incorrect in
this case; I'll note that to the documentation staff) but _two_ vectors.
Look at the help text.
help lsqcurvefit
Because LSQCURVEFIT expects your function to accept two inputs, it calls
your function with two inputs. If your function doesn't accept two inputs,
as yours doesn't, you will receive this error.
You will need to modify your function to satisfy LSQCURVEFIT's expectations
in order to use that function with LSQCURVEFIT.
--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
|
|
0
|
|
|
|
Reply
|
slord (13285)
|
9/27/2010 3:13:41 PM
|
|
Thanks Steven,
I decided to redo everything.
As you said, I took Optimization Toolbox page 346.
My plan was to use lsqnonlin as an example and change the parameters for lsqcurvefit.
My new code:
function x = ereduTermikoa
load A.mat
y = A(:,7);
t =(1:length(y))';
preX0 = lsqcurvefit(@FOS, [1 1], t, y); % Approach to a first order system
x0=[2*preX0(1) preX0(2)/2 2*preX0(1) preX0(2)/2 ];
LB=[0 0 0 0];
UB=[1 1 1 1].*1e10;
options = optimset('Algorithm','levenberg- marquardt','Display','off','MaxIter',1e3,'MaxFunEvals',1e10,'TolFun',1e-6,'TolX',1e-6,'DiffMinChange',10);
x= lsqcurvefit(@eredua, x0,t,y,LB, UB,options);
function F = eredua(x)
myobj = sim('ereduaSimulink','SrcWorkspace','Current','StopTime','8200');
F = myobj.get('yout');
end
end
----------------------------------------------------------------------------------------------------------------
I get the same error:
??? Error using ==> ereduTermikoa>eredua
Too many input arguments.
Error in ==> lsqcurvefit at 209
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in ==> ereduTermikoa at 14
x= lsqcurvefit(@eredua, x0,t,y,LB, UB,options);
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT
cannot continue.
??? try open('??? Error using ==> ereduTermikoa>eredua
|
Error: A MATLAB string constant is not terminated properly.
------------------------------------------------------------------------------------------------------------------
Any clue?
Thanks in advance,
John
|
|
0
|
|
|
|
Reply
|
John
|
10/4/2010 10:24:05 AM
|
|
"John " <hiramik@hotmail.com> wrote in message
news:i8ca05$t4r$1@fred.mathworks.com...
> Thanks Steven,
>
> I decided to redo everything.
> As you said, I took Optimization Toolbox page 346.
>
> My plan was to use lsqnonlin as an example and change the parameters for
> lsqcurvefit.
>
> My new code:
>
> function x = ereduTermikoa
> load A.mat y = A(:,7);
> t =(1:length(y))';
> preX0 = lsqcurvefit(@FOS, [1 1], t, y); % Approach to a first order
> system
> x0=[2*preX0(1) preX0(2)/2 2*preX0(1) preX0(2)/2 ];
> LB=[0 0 0 0];
> UB=[1 1 1 1].*1e10;
>
> options = optimset('Algorithm','levenberg-
> marquardt','Display','off','MaxIter',1e3,'MaxFunEvals',1e10,'TolFun',1e-6,'TolX',1e-6,'DiffMinChange',10);
>
> x= lsqcurvefit(@eredua, x0,t,y,LB, UB,options);
LSQCURVEFIT calls your function with TWO input arguments; the vector of
parameters to be optimized and the xdata at which it expects your objective
function to be evaluated.
> function F = eredua(x)
Your function does not accept two input arguments. This is the reason for
the error you received. Modify your function so it does accept two input
arguments.
*snip*
--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
|
|
0
|
|
|
|
Reply
|
slord (13285)
|
10/4/2010 1:35:26 PM
|
|
|
7 Replies
428 Views
(page loaded in 1.124 seconds)
Similiar Articles: lsqcurvefit and Simulink - comp.soft-sys.matlabHello, I have been using lsqcurvefit for the last weeks. Till now, I did my data fitting for a known function. But right now, I want to step forwar... BER vs SNR plot in simulink - comp.soft-sys.matlabBER vs SNR plot in simulink - comp.soft-sys.matlab... help me out in plotting the BER Vs SNR in simulink, I am using ... measured' - comp.soft-sys.matlab QPSK BER ... Simulink Algebraic Loop Error Help - comp.soft-sys.matlab ...lsqcurvefit and Simulink - comp.soft-sys.matlab Help with the lsqcurvefit solver - comp.soft-sys.matlab Simulink Algebraic Loop Error Help - comp.soft-sys.matlab ... lsqcurvefit error using matlab R2008a - comp.soft-sys.matlab ...lsqcurvefit and Simulink - comp.soft-sys.matlab lsqcurvefit error using matlab R2008a - comp.soft-sys.matlab ..... I have start a file, but I am receiving errors such as ... Help with the lsqcurvefit solver - comp.soft-sys.matlablsqcurvefit and Simulink - comp.soft-sys.matlab Help with the lsqcurvefit solver - comp.soft-sys.matlab lsqcurvefit and Simulink - comp.soft-sys.matlab Help with the ... lsqcurvefit with ODE45 - comp.soft-sys.matlablsqcurvefit and Simulink - comp.soft-sys.matlab lsqcurvefit with ODE45 - comp.soft-sys.matlab lsqcurvefit and Simulink - comp.soft-sys.matlab using lsqcurvefit to fit an ... problem with lsqcurvefit - comp.soft-sys.matlablsqcurvefit and Simulink - comp.soft-sys.matlab Problem with LevenbergMarquardt algorithm in lsqcurvefit - comp ... Dear All, I am using lsqcurvefit with the following ... Problem with "Trouble solving algebraic loop" - comp.soft-sys ...Solving a system of equations in Simulink - comp.soft-sys.matlab ... solving algebraic loop - comp.soft-sys.matlab Solving a system of equations in Simulink - comp.soft ... Problem with LevenbergMarquardt algorithm in lsqcurvefit - comp ...Dear All, I am using lsqcurvefit with the following option for one of my ... lsqcurvefit and Simulink - comp.soft-sys.matlab... UB=[1 1 1 1].*1e10; options = optimset ... using lsqcurvefit to fit an ODE to data and finding parameters ...lsqcurvefit and Simulink - comp.soft-sys.matlab lsqcurvefit with ODE45 - comp.soft-sys.matlab lsqcurvefit and Simulink - comp.soft-sys.matlab using lsqcurvefit to fit an ... lsqcurvefit and Simulink - comp.soft-sys.matlab | Computer GroupHello, I have been using lsqcurvefit for the last weeks. Till now, I did my data fitting for a known function. But right now, I want to step forwar... Least Squares (Model Fitting) Examples :: Optimization Algorithms ...Examples of least squares optimization using lsqnonlin, lsqcurvefit, and lsqlin ... Note The call to sim results in a call to one of the Simulink ordinary ... 7/27/2012 6:30:29 PM
|