MATLAB Answers

Fitting simulink model with optimization toolbox?

7 views (last 30 days)
Jim Bosley
Jim Bosley on 31 Oct 2017
Answered: Yogananda Jeppu on 13 Nov 2017
I don't currently have Simulink Design Optimization.
I'm using optimization and global optimization toolboxes to fit a simulink model to data
I'd like to use something like the pseudo code example below. Am I barking up the right tree, or is there a better approach? The optimization examples on the mathworks site don't have any simulink optimization by toolbox examples. Are there examples out there?
Anyone have feedback on Simulation Design Optimization vs using optimization toolbox?
Thanks!
FitModel.m
Program FitModel
load_system('mymodel') ; % mymodel uses named parameters, and loads them with preload function
% For this example, the preload function is prlfcn.m
x0(1) = a; % a and b are matlab variables in defined in the script prlfcn.m
x0(2) = b; % and are parameters in the simulink model mymodel. For example,
% the value of a simulink constant block might be set to "a", and
% a simulink integrator initial condition might be set to "b"
% prlfcn.m contains my best current guess for a and b
ofh = @objfun(x) % objfun is the objective function, x is the vector of parameters
bestx = fminsearch(ofh,x0);
% end FitModel.m
objfun.mat
function value = objfun(x)
a = x(1); % Here, I'm putting the passed parameter x values into the matlab
b = x(2); % variables a and b, which are used by the model mymodel
sim('mymodel') ; % mymodel writes data to a "to file" block output.mat
load('output.mat') % output.mat has t and y vectors
load('data.mat') % data.mat has tdata and ydata vectors
value = (y-ydata)'*(y-ydata)
return
% end objfun.m
The preload function is represented by this simple example:
prlfcn.m
a = 5; % value for constant 1
b = 10; % initial condition for the model integrator
% end prlfcn.m

Answers (1)

Yogananda Jeppu
Yogananda Jeppu on 13 Nov 2017
I use a global variable for this and this works well. Define the tunable parameters in simulink as X array. X(1) is a gain in the simulink. X(2) is initial condition etc. Dont load data inside the function. Do that outside and just pass values as parameters of global.
global X inp y ydata
function value = objfun(x) global X inp y ydata % perhaps you have all absolute values X = abs(x); sim('mymodel') ; % mymodel writes data to a "to file" block output.mat
value = norm(y-ydata); return
This has been working for me. Not an elegant code in terms of software engineering but it gets the job done.

Community Treasure Hunt

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

Start Hunting!