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
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
"John " <hir...@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
sl...@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
"John " <hir...@hotmail.com> wrote in message
news:i7pov8$9ve$1...@fred.mathworks.com...
> "Steven_Lord" <sl...@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.
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
"John " <hir...@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*