Code optimization by avoiding Xcos recompilation and others.

Hi everyone! I'm an aerospace engineering student and my professors and I are working on a project to addapt a subject's assignments to Scilab, so that students have easier access to the software. Now we are trying to reproduce the actuation of an aircraft depending on some variables. This is my first time using this software so there are many things I ignore.

And here is the question: How could I optimize this code? Currently it's way too slow and we need to make it a bit faster. I tried using `scicos_simulate()` and saving the Xcos compilation information to a `list()`called `Info`in order to avoid recompiling the diagram every time - since the code calls the Xcos diagram nine times and that takes a while - but I don't know if there is a better way or even if it worked properly.

Also the values for `u_ini`, `w_ini`and `pospalanca`change depending on the compilation. Is there a way to make this part run faster?

Any help is greatly appreciated!

Here's the code:

```// Scilab code
importXcosDiagram("Trabajo.zcos");

controltotal = 0;
preci = 1 ;

while controltotal == 0

controlV = 0;

V1 = 1 ;
V = V1 ;
u_ini = V*cos(alfa);
w_ini = V*sin(alfa);
Context.Tf=0.1;
Info = scicos_simulate(scs_m, list(), Context, 'nw');
Fuerzas=FuerzasStruct.values;
Fz1 = Fuerzas(3);

V2 = 340 ;
V = V2 ;
u_ini = V*cos(alfa);
w_ini = V*sin(alfa);
Context.Tf=0.1;
scicos_simulate(scs_m, Info, Context, 'nw');
Fuerzas=FuerzasStruct.values;
Fz2 = Fuerzas(3);

while controlV == 0
peso1 = 1-abs(Fz1)/abs(Fz2-Fz1);
peso2 = 1-abs(Fz2)/abs(Fz2-Fz1);
V_med = sqrt(V1*V1*peso1 + V2*V2*peso2) ;
V = V_med;
u_ini = V*cos(alfa);
w_ini = V*sin(alfa);
Context.Tf=0.1;
scicos_simulate(scs_m, Info, Context, 'nw');
Fuerzas=FuerzasStruct.values;
Fz_med = Fuerzas(3);

if abs(Fz_med) < preci
controlV = 1;
elseif Fz_med*Fz1<0
V2 = V_med;
Fz2 = Fz_med;
elseif Fz_med*Fz2<0
V1 = V_med;
Fz1 = Fz_med;
end
end
V = V_med ;

controlpospalanca=0;

pospalanca1 = 0 ;
pospalanca = pospalanca1 ;
Context.Tf=0.1;
scicos_simulate(scs_m, Info, Context, 'nw');
Fuerzas=FuerzasStruct.values;
Fx1 = Fuerzas(1);

pospalanca2 = 1 ;
pospalanca = pospalanca2 ;
Context.Tf=0.1;
scicos_simulate(scs_m, Info, Context, 'nw');
Fuerzas=FuerzasStruct.values;
Fx2 = Fuerzas(1);

while controlpospalanca == 0
peso1 = 1-abs(Fx1)/abs(Fx2-Fx1);
peso2 = 1-abs(Fx2)/abs(Fx2-Fx1);
pospalanca_med = (pospalanca1*peso1 + pospalanca2*peso2) ;
pospalanca = pospalanca_med;
Context.Tf=0.1;
scicos_simulate(scs_m, Info, Context, 'nw');
Fuerzas=FuerzasStruct.values;
Fx_med = Fuerzas(1);

if abs(Fx_med) < preci
controlpospalanca = 1;
elseif Fx_med*Fx1<0
pospalanca2 = pospalanca_med;
Fx2 = Fx_med;
elseif Fx_med*Fx2<0
pospalanca1 = pospalanca_med;
Fx1 = Fx_med;
end
end
pospalanca = pospalanca_med ;

controldelta=0;

delta1 = -30*%pi/180 ;
delta = delta1 ;
Context.Tf=0.1;
scicos_simulate(scs_m, Info, Context, 'nw');
Fuerzas=FuerzasStruct.values;
My1 = Fuerzas(2);

delta2 =  30*%pi/180 ;
delta = delta2 ;
Context.Tf=0.1;
scicos_simulate(scs_m, Info, Context, 'nw');
Fuerzas=FuerzasStruct.values;
My2 = Fuerzas(2);

while controldelta == 0
peso1 = 1-abs(My1)/abs(My2-My1);
peso2 = 1-abs(My2)/abs(My2-My1);
delta_med = (delta1*peso1 + delta2*peso2) ;
delta = delta_med;
Context.Tf=0.1;
scicos_simulate(scs_m, Info, Context, 'nw');
Fuerzas=FuerzasStruct.values;
My_med = Fuerzas(2);

if abs(My_med) < preci
controldelta = 1;
elseif(My_med*My1<0)
delta2 = delta_med;
My2 = My_med;
elseif(My_med*My2<0)
delta1 = delta_med;
My1 = My_med;
end
end
delta = delta_med ;

Fuerzas=FuerzasStruct.values;

if (abs(Fuerzas(1)) < preci) & (abs(Fuerzas(2)) < preci) & (abs(Fuerzas(3)) < preci)
controltotal = 1;
end
end // while controltotal

V
pospalanca
delta

Thank you all so much for your help and time!

Scilab 01-05-21, 2:51 p.m. AnaCastro
The information is good, i need more, i'm still learning about it. run 3
05-05-21, 10:03 a.m. kaleanna

How to optimize the code by avoiding recompiling the Xcos is pretty cool and efficient. I appreciate how to do this. wuxiaworld
11-05-21, 10:22 a.m. minion89