How to do parametric analysis (equivalent of PARAM_VAR) in recent versions of SCILAB

In SCILAB 5.5.2, a package called CPGE was used very frequently in educational circles with great success for creating XCOS diagrams. Unfortunately, this package is no longer updated and is incompatible with the latest versions of SCILAB.
One of the really useful features of CPGE was the ‘PARAM_VAR’ block (a simple block, very intuitive to use) which allowed you to assign several values to a parameter during a simulation and therefore to plot multiple values on a single graph.

My question is how can we do this today with recent versions of SCILAB in a simple way while remaining in XCOS?

Translated with DeepL.com (free version)

As I get no answers, I suppose it is impossible. So I propose a solution with a post processing functionality which needs to write a script. It is less convenient than a simple block like PARAM_VAR used with CPGE package.

One way of performing a parametric analysis with the latest versions of SCILAB is to use “xcox_simulate” post processing functionality:

  1. Create the XCOS diagram with “To Workspace” block to get the data outside (see my example below),
  2. In the diagram, don’t forget to set the context to define the parameter(s) to vary,
  3. Write a script using xcos_simulate (see my example below),
  4. Run the script to watch the plot with several curves according to the variations of the parameter(s). No need to run the XCOS diagram before because the script loads the diagram.

The corresponding script I wrote:

importXcosDiagram("zcos file path/how to set parameters.zcos")

typeof(scs_m) //The diagram data structure

//This diagram uses 2 context variables :
//  z : the damping coefficient
//  T :  the time constant
scs_m.props.context //the embedded definition

//first batch simulation with the parameters embedded in the diagram
xcos_simulate(scs_m, 4);
clf();// clear previous window
plot(simOut.time,simOut.values,'b');


// Change z value
scs_m.props.context = ["z=0.5" "T=1"];
xcos_simulate(scs_m, 4);
plot(simOut.time,simOut.values,'r');


// Change z value again
scs_m.props.context = ["z=1" "T=1"];
xcos_simulate(scs_m, 4);
plot(simOut.time,simOut.values,'k');

xgrid();

legend("z = 0.1","z = 0.5","z = 1")

XCOS diagram:

Plot results:

plot diagram - how to set parameters SCILAB 2024

The plot is correct excepted the legend which is wrong. I don’t know why. I have tried several ways to plot the curves, but none of them is OK. If anyone has any ideas, I’d love to hear from you.

My opinion is that the method using xcos_simulate is very cumbersome compared with the simple PARAM_VAR block defined in the CPGE package. I’m sorry that this package is no longer compatible.

I wonder whether recreating this custom block for the latest versions would be complicated or not?

I solved the problem of the erroneous Legend by using scicos_simulate instead of xcos_simulate instruction.

The XCOS .

The script is the following one:

importXcosDiagram("file path/how to set parameters.v2.zcos")

typeof(scs_m) //The diagram data structure

//This diagram uses 1 context variable :
//  z : the damping coefficient
//  T :  the time constant
scs_m.props.context; //the embedded definition

// Create a dedicated fig. not to interfer with the Cscope one
fg=figure(0);clf();

//first batch simulation with the parameters embedded in the diagram
scicos_simulate(scs_m);

plot(simOut.time,simOut.values,'b');


// Change z value
Context.z=0.5;
scicos_simulate(scs_m,Context);
simOut
plot(simOut.time,simOut.values,'r');


// Change z value again
Context.z=1;
scicos_simulate(scs_m,Context);
simOut
plot(simOut.time,simOut.values,'k');

xgrid();

legend("z = 0.1","z = 0.5","z = 1")

The Legend is now correct as you can see below:

plot diagram - how to set parameters SCILAB 2024.v2

Hello Pascal,

That’s great that you made it yourself. However, in the future, please don’t crosspost (e.g. both discourse and stackoverflow). For example, nobody understands that you finally were given the idea of using scicos_simulate on stackoverflow (plot - Is it possible to create a "button" inside an XCOS diagram in order to print automatically a curve defined by a "To Workspace" block? - Stack Overflow). In order to be able to learn from users requests and use cases, it is better to be able to read the whole story in one place, and Scilab discourse is the best one.

Anyway, thanks for joining the Scilab community !

S.

Thanks for your welcome message.

The messages posted in parallel on scilab.discourse.group and stackoverflow are identical. I just wanted to increase the scope of my message. No offense.

In addition, I’m taking advantage of this reply to ask whether it would be complicated to update the CPGE package to the latest version of SCILAB, or possibly have an idea on how to recreate a custom block that would have the same functions as PARAM_VAR?

I’m not a coding specialist, but if there’s a methodology for programming a block yourself in XCOS, I’d be interested. I haven’t found one on the net.

Thank you in advance for your reply.
Pascal77

PS :
Regardig the topic above, and more particularly the legend problem, in fact, I got the root explanation : CScope in the associated XCOS diagram interferes with the plots that will be launched by the script. To avoid this, simply add the following instruction: fg=figure(0);clf();

A more radical solution is to remove the CScope from the XCOS diagram.

Just for the fun, you will find hereafter another way to do the parametric analysis with plot2d, this time. Moreover, the plot shows the Input this time.

2024-06-05 11_43_07-Parametric analysis with input plot (C__Users_Pascal_Documents_SciLab files_SCIL

importXcosDiagram("C:\full path name\Parametric analysis.zcos")

typeof(scs_m) //The diagram data structure

//This diagram uses 1 context variable :
//  z : the damping coefficient

scs_m.props.context; //the embedded definition
clf();
// Create a dedicated fig. not to interfer with the Cscope one
fg=figure(0);clf();

//first batch simulation with the parameters embedded in the diagram
scicos_simulate(scs_m);
//plot(output.time,output.values,'b');
t=output.time;
x=Input.values;
y1=output.values;

// Change z value
Context.z=0.5;
scicos_simulate(scs_m,Context);
//plot(output.time,output.values,'r');

y2=output.values;

// Change z value again
Context.z=1;
scicos_simulate(scs_m,Context);
//plot(output.time,output.values,'k');

y3=output.values;

plot2d(t,[x,y1,y2,y3],[5,2,13,27],leg="Input@z = 0.1@z = 0.5@z = 1",axesflag=1);
xgrid(color("black"),1,7);
ax=gca();

ax.children;
ax.background=color('snow');//color name get from color_list
ax.thickness = 2;  // ...and the thickness of the frame.

gcf().background=color('antiquewhite');//color name get from color_list
gcf().children.children.children.thickness = 2;//set thickness of all curves
gcf().figure_size=[800,600];


The plot is given below with its legend outside. I have done some cosmetic improvements to have a better view.

This way to proceed seems to be more efficient and reliable (regarding the legend).

1 Like