r/scilab • u/mrhoa31103 • 3d ago
XCOS Series - Installment 2 - Simple Solve of First Order ODE (Where everyone starts!) but we'll add a few bells and whistles
We'll start off with the video I mentioned previously "Xcos - quick and complete introduction" by "Automotive Engineering Explained" a hands-on tutorial on Xcos block diagram modeling and simulation, containing the following topics:
- What is Xcos ?
- Xcos environment
- How to launch Xcos
- Xcos Palette
- Xcos block
- Build an Xcos diagram
- Xcos simulation parameters
- Run simulation and visualize results
My add to his list:
- Demonstrate how to get simulation data from the Xcos window into the main Scilab workspace where we can do a much better job of documentation.
Show how to run an Xcos simulation from a standard Scilab script.
- //Next Two Lines Loads and Executes the Xcos File // importXcosDiagram("C:\Users\MattheW\Documents\eBooks\ScILabNotes\twentyeighthSciLabFile.zcos"); //filename.xcos or filename.zcos
- xcos_simulate(scs_m, 4); //
Plotting Xcos simulation variables (cell variables) without massaging them beforehand.
- //Plot the Xcos data on the chart black +'s
- plot2d(Y_Xcos.time,Y_Xcos.values,style = -1); // style = -1 gives symbols +'s with no lines
- Not sure whether he covered it or someone else but to get the Latex (fancy math text) representations, you need "$$" at the beginning and end of the string and use standard Latex commands. The labels on the lines are just text located close to the line and nothing else.
Note: I'm going to provide pictures of the Xcos networks and not provide the diagram files and let you build them for yourself especially early on.
XCOS wise: Things that need to be set beyond building the network shown in the diagram.
- "Sampling Time" Clock => Period 0.1 seconds/Initialization Time 0.01 seconds
- "To workspace => Size of Buffer: 128 / Scilab Variable Name: Y_Xcos (this variable is the "cell" type variable) / Inherit 0 (I have not dug into this setting but it works for now)
- "1/s" (Integer Block) => Initial State 1 / We'll get into how to transfer constant from the main window next week.
- From the ribbon: "Simulation/Setup" - Final Integration Time 3.0/Maximum Step Size = 0.01 and everything else can be the defaulted values. Note: The integrator is "Sundials/CVODE-BDF-NEWTON" but could be changed if you so chose.
I'm going to use one of my previous "Numerical Analysis" Scripts as a "known good" solution.
Main Script Output:
"Ordinary Differential Equations- Initial Value Problems using Runge-Kutta(RK-4)Methods of Integration"
"2026-11-30 11:16:26.219"
Figures:
Xcos Network (Diagram - generated directly from Export menu in Xcos)

Scope Output (from Xcos Output) Note - if this graph is the last output you do (rare), you can add stuff to it from the main command window. Such as labels, titles, xgrid...but it has to be the last one. I'll need to dig some to see if there is a figure number attached to it.

Main Script Output

