# How to do parametric analysis (equivalent of PARAM\_VAR) in recent versions of SCILAB

**URL:** <https://scilab.discourse.group/t/how-to-do-parametric-analysis-equivalent-of-param-var-in-recent-versions-of-scilab/517>\
**Category:** Xcos\
**Created:** [May 22, 2024, 6:26pm UTC](https://scilab.discourse.group/t/how-to-do-parametric-analysis-equivalent-of-param-var-in-recent-versions-of-scilab/517 "2024-05-22T18:26:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pascal77](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pascal77](https://scilab.discourse.group/u/Pascal77)\
**Post date:** [May 22, 2024, 6:26pm UTC](https://scilab.discourse.group/t/how-to-do-parametric-analysis-equivalent-of-param-var-in-recent-versions-of-scilab/517/1 "2024-05-22T18:26:11Z")

</div>

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](http://DeepL.com) (free version)

---

<div class="post-metadata">

**Author:** ![Pascal77](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pascal77](https://scilab.discourse.group/u/Pascal77)\
**Post date:** [May 25, 2024, 12:25pm UTC](https://scilab.discourse.group/t/how-to-do-parametric-analysis-equivalent-of-param-var-in-recent-versions-of-scilab/517/2 "2024-05-25T12:25:06Z")

</div>

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:

```auto
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:

 ![XCOS diagram - how to set parameters SCILAB 2024](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/6b1b4f3133506899370731b6c0f097dfdfc9e03a.png)

Plot results:

![plot diagram - how to set parameters SCILAB 2024](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/18dc9bc9052a1eda2fca748b9607606a5b0309d3.png)

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?

---

<div class="post-metadata">

**Author:** ![Pascal77](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pascal77](https://scilab.discourse.group/u/Pascal77)\
**Post date:** [May 26, 2024, 2:31pm UTC](https://scilab.discourse.group/t/how-to-do-parametric-analysis-equivalent-of-param-var-in-recent-versions-of-scilab/517/3 "2024-05-26T14:31:17Z")

</div>

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:

```auto
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](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/4828c211f951474a1ed345d572f792e41ef1dca9.png)

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [May 27, 2024, 7:24am UTC](https://scilab.discourse.group/t/how-to-do-parametric-analysis-equivalent-of-param-var-in-recent-versions-of-scilab/517/4 "2024-05-27T07:24:52Z")

</div>

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](https://stackoverflow.com/questions/78513184/is-it-possible-to-create-a-button-inside-an-xcos-diagram-in-order-to-print-aut)). 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.

---

<div class="post-metadata">

**Author:** ![Pascal77](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pascal77](https://scilab.discourse.group/u/Pascal77)\
**Post date:** [May 27, 2024, 9:02am UTC](https://scilab.discourse.group/t/how-to-do-parametric-analysis-equivalent-of-param-var-in-recent-versions-of-scilab/517/5 "2024-05-27T09:02:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pascal77](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pascal77](https://scilab.discourse.group/u/Pascal77)\
**Post date:** [June 5, 2024, 9:49am UTC](https://scilab.discourse.group/t/how-to-do-parametric-analysis-equivalent-of-param-var-in-recent-versions-of-scilab/517/6 "2024-06-05T09:49:14Z")

</div>

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](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/cc26e996a6cdb7f475358ac2ef41e63d03dfa2e4.png)

```auto
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.

 ![2024-06-05 11_47_28-Figure n°20003](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/d6f5e9f1bbf49ef1ef345221f9e2484457fd28ad.png)

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