Dear all,
i’m wondering whether it exists somewhere help resource or specific examples about the calibration of model developed in Xcos (modelica) environment ?
As first step in naïve approach I’m considering to write a cost function that includes a call to Xcos simulation where parameters are read from the Context
and simulation outputs in matrix updated in the context
as well. Then call fminsearch with such cost function. Does it make sense or another more Xcos-integrated approach available ?
Thank you for any comment and tip !
David
Hello David,
Reading your question, I realize that we completely miss this kind of example in Scilab. Data fitting when the parameters are those of a system of odes is quite common and we have such examples in pure Scilab. But when the system is an Xcos simulation, I think we don’t have any. Anyway, I don’t see any other method than using the Context and ToWorkspace blocks (but I have a limited experience of Xcos).
S.
thank you Stephane for your comment, hopefully I may contribute with some example in a few days !
In fact here is a simple example, where the a coefficient of an exponential
y=\exp(at) is fitted:
function [c,Y]=costf(a)
Context.a=a;
scicos_simulate(scs_m,Context);
c=sum((Y.values-Yobs').^2);
endfunction
loadXcosLibs(); loadScicos();
importXcosDiagram("exp.zcos")
tobs=0:29;
Yobs=exp(-tobs/15)+rand(tobs,"normal")/50;
options = optimset ("Display","iter");
a_opt = fminsearch(costf,0,options);
t=linspace(0,29,100);
clf
plot(tobs,Yobs,'o',t,exp(a_opt*t));
legend("data","y=exp("+string(a_opt)+"*t)")
Iteration Func-count min f(x) Procedure
0 2 11.077356
1 2 11.077356 initial simplex
2 4 4.6579769 expand
3 6 0.4460295 expand
4 8 0.0476999 reflect
5 10 0.0399707 contract inside
6 12 0.0091233 contract inside
7 14 0.0091233 contract inside
8 16 0.0091233 contract inside
9 18 0.0086472 contract inside
10 20 0.0086472 contract inside
11 22 0.0086472 contract inside
12 24 0.0086463 contract inside
13 26 0.0086445 contract inside
Optimization terminated:
the current x satisfies the termination criteria using OPTIONS.TolX of 0.0001
and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 0.0001

exp.zcos (2.9 KB)
Thank you Stephane, it clarifies the structure and key steps to drive the calibration process with underlying Xcos simulation.
David
Hello,
I’m running Xcos simulation in batch mode for calibration , so using the nw
option and Infos
between the calls to avoid unneeded recompilation of my diagram that is containing Modelica blocks.
Infos= scicos_simulate(scs_m,Infos,Context,"nw");
However, since the optimization of parameters to minimize error function ( contains scicos_simulate()
call above) requires to change the numerical values, the system of equations is changing between each call and diagram actually needs to be recompiled at each iterative call and I got a huge number of compilation popup windows as shown below:
Is there any way in Xcos functions/options to disable the compilation popup, rather than running whole scilab in console mode ?
In addition, the optimization is terminating abruptly on an xcos simulation error, maybe because I didn’t set bounds of the parameters yet, especially no physically realistic constraints set yet :
at block #5 “”
IDACalcIC: The residual function failed at the first call.
Xcos error: Partial information in “pout(2)” is unset for Block #16 - “-3293c853:1970b8356e6:-7fd5” : a value between 1 and 52 expected.
and I was not able to retrieve the last set of parameters. Is there a way to stop optimization on error with try catch
for instance ?
Thank you for any advice
David
To me, this is a no go for such a setup. You should directly use OpenModelica and its optimization features (see Optimization with OpenModelica — OpenModelica User's Guide v1.26.0-dev-135-ge3a5fb056f documentation).
S.
I managed to run the optimization from Advanced Console
Scilab mode and in this case I have no popup, the content is redirected to the console : so I got a number of unneeded lines but I can at least work in parallel.
I use now leastsq
instead of fminsearch to allow setting the bounds’ constraints of solutions. Let’s see if relevant solutions are found…
x0= [100. 0.1 0.00001]'
iprint= 3
[fopt,xopt] = leastsq(iprint, costf, "b",[1E-2;1E-5;1E-9],[1E4;1;1], x0, "gc")