Main Script Code:
clear
clc
//close out all existing figures
close(winsid())
//Ordinary Differential Equations Initial Value Problems
//Lec 7.4 Runge-Kutta Methods - higher order Algorithms
//https://www.youtube.com/watch?v=51PwCoFtyXw&ab_channel=MATLABProgrammingforNumericalComputation
//
//
disp("Ordinary Differential Equations- Initial Value Problems using Runge-Kutta(RK-4)Methods of Integration",string(datetime()))
//
//Runge-Kutta Method:
//
// y(i+1) = y(i) + h*Si
// where, nth order RK method gives the slope as
// Si = w1*k1+w2*k2+...+wn*kn
// w=weighted values
// k = ??
//
// Huen's Method (RK-2)
// y(i+1)=y(i)+h/2*(k1+k2)
// k1 = f(t(i),y(i)), k2 = f(t(i+h),y(i)+h*k1)
//
// Using the previous differential equation example
// y' + 2*t*y = 0
// y(0)=1
//
// Standard RK-4 Method
// y(i+1)=y(i)+h/6*(k1+2*k2+2*k3+k4);//Error Order h^5
// k1 = f(t(i),y(i))
// k2 = f(t(i+h/2),y(i)+h*k1/2)
// k3 = f(t(i+h/2),y(i)+h*k2/2)
// k4 = f(t(i+h),y(i)+h*k3)
//
//define the function for explicit method
function [
results
]=derivativeF(
t
,
x
)
results
= -2*
t
*
x
;
endfunction
//
y0 = 1
//Initial Condition required
// ODE: y'=-2*t*y
// Analytical Solution: y(t) = exp(-t^2);
//
n = 1250;
//number of steps - It takes n>=1500 for RK4 to be
//more accurate than RK2!!! In SciLab...Matlab says
//RK4 vastly better.
tstart = 0;
//start time for simulation
tstop = 3;
//stop time for simulation
h = (tstop-tstart)/n;
//step size
t = linspace(tstart,tstop,n);
//time vector
ya = exp(-t.^2);
// Analytical Solution vector
y=[y0];
// Initial point in y output vector
y4=[y0];
// Initial point in y4 output vector
for i = 1:1:n-1;
//Simulation Heun's Method
k1 = derivativeF(t(i),y(i));
k2 = derivativeF(t(i+h),y(i)+h*k1)
y(i+1)=y(i)+h/2*(k1+k2);
end
for i = 1:1:n-1;
//Simulation RK4 Method
k1 = derivativeF(t(i),y(i))
k2 = derivativeF(t(i+h/2),y(i)+h*k1/2)
k3 = derivativeF(t(i+h/2),y(i)+h*k2/2)
k4 = derivativeF(t(i+h),y(i)+h*k3)
y4(i+1)=y4(i)+h./6.*(k1+2*k2+2*k3+k4);
end
//
//Next Two Lines Loads and Executes the Xcos File
//
importXcosDiagram("C:\Users\MattheW\Documents\eBooks\ScILabNotes\twentyeighthSciLabFile.zcos");
xcos_simulate(scs_m, 4);
//
yshift= y+0.01;
//Shifting numerical output to show on same graph
yshift1=y4-0.01;
//Shifting numerical output to show on same graph
err_percent = abs((ya'-y)./ya')*100;
//error percentage calculation
err_4_percent = abs((ya'-y4)./ya')*100;
//error percentage calculation
//next line is information only
output = [t',ya',y, err_percent,y4,err_4_percent];
//output dump
//disp("output ",output);
//plot2d(t',output);//black =, blue = 2, green =3, cyan = 4, red = 5
//plotting example fancy output two y axes...
scf(0);clf;
//axis y1
c = color("black")
c1 = color("red")
c2 = color("green")
//plot(t',ya',"-b",t',y,"--r")
plot2d(t',ya',style=c);
//black =, blue = 2, green =3, cyan = 4, red = 5
plot2d(t',yshift',style=c1);
//black =, blue = 2, green =3, cyan = 4, red = 5
plot2d(t',yshift1',style=c2);
//black =, blue = 2, green =3, cyan = 4, red = 5
//Plot the Xcos data on the chart black +'s
plot2d(Y_Xcos.time,Y_Xcos.values,style = -1);
//
h1=gca();
h1.font_color=c;
h1.children(1).children(1).thickness =2;
xlabel("$t(Seconds)$","FontSize",3)
ylabel("$Y(t)$","FontSize",3);
title("$https://x-engineer.org/create-multiple-axis-plot-scilab$","color","black");
xgrid
legend("$Analytical$","$Heun\ ''s\ Method+0.01\ shift$","$RK4-0.01\ shift$","$Xcos\ no\ shift$",1)
a = gce();
a.Font_Size = 3;
// Axis y2
c=color("blue");
c1 = color("red")
h2=newaxes();
h2.font_color=c;
plot2d(t',err_4_percent,style=c1)
plot2d(t',err_percent,style=c)
h2.filled="off";
h2.axes_visible(1)="off";
h2.y_location="right";
h2.children(1).children(1).thickness=2;
ylabel("$Error Percentage$","FontSize",3,"color",c)
xgrid



















































