r/scilab • • Nov 13 '25

r/Scilab is now public. AKA Not Restricted so people can now post to this subreddit.

6 Upvotes

Ladies and Gentlemen,

This subreddit is back open to the public. You can post your Scilab queries here. It may take a while to breathe life back into the subreddit so get the word out. I'll will be working on the wiki to get some Scilab support information out there and some links to the discord site.

Hope to see a growing community here soon.


r/scilab • • Feb 12 '25

Help with my simulation

Thumbnail
gallery
5 Upvotes

I would like to perform a closed loop simulation with PID control; but unfortunately it is not working, a graph of an infinite line appears, I probably configured something wrong. It was supposed to be like this other graph from colab. If anyone can help me, I thank you in advance.


r/scilab • • Oct 22 '24

Xcos for embedded software

5 Upvotes

Hi all! How many of you use xcos and scilab for testing control strategies to be implemented in embedded systems? I mean real time discrete control simulations and even custom C code blocks for control validation.

I know there are Commercial solutions for power electronics, vehicle dynamics, robotics, etc. but a "free for commercial purposes" licensing makes scilab and xcos super interesting for small companies that can not afford expensive Simulink (Matlab) or Simba (Python) simulation environments.

Did I discovered the paradise of control engineers?


r/scilab • • 11d ago

Scilab 2027.0.0 is released. They finally have transparency just as I had figured out a work around.

4 Upvotes

r/scilab • • Apr 08 '26

I finally cracked the code on Scilab 3D Integration. They could surely do a better job on documentation.

4 Upvotes

If you look at their examples, quote

// integration over a cube -1<=x<=1;-1<=y<=1;-1<=z<=1

function v=f(xyz, numfun), v=xyz'*xyz, endfunction

[result, err] = int3d(-1,1,-1,1,-1,1, f, 1, [0,100000,1.d-5,1.d-7])

but you have to integrate "v = 3*x*y^3*z^2" over xrange (-1,3), yrange(1,4) and zrange(0,2)...

here's what you have to do...

// Define the function f(x, y, z)...nope,nope,nope!!! Get crazy

// error codes like unknown z variable

// Define this function

function v= f(xyz, numfun)

//Note: x = xyz(1), y = xyz(2), z = xyz(3)

//have no idea what "numfun" is but you cannot drop it.

v=3*xyz(1)*xyz(2)^3*xyz(3)^2

endfunction

// Triple integration in one go

// computes the intergral of 3xy^3z^2 over the

// integration over a cube -1<=x<=3;1<=y<=4;0<=z<=2

// Correct Answer is 2040

[result, err] = int3d(-1,3,1,4,0,2, f, 1, [0,100000,1.d-5,1.d-7])

outputString = strcat(['3d result = ', string(result)]);

disp(outputString);

I understood that v had to be a single number how to get it was the trick. It would have been really nice to have an example like this one in their documentation.

Go ahead and roast me if you thought was blatantly obvious!


r/scilab • • Apr 21 '20

How do I learn Scilab?

4 Upvotes

Hello, I'm going through a Scilab course on Udemy, but it is too brief. I don't know where to go to continue learning about the program after I'm done with this course. Until now my only option would be searching the scilab manual, keyword for keyword.

Background: 3rd year engineering student, know basic principles of some very basic languages and Matlab.


r/scilab • • Mar 08 '19

I wrote a program to plot 3 curves for different values of k, but I am unable to make the title contain the value of k. Is there any way I can do that? I am new to scilab.

Post image
4 Upvotes

r/scilab • • Dec 28 '25

Sixth Installment - SciLab Equivalent File for NPTEL "Matlab Programming for Numerical Computation - Subjects: Lower Upper (LU) Decomposition and Partial Pivoting

3 Upvotes

Subject - Solving Simultaneous Equations via Lower Upper (LU) Decomposition and Partial Pivoting and row reduced echelon command).

============================================================

Note some extra stuff not part of the Numerical Analysis Course: I've added some determinate tracking calculation throughout the program just to show how the partial pivoting and decompositions affect the determinate value. I'm currently refreshing my Linear Algebra stuff and I'm on determinants and row operation influences.

I also added the use of the "lu" function in SciLab and how to get the solution vector using the lu function result and rref function techniques. I had some disappointment that the lusolve command is only for solving sparse matrices.

============================================================

As I mentioned that I would add some SciLab code I generated while doing a refresher on Numerical Computation (and gave me an opportunity to learn SciLab less the XCOS stuff).

The YouTube channel is referenced in the second line of the program just remember strip off the "//" (// is making it a comment line in SciLab) before the https://...

https:
//www.youtube.com/watch?v=smsE7iOlNj4&t=17s&ab_channel=MATLABProgrammingforNumericalComputation

is the link to the sixteenth class.

Output:

"A = "

  1. 4. -2.

    1. 3.

"determinate of A ="

-4.0000000

"b = "

4.

9.

7.

"Augmented Matrix A:b ="

      1. 4.

  1. 4. -2. 9.

    1. 3. 7.

"Solution to Ax =b x1 ="

1.0000000

2.0000000

1.0000000

"Ac pivot ="

3. 4. -2. 9.

      1. 4.

    1. 3. 7.

"number of row swaps ="

"determinate of the pivoted A ="

-4.0000000

"L ="

  1.      0.   0.

0.3333333 1. 0.

0.6666667 5. 1.

"U ="

3. 4. -2.

0. -0.3333333 1.6666667

0. 0. -4.

"determinant of L ="

"determinant of U ="

4.

"determinant of L*U"

-4.0000000

"which is the same as determinate A"

"l ="

0.3333333 0.2 1.

  1.      0.    0.

0.6666667 1. 0.

"u ="

3. 4. -2.

0. -1.6666667 4.3333333

0. 0. 0.8

"determinant of l ="

"determinant of u ="

-4.0000000

"determinant of l*u"

-4.0000000

"which is the same as determinate A"

"Ac = "

3. 4. -2. 9.

0. -0.3333333 1.6666667 1.

0. 0. -4. -4.

"Solution from back substitution in L and U x3 = "

"Solution from the l-u decomposition ="

1.0000000

2.0000000

1.0000000

//Lecture 4.3: LU Decomposition and Partial Pivoting
//https://www.youtube.com/watch?v=smsE7iOlNj4&t=17s&ab_channel=MATLABProgrammingforNumericalComputation
//
//  x1+  x2+  x3 = 4
//2*x1+  x2+3*x3 = 7
//3*x1+4*x2-2*x3 = 9
//Using Matlab's powerful Linear Algebra:
clc
A = [1,1,1;3,4,-2;2,1,3];
//A = [3,4,-2;1,1,1;2,1,3]; //Already "pivoted" matrix form
//A=[-3 2 6;5 -1 5;10 -7 0];  //Just another matrix to play with...
disp("A = ", A);

disp("determinate of A =", det(A)); 
//We show how the determinant is
// affected with these calculations too.

b = [4;9;7];
//b = [9;4;7]; //goes along with the already "pivoted" A matrix
disp("b = ", b);

Ac=[A,b];
disp("Augmented Matrix A:b =",Ac)

[n,m]=size(Ac);
//disp("n,m "+string(n)+" and "+string(m));

L=eye(A);
//disp("L =",L);
x1 = A\b;
disp("Solution to Ax =b x1 =",x1);

//Calculate L matrix using Gauss Elimination
//  We're going to add Partial Pivoting where
//  we select the largest element in row 1 and exchange
//  that equation with the 1st row.

nswaps = 0;
for k = 1:1:n
// number of row swaps
    for i = k:1:n
        if (abs(Ac(i,k))>abs(Ac(k,k))) then
            temp = 0
            nswaps = nswaps + 1
            for j=1:1:m
                temp = Ac(k,j)
                Ac(k,j)=Ac(i,j)
                Ac(i,j)=temp
            end
//            disp("Ac =",Ac);
         end 
    end
end
disp("Ac pivot =",Ac);
Ad = Ac(1:3,1:3);
disp("number of row swaps =", nswaps)
disp("determinate of the pivoted A =", (-1)^nswaps*det(Ad));
//We did 'nswaps' row swaps and everytime we do a row swap, it changes 
//the sign of the determinant. We are doing LU decomposition on the 
//partial pivoted matrix not the original matrix.  We've coded in the 
//sign change so it should show no difference in the determinant on 
//the final determinant calculation.

//Calculate L matrix
for i = 1:1:n
    for j = i+1:1:n
        alpha = Ac(j,i)/Ac(i,i);
        L(j,i)=alpha;
//            disp("alpha ="+string(alpha));
        
//Rj = Rj-alphai,j*Ri where alphai,j = A(j,i)/A(i,i)
         Ac(j,:)=Ac(j,:)-alpha.*Ac(i,:);
//             disp("Ac = ",Ac);
    end
end

U = Ac(1:n,1:n);
disp("L =",L,"U =",U);
disp("determinant of L =" ,det(L));
disp("determinant of U =", det(U));
disp("determinant of L*U", (-1)^nswaps*det(L*U),"which is the same as determinate A");
//should show no change in determinant since we've only added or subtracted
//row operations with nswaps row swaps but no multiplication)

//LU decomposition
[l,u]=lu(A)  
//note we're doing the lu decomposition on the original
//matrix 
disp("l =",l);
disp("u =",u);
disp("determinant of l =" ,det(l));
disp("determinant of u =", det(u));
disp("determinant of l*u", det(l*u),"which is the same as determinate A");
//note: lu command doesn't change determinant when done on the original
//matrix

//Backsubstitution
x3 =zeros(m,1);  
//need 1 longer in vector so formula works
for i = n:-1:1
    x3(i)= (Ac(i,$)-(Ac(i,i+1:$)*x3(i+1:$)))/Ac(i,i);
end
disp("Solution from back substitution in L and U x3 = ",x3(1:n))

//Solve using the lu decomposition results
//l*y = b and u*x4 = y and we're going to use the function rref()
//solve the system equations
Af = 
rref
([l b]) 
//row reduce until we get the y vector in the last column
y = Af(1:$,$) 
// pick off the last column
Ag = 
rref
([u y]) 
//row reduce until we get the x4 solution in the last column
x4 = Ag(1:$,$) 
// pick off the last column
disp("Solution from the l-u decomposition =", x4)

//One more note - there is a Scilab function lusolve - this is for sparse
//matrices - it chokes on these non-sparse matrices.  Do not attempt to use.
//

r/scilab • • Dec 22 '25

Fifth Installment - SciLab Equivalent File for NPTEL "Matlab Programming for Numerical Computation - Subjects: Gaussian Elimination

3 Upvotes

Subject - Simultaneous Equations via Several Methods (Matrix Inversion, the backslash command, Gaussian Elimination, and row reduced echelon command).

As I mentioned that I would add some SciLab code I generated while doing a refresher on Numerical Computation (and gave me an opportunity to learn SciLab less the XCOS stuff).

The YouTube channel is referenced in the second line of the program just remember strip off the "//" (// is making it a comment line in SciLab) before the https://...

//https://www.youtube.com/watch?v=JY99xfMgwnk&t=12s&ab_channel=MATLABProgrammingforNumericalComputation

//https://www.youtube.com/watch?v=celUu5aY6_Q&ab_channel=MATLABProgrammingforNumericalComputation

is the link to the fifteenth class.

Output:

"A = "

    1. 3.

  1. 4. -2.

"b = "

4.

7.

9.

"x (via Ainverse*B) = "

1.0000000

2.

"x1 (via A\B) ="

1.0000000

2.0000000

1.0000000

"Ab (Augmented Matrix) ="

      1. 4.

    1. 3. 7.

  1. 4. -2. 9.

"Ab = "

      1. 4.

    1. 3. 7.

  1. 4. -2. 9.

"alpha =2"

"Ab = "

      1. 4.

  1. -1. 1. -1.

  2. 4. -2. 9.

"alpha =3"

"Ab (row reduced Augmented Matrix) = "

      1. 4.

  1. -1. 1. -1.

    1. -5. -3.

"alpha =-1"

"Ab = "

      1. 4.

  1. -1. 1. -1.

  2. 0. -4. -4.

"x2 (via Gaussian Elimination specific size matrix])="

"Ac = "

      1. 4.

    1. 3. 7.

  1. 4. -2. 9.

"alpha =2"

"Ac = "

      1. 4.

  1. -1. 1. -1.

  2. 4. -2. 9.

"alpha =3"

"Ac = "

      1. 4.

  1. -1. 1. -1.

    1. -5. -3.

"alpha =-1"

"Ac = "

      1. 4.

  1. -1. 1. -1.

  2. 0. -4. -4.

"x3 (via Gaussion Elimination generic size matrix) = "

"x4 (via reduced row echelon command) ="

Code:

//Linear Equations
//Lec 4.2 Guassian Elimination
//https://www.youtube.com/watch?v=JY99xfMgwnk&t=12s&ab_channel=MATLABProgrammingforNumericalComputation
//  x1+  x2+  x3 = 4
//2*x1+  x2+3*x3 = 7
//3*x1+4*x2-2*x3 = 9
//Using Matlab's powerful Linear Algebra:
A = [1,1,1;2,1,3;3,4,-2];
b = [4;7;9];
disp(,,"A = ",A);
disp(,"b = ",b);

x = inv(A)*b;
disp(,"x (via Ainverse*B) = ",x);
x1 = A\b;
disp(,"x1 (via A\B) =",x1);

//doing it the hard way
//create an Augmented Matrix
Ab = [A b];
disp(,,"Ab (Augmented Matrix) =",Ab);

Ac = Ab
[n,m]=size(Ab);

disp("Ab = ",Ab)
//Rj = Rj-alphai,j*Ri where alphai,j = A(j,i)/A(i,i)
//R = row elements
//With A(1,1) as a pivot element
alpha = Ab(2,1)/Ab(1,1);
disp("alpha ="+string(alpha));
//R2 = R2-alpha*R1
Ab(2,:)=Ab(2,:)-alpha.*Ab(1,:);
disp("Ab = ",Ab);
alpha = Ab(3,1)/Ab(1,1);
disp("alpha ="+string(alpha));
Ab(3,:)=Ab(3,:)-alpha.*Ab(1,:);

disp(,,"Ab (row reduced Augmented Matrix) = ",Ab);
//With A(2,2) as a pivot element
alpha = Ab(3,2)/Ab(2,2);
disp("alpha ="+string(alpha));
Ab(3,:)= Ab(3,:)-alpha.*Ab(2,:);
disp("Ab = ",Ab)
//Back substitution
x2 =[0;0;0];
x2(3)= Ab(3,4)/Ab(3,3);
x2(2)= (Ab(2,4)- x2(3)*Ab(2,3))/Ab(2,2);
x2(1)= (Ab(1,4)- x2(3)*Ab(1,3)-x2(2)*Ab(1,2))/Ab(1,1);
disp(,"x2 (via Gaussian Elimination specific size matrix])=", x2);
//using for statements and array commands
//Rj = Rj-alphai,j*Ri where alphai,j = A(j,i)/A(i,i)
//R = row elements
disp(,,"Ac = ",Ac);
for i = 1:1:n
    for j = i+1:1:n
        alpha = Ac(j,i)/Ac(i,i);
        disp("alpha ="+string(alpha));
        //Rj = Rj-alphai,j*Ri where alphai,j = A(j,i)/A(i,i)
         Ac(j,:)=Ac(j,:)-alpha.*Ac(i,:);
         disp("Ac = ",Ac);
    end
end
x3 =zeros(m,1);  //need 1 longer in vector so formula works
for i = n:-1:1
    x3(i)= (Ac(i,$)-(Ac(i,i+1:$)*x3(i+1:$)))/Ac(i,i);
end
disp(,,"x3 (via Gaussion Elimination generic size matrix) = ",x3(1:n))
// could do this solve another way to
x4 = 
rref
(Ab)(1:n,m);

disp(,,"x4 (via reduced row echelon command) =", x4(1:n))

r/scilab • • Dec 08 '25

Third Installment - SciLab Equivalent File for NPTEL "Matlab Programming for Numerical Computation

3 Upvotes

Subject - More on Numerical Integration Techniques

I mentioned that I would add some SciLab code I generated while doing a refresher on Numerical Computation (and gave me an opportunity to learn SciLab less the XCOS stuff).

The YouTube channel is referenced in the second line of the program just remember strip off the "//" (// is making it a comment line in SciLab) before the https://...

For example:  //https://www.youtube.com/watch?v=tByDeErG0Ic&t=13s&ab_channel=MATLABProgrammingforNumericalComputation is the link for the 12th class session depicted here.

Output:

Startup execution:

loading initial environment

"The integral of 2-x+ln(x) from 1 to 2 with step size 0.025 is ... "

"True Value = 0.8862944"

"Trap Value = 0.8862683 with Error = 0.000026"

"Simp Value = 0.8862944 with Error = 3.794D-09"

"Execution done."

Code:

//Numerical Integration
//Lec 3.4 Newton-Cotes Integration Formulae
//https://www.youtube.com/watch?v=tByDeErG0Ic&t=13s&ab_channel=MATLABProgrammingforNumericalComputation
//
// Integration is area under a curve
// Single Variable application
//     Trapezoid Rule -> Area = h*(f(x+h)+f(x))/2 = 2 points
//        Truncation Error - Order h^3
//     Simpson's 1/3rd Rule -> 3 point fit
//     area for "x to x+2h" =  h/3*(f(x)+4*f(x+h)+f(x+2h))
//        Truncation Error - Order h^5
//     Simpson's 3/8th Rule -> 4 point fit
//     area for "x to x+3h" = 3h/8*(f(x)+3*f(x+h)+3*f(x+2h)+f(x+3h))
//        Truncation Error - Order h^5
// Let's do an example
// integrate f(x)= 2 - x + ln(x)
// true value = 2*x - 0.5*x^2 + (x*ln(x)-x)
// simplifying = x - x^2/2 + x*ln(x)
// compare Trap and 1/3 Simpson rules
function result =f(x)
    result = 2-x+log(x);
endfunction

a = 1;
b = 2;
//number of steps
n = 40; 
//n needs to be even!
h = (b-a)/n;
disp("The integral of 2-x+ln(x) from "+ string(a)+" to "+string(b)+" with step size "+string(h)+" is ... ")
TruVal = (b-b^2/2+b*log(b))-(a-a^2/2+a*log(a));
disp("True Value = "+string(TruVal));
Trap_area = 0.;
Simp_area = 0.;
for i = 1:1:n;
    x = a + h.*(i-1);
    f1 = f(x);
    f2 = f(x+h);
    areaT = h*(f2+f1)/2;
    Trap_area = Trap_area + areaT;
end
errT = abs(TruVal - Trap_area);
disp("Trap Value = "+string(Trap_area)+" with Error = "+string(errT));
for i = 1:1:n/2;
    x = a + (2*h).*(i-1);
    f1 = f(x);
    f2 = f(x+h);
    f3 = f(x+(2*h));
    areaS = h/3*(f1+4*f2+f3);
    Simp_area = Simp_area + areaS;
end
errS = abs(TruVal - Simp_area);
disp("Simp Value = "+string(Simp_area)+" with Error = "+string(errS));

r/scilab • • Oct 06 '24

Scilab on Windows on Arm

3 Upvotes

Hi,

I'd love a version of Scilab for my Snapdragon X plus with Windows 11.

Is there any ongoing work with making it compatible?


r/scilab • • Sep 01 '24

Scilab on Linux

3 Upvotes

Can someone please help me. I don't know what to do. I just received a chromebook running in Linux. I'm stuck. I just downloaded the file from the site and it's a

.tar.xz

Now what do I do? If I click the file, it doesn't say download or something.


r/scilab • • Apr 26 '20

How can I open Scilab in my Windows console?

3 Upvotes

I would like to use it inside my integrated Visual Studio Code terminal but I don't know how to start it inside the console


r/scilab • • Apr 23 '20

Trying to Launch Scilab 6.1.0 on Fedora 30 aarch64 running inside a proot container on Termux?

3 Upvotes

Hello friends,

I am relatively new to fedora (have used ubuntu/debian before for testing). I am trying to get Scilab to launch but I am having issues. Just to make sure, the fedora image in running inside a container and currently is in root (no users defined yet). I have installed all the requisite packages I need.

The output I get is as followed

[root@localhost ~]# scilab
Could not access to the Main Scilab Class:
Exception in thread "main" java.lang.ExceptionInInitializerError
	at org.scilab.modules.localization.Messages.gettext(Unknown Source)
	at org.scilab.modules.commons.xml.XConfiguration.<clinit>(Unknown Source)
	at org.scilab.modules.core.Scilab.<clinit>(Unknown Source)
Caused by: java.lang.NullPointerException
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1847)
	at java.lang.Runtime.loadLibrary0(Runtime.java:871)
	at java.lang.System.loadLibrary(System.java:1124)
	at org.scilab.modules.localization.MessagesJNI.<clinit>(Unknown Source)
	... 3 more

Scilab cannot create Scilab Java Main-Class (we have not been able to find the main Scilab class. Check if the Scilab and thirdparty packages are available).
[root@localhost ~]# 

The output makes it seem JAVA is mising, but I have both openjdk1.8 and openjdk11 and latest openjdk installed. Switching between them doesn't change anything. whereis java and java -version all work as well as I checked premissions using ls -l dir and they all have the -xwrxr (makes sense since I am root atm). scilab-cli works fine. I cannot understand if its driver issue either, because I already have software 3D acceleration and FreeCAD, gmsh, paraview, blender and even octave work.

I don't know much about Java debugging so I am hoping I can get some pointers.


r/scilab • • Mar 24 '20

How to bring back variable browser embedded in the scilab workspace?

3 Upvotes

I am a beginner in scilab. I was trying to write certain functions, when accidentally i closed the variable browser. Now I have to manually open it from applications as a separate window which earlier was already embedded below my file browser. Any help on how to reverse it?


r/scilab • • Jan 03 '19

Install GNU Octave and Scilab on Windows 10 and Ubuntu 18.10 - MATLAB al...

Thumbnail
youtube.com
3 Upvotes

r/scilab • • Aug 03 '26

Data-Driven Science and Engineering Series - Installment 7 - PCA - Ovarian Cancer Example

2 Upvotes

In this installment, he shows a "How to use PCA on some Ovarian Cancer Markers

Scilab new commands Scatter3d plotting routine.

Link to the exact video.  https://www.youtube.com/watch?v=VqjJ5YYt78Y&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=23&t=5sFor Free Electronic Complete Textbook "FREE PDF for 2ND EDITION OF OUR BOOK!!!" https://x.com/eigensteve/status/1718004606901666063

Section 1.5 Principal component analysis (PCA)

Output: None

Plots:

Code:

clear, close, clc
disp("Data-Driven Science and Engineering -PCA Ovarian Cancer Example",string(datetime()))
//Section 1.5 Principal Component Analysis

//github databook_matlab code file
//https://github.com/dynamicslab/databook_matlab/blob/master/CH01/CH01_SEC05_2_OvarianCancer.m

//location of the OvarianCancer.mat file
//https://www.mathworks.com/matlabcentral/answers/1955269-function-for-visualisation-of-raw-data-ovarian-cancer

//load observation data - complete path
loadmatfile("C:\Users\MattheW\Documents\eBooks\ScILabNotes\DataScience\CODE\CH01\OvarianCancer.mat","-mat")

obs = X'

//Since the OvarianCancer.mat file did not have the "grp" data with
// it, I need to generate it based upon what he verbalized"
// First 113 were "Cancer" and the rest of the 216 were "Normal"
grp = cell(1,216);  // Note: I've hard coded the length of Y into grp!!!
for i = 1:113
  grp{i} = 'Cancer';
end
for i = 114:216
  grp{i} = 'Normal';
end
// I could save grp and Y as grp and obs respectively OvarianCancer.mat
// but not today!

[U,S,V] = svd(obs,'e');

scf(0)
clf
subplot(1,2,1)
semilogy(diag(S),'k-o','thickness',1.5)
//set(gca,'FontSize',13), 
//axis tight 
xgrid 
xlabel('r')
ylabel('Singular Value sigma_r')
subplot(1,2,2)
plot(cumsum(diag(S))./sum(diag(S)),'k-o','thickness',1.5)
//set(gca,'FontSize',13), axis tight, 
xgrid
xlabel('r')
ylabel('Cumulative Energy')
//set(gcf,'Position',[100 100 600 250])

scf(1)
clf

for i = 1:size(obs,1)
  x = V(:,1)'*obs(i,:)';
  y = V(:,2)'*obs(i,:)';
  z = V(:,3)'*obs(i,:)';
  if (grp{i}=='Cancer')
    scatter3d(x,y,z,"markerFaceColor","red");
  else
    scatter3d(x,y,z,"markerFaceColor","blue");
  end
end
xlabel('PCA 1')
ylabel('PCA 2 [Cancer - Red Dots Normal - Blue Dots]')
zlabel('PCA 3')
gca().rotation_angles =[85,25];
xgrid

r/scilab • • Jun 29 '26

Data-Driven Science and Engineering Series - Installment 1 - Singular Value Decomposition

2 Upvotes

We're starting a new series off of the Steve Brunton's Data-Driven Science and Engineering Series. He's definitely one of my favorite instructors when it comes to numerical and data analysis. First topic of the first short course series is Singular Value Decomposition (SVD). For this first example, it will be using the Image compression as an applied example.

To follow the series,

(As a motivator, I wish I'd had this method in my "Engineering Toolbox" since this technique helps one "sort the wheat from the chaff" when it comes to data reduction. I just never came across it in this form. I've done "Design of Experiments", produced "Statistical Models" and used techniques to figure out "what was important and what was not" but this SVD technique is very scalable.)

2) I do suggest you download the book and read the pertinent sections.

3) Download the data used for the short course. See below for location of the data.

4) For the Scilab code below to work, you'll need to use the Atoms Module Manager, navigate to Image Processing and install the Image Processing and Computer Vision Toolbox (IPCV 4.5.0.2).

5) I'm using Scilab 26.1.0 but have run this successfully on version 26.0.0.

I'm not sure how much coverage SciLab provides for this course but we'll see as we go along.

====================================

These lectures (videos) follow Chapter 1 from the book (freely available in electronic form):
"Data-Driven Science and Engineering: Machine Learning, Dynamical Systems, and Control" by Brunton and Kutz

Data and Python and MATLAB Native Code at https://databookuw.com/
Note: links to Matlab Code.zip + Data.zip on Main Page

Amazon: https://www.amazon.com/Data-Driven-Sc... //If you want hardcopy book.

Book Website(s): http://databookuw.com //For Videos and such

X posting "FREE PDF for 2ND EDITION OF OUR BOOK!!!" databookV2.pdf //For Free Electronic Complete Textbook

Chapters available at: http://databookuw.com/databook.pdf //Free Electronic Version

========================================

Videos

Singular Value Decomposition Short Course Overview
https://www.youtube.com/watch?v=gXbThCXjZFM&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=1

Singular Value Decomposition: Mathematical Overview Short Lecture 2
https://www.youtube.com/watch?v=nbBvuuNVfco&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=2

Singular Value Decomposition: Matrix Approximation Short Lecture 3
https://www.youtube.com/watch?v=xy3QyyhiuY4&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=3

Singular Value Decomposition: Dominant Correlations Short Lecture 4
https://www.youtube.com/watch?v=xy3QyyhiuY4&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=4

He discusses getting code and data files in this fifth lecture
https://www.youtube.com/watch?v=QQ8vxj-9OfQ&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=5

Steve Brunton already has two other short courses on Fourier Transforms and Sparsity & Compression which might be material for future series.

Output: None - Graphical Only

Plots:

Code:

/* Start Block Comment 

//Singular Value Decomposition Overview
//https://www.youtube.com/watch?v=gXbThCXjZFM&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=1

//These lectures follow Chapter 1 from:
//"Data-Driven Science and Engineering: Machine Learning, Dynamical Systems, and Control"  by Brunton and Kutz

//Amazon: https://www.amazon.com/Data-Driven-Sc... //If you want hardcopy book.

//Book Website: http://databookuw.com //Free Electronic Web-based Version

//Chapters available at: http://databookuw.com/databook.pdf //Free Electronic Version

 //For Free Electronic Complete Textbook "FREE PDF for 2ND EDITION OF OUR BOOK!!!" https://x.com/eigensteve/status/1718004606901666063

//Brunton Website: http://eigensteve.com

//Singular Value Decomposition: Mathematical Overview Short Lecture 2
//https://www.youtube.com/watch?v=nbBvuuNVfco&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=2

//Singular Value Decomposition: Matrix Approximation Short Lecture 3
//https://www.youtube.com/watch?v=xy3QyyhiuY4&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=3

//Singular Value Decomposition: Dominant Correlations Short Lecture 4
//https://www.youtube.com/watch?v=xy3QyyhiuY4&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=4

//He discusses getting code and data files in this fifth lecture
//https://www.youtube.com/watch?v=QQ8vxj-9OfQ&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=5

//Data and Python and MATLAB Native Code at https://databookuw.com/
//links to Code.zip + Data.zip on Main Page 

*/ //End of Block Comment

clear, close, clc

disp("Data-Driven Science and Engineering - Singular Value Decomposition Image Processing Example ",string(datetime()))

//Need to load this atoms module into SciLab for imread, imshow, rgb2gray

atomsLoad("IPCV")  //Once loaded into Scilab, this command line is unnecessary

//Use full file path to your unzipped data file...
A=imread('C:/Users/Matthew/Documents/eBooks/ScILabNotes/SVD_PCA_Course/DATA/dog.jpg');
X=double(rgb2gray(A)); // Convert RBG->gray, 256 bit->double.
nx = size(X,1); ny = size(X,2);

scf()
clf
subplot(2,2,1)
imshow(uint8(X));
title('Original in Gray Scale');

[U,S,V] = svd(X);

plotind=2;
for r=[5 20 100];  // Truncation value
    Xapprox = U(:,1:r)*S(1:r,1:r)*V(:,1:r)'; 
    // Approx. image
    
    subplot(2,2,plotind)
    plotind = plotind+1;
    imshow(uint8(Xapprox))
    title(['r=', msprintf('%d',r),', ',msprintf('%2.2f',100*r*(nx+ny)/(nx*ny)),' % storage']);
end

// f_ch01_ex02_2
scf();
clf;
subplot(1,2,1), semilogy(diag(S),'k')
xgrid
xlabel('r')
ylabel('Singular Value sigma_r')
subplot(1,2,2), plot(cumsum(diag(S))/sum(diag(S)),'k')
xgrid
xlabel('r')
ylabel('Cumulative Energy')

r/scilab • • May 25 '26

Twenty Seventh Installment - Boundary Value Problem Solving Nonlinear Equation Example and "Mixed Boundary" Conditions

2 Upvotes

In this edition we look at solving Boundary Value Problems with Nonlinear Equation and one with a mixed boundary (parameter is held at a value in one location and it's derivative is held at a value at another location.

Note: The program has two major blocks and in it's current configuration (the first major block (the Nonlinear Example) runs and the second example (the Mixed Boundary Condition Example) is "block commented" out.

Link to the specific lecture (note the urls cover the same material) and one might find it useful to use the NPTEL Link:

https://www.youtube.com/watch?v=chChbuP-hRk&ab_channel=NPTEL-NOCIITM
https://nptel.ac.in/courses/103106118

He does not show the MATLAB code and the solution. He moves onto a new subject in the next section. I had to do the solution cross-checking myself.

  1. The nonlinear problem has reasonably good match of solutions. Derivatives will show more variation.

  2. The mixed boundary problem checks out perfectly with the analytical expectations.

  3. Anytime you're working with unknown results...always do at least 2 different analytical techniques and try to reconcile the differences.

Output:

"Non-linear example"
   "Extensions of ODE-BVP"
  "2026-05-23 16:29:45.484"
  "Pos x Temp T     TempGrad   FiniteDiff  FiniteDiff_dTdx"
   0.     100.       -118.64825   100.     -133.1775
   0.05   94.21345   -112.86181   93.3411  -126.7985
   0.1    88.709121  -107.35721   87.3202  -114.9776
   0.15   83.47325   -102.12134   81.8434  -104.8836
   0.2    78.492727  -97.141089   76.8318  -96.249  
   0.25   73.755099  -92.403352   72.2185  -88.8566 
   0.3    69.24853   -87.896868   67.9461  -82.5285 
   0.35   64.961754  -83.60988    63.9656  -77.118  
   0.4    60.884054  -79.532179   60.2343  -72.5033 
   0.45   57.005218  -75.653556   56.7153  -68.5822 
   0.5    53.31555   -71.963803   53.3761  -65.2689 
   0.55   49.80583   -68.454149   50.1884  -62.4901 
   0.6    46.467286  -65.11544    47.1271  -60.1831 
   0.65   43.29157   -61.939723   44.1701  -58.2936 
   0.7    40.27073   -58.919049   41.2977  -56.7738 
   0.75   37.397213  -56.045466   38.4927  -55.5814 
   0.8    34.663841  -53.312145   35.7396  -54.6782 
   0.85   32.06378   -50.711955   33.0249  -54.0293 
   0.9    29.590529  -48.238705   30.3367  -53.6024 
   0.95   27.237897  -45.886201   27.6647  -53.3668 
   1.     25.        -43.648253   25.      -53.2931 
 
"Mixed Boundary Example"

  "Extensions of ODE-BVP"
  "2026-05-23 11:07:30.378"
  "Pos x Temp T     TempGrad     Analytical Analytical Derivative"
   0.    100.       -144.60414   100.       -144.60414
   0.1   86.947997  -117.30547   86.947997  -117.30547
   0.2   76.382185  -94.714688   76.382185  -94.714689
   0.3   67.87852   -75.925137   67.87852   -75.925138
   0.4   61.095721  -60.182729   61.095721  -60.182729
   0.5   55.76157   -46.855664   55.76157   -46.855664
   0.6   51.661989  -35.409083   51.661989  -35.409082
   0.7   48.632447  -25.383591   48.632447  -25.383591
   0.8   46.551358  -16.376832   46.551358  -16.376833
   0.9   45.335201  -8.0273338   45.335201  -8.0273337
   1.    44.935167  -8.882D-16   44.935167   0.        

Graphs:

Code:

//Lecture 10.4: Extensions of ODE-BVP
//https://www.youtube.com/watch?v=chChbuP-hRk&ab_channel=NPTEL-NOCIITM
//https://nptel.ac.in/courses/103106118
//
disp("Extensions of ODE-BVP",string(
datetime
()))
//
// Extension 1) Non linear equation
// Extension 2) Mixed Boundary Condition
//
//Boundary Value Problems
//Example Heated Fin/Rod
//d2T/dx^2=gamma1*(T-25);
//
//Two easy causes of non-linearity are: 
//  add Radiation to the right hand side.
//  heat loss = sigma1*epsilon*((T+273)^4-(Ta+273)^4))
//
//  gamma1 is a function of T so convection term is 
//  gamma1(T)(T-25) where typically gamma1 = gamma0*T^0.75
//  in our example, we use gamma1= gamma0*T^1 due
//  numerical issues using 0.75 (which I do not want to investigate
//  at this time).
//
//  so in our case "ff = gamma1*(u(1)-Ta)"
//  becomes     "ff = gamma0*u(1)*(u(1)-Ta)"
//  and    "dff = [gamma0*(2*u(1)-Ta),0];"
//  using gamma0 = 0.04 to keep solution close to prior gamma1 of 4
//
// Mixed Boundary Condition
// 1) Dirichlet T(0)=100, T'(1)=b(T)
// 2) Nuemann T'= some value
// 3) Mixed Phi(T,T')=0
//
// First Case Non-Linear Function...
// T at wall = 100 C
// T at end (x=1) = 25 C
// 
// 0 = k*d2T/dx^2-h*av*(T-Ta)
// where k = thermal conductivity of rod
//       h = convection coefficient of conditions
//       av = surface area of the rod
//       Ta = Temperature of surrounding atmospere
// d2T/dx^2 = (h*av/k)*(T-Ta); 
// d2T/dx^2 = gamma1*(T-Ta);
// 
//Boundary Condition 1: 0 = g1(ya,yb)=> T(x=0)-100 = 0
//B C 2: T(x=1)-25 = 0

// The external functions
// These functions are called by the solver with zu=[u(x);u'(x);u''(x);u'''(x)]

// - The function which computes the right hand side of the differential equation
funcprot(0);
function ff=
f
(x, u)
//Define Constants
    Ta = 25;
    gamma0=0.04; 
    ff = gamma0.*u(1).*(u(1)-Ta);//right hand side of the differential equation
endfunction

// - The function which computes the derivative of f with respect to u
function dff=
df
(x, u)
   Ta =25 
   gamma0=.04;
   dff = [0, gamma0.*(2.*u(1)-Ta)];
endfunction

function [gg]=
g
(i, u)
    gg=[u(1)-100,u(1)-25];//Boundary Value Conditions 
// T(x=0)=100, T(x=1)=25 ,u(1)=T, u(2)=dT/dx, u(n+1)=dnT/dx^n (but not 
// used) since order is only 2....
    gg=gg(i);
endfunction

function [dgg]=
dg
(i, u)
    dgg = [1,0;1,0] //must be consistent with the boundary conditions
//set in subroutine g so if you have two boundary conditions on 
// u(1), it is [1,0;1,0]...if you have one condition on u(1) at the first 
// location and u(2) on the other, it needs to be [1,0;0,1] with gg 
// having the u(1) condition first.  If the u(1) condition is at the second
// position instead and the first has a u(2), it would look 
// like [0,1;1,0]...
//
    dgg=dgg(i,:);
endfunction

function [u0, du0]=
guess
(x)
    u0=100;
    du0=-100;
endfunction

//
n = 1; //One differential equation
m = 2;// Second order differential equation
xL=0;
xR = 1;
Dx=0.05;
x = [xL:Dx:xR];
fixpnt = [];
zeta = [xL,xR];
ipar=zeros(1,11);
ipar(3)=1;
ipar(4)=2;
ipar(5)= 10000;
ipar(6) = 2000;
ipar(7)=1;
ltol =[1,2];
tol = [1e-5,1e-5];

u = bvode(x,n,m,xL,xR,zeta,ipar,ltol,tol,fixpnt,
f
,
df
,
g
,
dg
,
guess
);
FiniteDifferenceMethodSoln = [
100.000
93.3411
87.3202
81.8434
76.8318
72.2185
67.9461
63.9656
60.2343
56.7153
53.3761
50.1884
47.1271
44.1701
41.2977
38.4927
35.7396
33.0249
30.3367
27.6647
25.0000
];
FD_dTdx = [
-133.1775
-126.7985
-114.9776
-104.8836
-96.2490
-88.8566
-82.5285
-77.1180
-72.5033
-68.5822
-65.2689
-62.4901
-60.1831
-58.2936
-56.7738
-55.5814
-54.6782
-54.0293
-53.6024
-53.3668
-53.2931
]
allData = [x' u' FiniteDifferenceMethodSoln FD_dTdx]
disp("Pos x Temp T     TempGrad   FiniteDiff  FiniteDiff_dTdx", allData);   
//Plotting
scf
(0);
clf
;
//axis y1
c = color("black")
c1 = color("blue")
c2 = color("green")
c3 = color("purple")
plot2d(x,u(1,:),style=c3) //,'x','y(x)',"Nonlinear Function")
h1=
gca
();
plot2d(x,FiniteDifferenceMethodSoln,style=c1);//black =, blue = 2, green =3, cyan = 4, red = 5 ;//Commented out for last example
h1.font_color=c;
h1.children(1).children(1).thickness =2;
xlabel
("$X$","FontSize",3)
ylabel
("$T(x) $","FontSize",3);
title
("$\textbf{Nonlinear Function}$","FontSize",4);
legend
("BVODE","Finite Difference Values",3)
xgrid;
scf
(1);
clf
;
c = color("black")
c1 = color("blue")
c2 = color("green")
c3 = color("purple")
plot2d(x,u(2,:),style=c3)//,'x','dy/dx',"BVODE 2nd order solution")
plot2d(x,FD_dTdx,style=c1);//black =, blue = 2, green =3, cyan = 4, red = 5 ;//Commented out for last example
h1=
gca
();
h1.font_color=c;
h1.children(1).children(1).thickness =2;
xlabel
("$X$","FontSize",3)
ylabel
("$dT/dx$","FontSize",3);
title
("$\textbf{Nonlinear Function}$","FontSize",4);
legend
("BVODE","Finite Difference Values",4)
xgrid
//End of First Example Block
/*
//
// Second Case Mixed Boundary...
//
//Boundary Value Problems
//Example Heated Fin/Rod
//d2T/dx^2=gamma*(T-25);
// T at wall = 100 C
// replace "T at end (x=1) = 25 C" with "dT/dx(x=1) = 0" 
// so end is insulated
// 
// 0 = k*d2T/dx^2-h*av*(T-Ta)
// where k = thermal conductivity of rod
//       h = convection coefficient of conditions
//       av = surface area of the rod
//       Ta = Temperature of surrounding atmospere
// d2T/dx^2 = (h*av/k)*(T-Ta); 
// d2T/dx^2 = gamma1*(T-Ta);
// 
//Boundary Condition 1: 0 = g1(ya,yb)=> T(x=0)-100 = 0
//B C 2: dT/dx(x=1) = 0

// The external functions
// These functions are called by the solver with zu=[u(x);u'(x);u''(x);u'''(x)]

// - The function which computes the right hand side of the differential equation
funcprot(0);
function ff=
f
(x, u)
//Define Constants
    Ta = 25;
    gamma1=4;
    ff = gamma1*(u(1)-Ta);//right hand side of the differential equation
endfunction

// - The function which computes the derivative of f with respect to u
function dff=
df
(x, u)
   gamma1=4;
   dff = [gamma1,0];
endfunction

function [gg]=
g
(i, u)
//change  gg=[u(1)-100,u(2)-25];//Boundary Value Conditions 
// to
          gg=[u(1)-100,u(2)-0];//Boundary Value Conditions
// T(x=0)=100, T'(x=1)= 0 (Insulated End)
// 
// T(x=0)=100, T(x=1)=25 ,u(1)=T, u(2)=dT/dx, u(n+1)=dnT/dx^n (but not 
// used) since order is only 2....
    gg=gg(i);
endfunction

function [dgg]=
dg
(i, u)
// change    dgg = [1,0;1,0]
//to
   dgg = [1,0;0,1]
//must be consistent with the boundary conditions
//set in subroutine g so if you have two boundary conditions on 
// u(1), it is [1,0;1,0]...if you have one condition on u(1) at the first 
// location and u(2) on the other, it needs to be [1,0;0,1] with gg 
// having the u(1) condition first.  If the u(1) condition is at the second // position instead and the first has a u(2), it would look 
// like [0,1;1,0]...
//
    dgg=dgg(i,:);
endfunction

function [u0, du0]=
guess
(x)
    u0=100;
    du0=-100;
endfunction

//
n = 1; //One differential equation
m = 2;// Second order differential equation
xL=0;
xR = 1;
Dx=0.1;
x = [xL:Dx:xR];
fixpnt = [];
zeta = [xL,xR];
ipar=zeros(1,11);
ipar(3)=1;
ipar(4)=2;
ipar(5)= 10000;
ipar(6) = 2000;
ipar(7)=1;
ltol =[1,2];
tol = [1e-8,1e-8];

u = bvode(x,n,m,xL,xR,zeta,ipar,ltol,tol,fixpnt,
f
,
df
,
g
,
dg
,
guess
);

//Analytical Answer (Insulated End)
T = 25+75*cosh(2*(1-x))/cosh(2);
dTdx = 150.*cosh(2.*x).*(tanh(2.*x)-tanh(2));
allData = [x' u' T' dTdx']
disp("Pos x Temp T     TempGrad     Analytical Analytical Derivative", allData);   
//Plotting
scf
(0);
clf
;
//axis y1
c = color("black")
c1 = color("blue")
c2 = color("green")
c3 = color("purple")
plot2d(x,u(1,:),style=c1) //,'x','y(x)',"Example Heated Fin/Rod with Insulated End")
h1=
gca
();
plot2d(x,T+1,style=c3);//black =, blue = 2, green =3, cyan = 4, red = 5 ;//Commented out for last example
h1.font_color=c;
h1.children(1).children(1).thickness =2;
xlabel
("$X$","FontSize",3)
ylabel
("$T(x) $","FontSize",3);
title
("$\textbf{Example\ Heated\ Fin/Rod\ with\ Insulated\ End}$","FontSize",4);
legend
("BVODE","Analytical Values+0.01",2)
xgrid;
scf
(1);
clf
;
c = color("black")
c1 = color("blue")
c2 = color("green")
c3 = color("purple")
plot2d(x,u(2,:),style=c1)//,'x','dy/dx',"Example Heated Fin/Rod with Insulated End")
plot2d(x,dTdx'+1,style=c3);//black =, blue = 2, green =3, cyan = 4, red = 5 ;//Commented out for last example
h1=
gca
();
h1.font_color=c;
h1.children(1).children(1).thickness =2;
xlabel
("$X$","FontSize",3)
ylabel
("$dT/dx$","FontSize",3);
title
("$\textbf{Example\ Heated\ Fin/Rod\ with\ Insulated\ End}$","FontSize",4);
legend
("BVODE","Analytical Values+1",4)
xgrid
*/

r/scilab • • Apr 07 '26

👋 Welcome to r/scilab - Introduce Yourself and Read First!

2 Upvotes

Hey everyone! I'm u/mrhoa31103, the moderator of r/scilab a recovering subreddit from the great Apollo incident exodus.

This is our new home for all things related to Scilab. We're excited to have you join us!

What to Post
Post anything that you think the community would find interesting, helpful, or inspiring. Feel free to share your thoughts, photos, or questions about Scilab and its applications.

Community Vibe
We're all about being friendly, constructive, and inclusive. Let's build a space where everyone feels comfortable sharing and connecting. I'm running kind of a blog on Scilab programs so people can see full up working programs but do not feel your posts have to live up those formats. If you have a quick question, "how do you do this or that?" or found a neat application, share it.

Thanks for being part of the community. Together, let's make r/scilab amazing.


r/scilab • • Mar 23 '26

Eighteenth Installment - Solving Second Order, Constant Coefficient Ordinary Differential Equations with Initial Values. Including Code to Generate a Phase Portrait of the Problem.

2 Upvotes

In this edition we solve the second order, constant coefficient (the mass, spring, damper problem) ODE using a pair of first order ODE's as a system of ODEs.

Link to the specific lecture:

https://www.youtube.com/watch?v=i5Ton-G9zNs&ab_channel=MATLABProgrammingforNumericalComputation

I've included some additional code to create the "Phase Portrait" of the same problem. The additional code was not part of the original course but covered in an advanced Engineering Mathematics course ME564 by Steve Brunton.

Link to the specific lecture:

https://www.youtube.com/watch?v=i5Ton-G9zNs&ab_channel=MATLABProgrammingforNumericalComputation

Output: First Couple of Lines

 "Ordinary Differential Equations- Multi-Variable ODE Methods of Integration"
  "2026-03-16 10:25:24.166"
  "time       velocity    displacement"
   0.          0.          1.       
   0.2020202  -0.285297    0.9705457
   0.4040404  -0.526656    0.8876959
   0.6060606  -0.7143162   0.7613806
   0.8080808  -0.8425653   0.6030931
   1.010101   -0.9096817   0.4250752
   1.2121212  -0.9176505   0.2395353
   1.4141414  -0.8716928   0.0579447
   1.6161616  -0.779652   -0.1095563
   1.8181818  -0.6512877  -0.2546176
   2.020202   -0.497526   -0.3709916
   2.2222222  -0.3297147  -0.4546928
   2.4242424  -0.1589252  -0.5040112
   2.6262626   0.0046624  -0.5193931
   2.8282828   0.1522629  -0.503208 
   3.030303    0.2768618  -0.4594262
   3.2323232   0.3734507  -0.3932356
   3.4343434   0.4391251  -0.3106235
   3.6363636   0.4730505  -0.2179536
   3.8383838   0.476312   -0.1215607
   4.040404    0.4516667  -0.0273866
   4.2424242   0.4032234   0.0593262
   4.4444444   0.336075    0.1342711
   4.6464646   0.2559087   0.194237 
   4.8484848   0.1686192   0.2371892
   5.0505051   0.0799477   0.262274 

Graphs:

Code from Original Course:

//Ordinary Differential Equations Initial Value Problems
//Lec 8.1 Solving Multi-Variable ODE Methods 
//https://www.youtube.com/watch?v=i5Ton-G9zNs&ab_channel=MATLABProgrammingforNumericalComputation
//
//
disp("Ordinary Differential Equations- Multi-Variable ODE Methods of Integration",string(
datetime
()))
//
// model mass-spring-damper
// m*d^2x/dt^2 + c*dx/dt +kx = 0
// u/x(0)=1
//
// create 2 - first order equations
//  d^2/dt^2 = dv/dt where v = velocity
//  m*dv/dt +c*v + kx = 0
//  v = dx/dt where x = displacement
//
// dx/dt = v            x(0)=1
// dv/dt = -(c*v+k*x)/m v(0)=0
//
// output vector y =[x;v]
// derivative vector = [v; -(c*v+k*x)/m]
//
function [results]=
derivativeVector
(t, y);
//constants of the system
    c=5;
    k=15;
    m=10;
//extraction of variables
    x = y(1);
    v = y(2);
    results(1,1)=v;
    results(2,1)=-(c*v+k*x)/m
endfunction

y0=[1;0];
t0 = 0;
n = 100;
t = linspace(0,20,n)';
//y = ode("rkf",y0,t0,t,derivativeVector)
y = ode("adams",y0,t0,t,
derivativeVector
)
// Other types of integration "adams","stiff","rk","rkf","fix","discrete"
// and "root"
// Everything works but "discrete" for this problem
// discrete is a sampling routine!!
y = y';//Transform from row vectors to column vectors
x_vector =y(:,1);
v_vector=y(:,2);
output = [t,v_vector,x_vector]
disp("time       velocity    displacement",output)
// Note: Very accurate and uses Runge-Kutta 4,5 uses
// variable step size and just reports out at time t vector
//
// Other types of integration "adams","stiff","rk","rkf","fix","discrete"
// and "root"
// Everything works but "discrete" for this problem
// investigate later...
//plotting example fancy output two y axes...
scf
(0);
clf
;
//axis y1
c = color("red")
c2 = color("black")
plot2d(t,x_vector,style=c);//black =, blue = 2, green =3, cyan = 4, red = 5
h1=
gca
();
h1.font_color=c;
h1.children(1).children(1).thickness =2;
xlabel
("$t(Seconds)$","FontSize",3)
ylabel
("$X(t)$","FontSize",3);

xgrid
h1=
gca
();
h1.font_color=c;
h1.children(1).children(1).thickness =2;
xlabel
("$t(Seconds)$","FontSize",3)
ylabel
("$Displacement$","FontSize",3,"Color",5);
title
("Displacement and Velocity Versus Time","color","black");
xgrid
// Axis y2
c=color("blue");
c1 = color("red")
h2=newaxes();
h2.font_color=c;
plot2d(t,v_vector,style=c)
h2.filled="off";
h2.axes_visible(1)="off";
h2.y_location="right";
h2.children(1).children(1).thickness=2;
ylabel
("$Velocity$","FontSize",3,"color",c)
xgrid

Code from a Second Course that shows how to generate the Derivative Arrow Plot:

//Lecture 8 ME564 Matrix Systems of First Order Equations using eigenvalues
// and Eigencectors.
clc;clear "all"
disp("ME564 Lecture 8 Matrix Systems of First Order Equations",string(
datetime
()))
A=[0 1;-1.5 -.5]
y0=[1.;0.0];
t0 = 0; //Tstart
tend = 20; //Tend
t= t0:.05:tend';//time vector
//define derivativeF with an inline function 
deff
("[results]=derivativeF1(t,y)","results= [A*y]")
//equivalent to MATLAB ODE45 is the following command in SciLab
//there is more options and outputs available - see the help file.
y = ode("fix",y0,t0,t,
derivativeF1
);
//disp("y=",y')
//Note: rkf has a limited number of steps like 160 maximum in this case
// use "stiff","adams","rk","fix" to move beyond 160 steps...
// according to the documentation fix is the same solver as rkf
// works without restriction
scf
(1);
clf
;
plot
(y(1,:),y(2,:),"-r","thickness",2)
xlabel
("$Position$","FontSize",3)
ylabel
("$Velocity$","FontSize",3);
title
("$ME564\ LECTURE\ 8$","color","black");
legend
('numerical',3)
xgrid
//
// the following plots the derivative arrows onto the plot
// showing the solution following the arrows.
//
xf= min(y(1,:)):(max(y(1,:))-min(y(1,:)))/10:max(y(1,:));
yf= min(y(2,:)):(max(y(2,:))-min(y(2,:)))/10:max(y(2,:));
fchamp
(
derivativeF1
,0,xf,yf)
xgrid
//
//

scf
(2);
clf
;
plot
(t,y(1,:),'-r','thickness',2)
xgrid
xlabel
("$t(Seconds)$","FontSize",3)
ylabel
("$X(t)$","FontSize",3);
title
("$ME564\ LECTURE\ 8\ Second\ Order\ Systems$","color","black");
legend
('numerical')

r/scilab • • Feb 24 '26

Just a quick note - Not part of the series - Do you know you can extend SciLab's capabilities with Toolboxes similar to MATLAB Toolboxes and GNU Octave's Packages - See the ATOMS: Homepage

2 Upvotes

See what's available on ATOMS: Homepage and links repeated in the wiki/resource page.

I've personally had to use the Differential Equations, Sci-Sundials package since the regular ODE solvers in SciLab do not handle complex variables. I was doing some Fourier Transform stuff at the time.

You can also get to the additional capabilities through the menu ribbon see "Applications/Module-manager - ATOMS"


r/scilab • • Feb 02 '26

Eleventh Installment - SciLab Equivalent File for NPTEL "Matlab Programming for Numerical Computation - Subject: Solving Non-linear, Single Variable, Algebraic Equations via Newton-Raphson Method.

2 Upvotes

Link to the specific lecture (lecture 22) . Program also demonstrates the use of functions, for loops and while loops.

https://www.youtube.com/watch?v=bsgPR0lWiTg

Next week teaser: Multi-variable solving...

Sample Output:

"Newton-Raphson in Single Variable"

"2026-01-30 14:44:55.088"

" "

" "

"for loop"

"x =0.05"

"x =0.1050385"

"x =0.1471105"

"x =0.1580946"

"x =0.1585934"

"x =0.1585943"

"x =0.1585943"

"x =0.1585943"

"x =0.1585943"

"x =0.1585943"

"x =0.1585943"

" "

" "

"for loop"

"x =0.5"

"x =-0.3068528"

"x =-0.0425902+%i*0.737655"

"x =0.9880806+%i*0.272197"

"x =1.8457458-%i*3.5312688"

"x =2.8269442-%i*0.520963"

"x =3.1228966+%i*0.0222697"

"x =3.1461957-%i*0.0000779"

"x =3.1461932-%i*2.897D-11"

"x =3.1461932+%i*1.926D-21"

"x =3.1461932"

" "

" "

"while loop max iterations = 15"

"count = 1 x =0.1583153 err =0.0083153"

"count = 2 x =0.158594 err =0.0002788"

"count = 3 x =0.1585943 err =0.0000003"

The Code Below:

//Lecture 5.4 Non-linear Algebraic Equations
//Newton-Raphson (Single Variable)
//https://www.youtube.com/watch?v=bsgPR0lWiTg
//
//
// f(x)=2-x+ln(x)
// f'(x)=-1+1/x
//   (i+1)    (i)    (i)     (i)
// x       = x   -f(x  )/f'(x  )
//                         2
//         (i+1)    |  (i)|
//where Err       = |E    |   a "quadratic rate of convergence"
//
function [result]=
equation
(x);
    result = 2-x+log(x);
endfunction

function [result]=
derivative
(x);
    result = -1 + 1/x;
endfunction

disp("Newton-Raphson in Single Variable",string(
datetime
()))
disp(" "," ","for loop")
iteration_count = 10;
x=.05;
disp("x =" +string(x));
for i = 1:1:iteration_count;
    x = x - 
equation
(x)/
derivative
(x);
    disp("x =" +string(x)); 
end
disp(" "," ","for loop")
x=.5;
disp("x =" +string(x));
for i = 1:1:iteration_count;
    x = x - 
equation
(x)/
derivative
(x);
    disp("x =" +string(x)); 
end
disp(" "," ","while loop max iterations = 15")
xold=.15;//Initial guess at solution
count = 0; //keeping track of the iteration count
err =1; //initial err to kick off while loop
errTol = 0.00001; //error tolerance
while (norm(err)>errTol);  
    x = xold - 
equation
(xold)/
derivative
(xold);
    err = (x-xold);
    count = count+1;
    xold = x;
    disp("count = "+string(count)+" x =" +string(x)+" err =" +string(norm(err)));
    if count>15 then;
         break;
    end;
end

r/scilab • • Jan 11 '26

Eighth Installment - SciLab Equivalent File for NPTEL "Matlab Programming for Numerical Computation - Subject: Solving Nonlinear Equations using Bisection method - An easy one with some demonstrated plotting commands

2 Upvotes

Subject: Solving Nonlinear Equations using Bisection method

-----------------------------------------------------------------------------------------

Link to the specific lecture (lecture 19).

https://www.youtube.com/watch?v=5W46xcVInL8&t=51s&ab_channel=MATLABProgrammingforNumericalComputation

Note: Even though x range was 0.1 to 4, the program only picks up the first solution since it is simply coded to find a solution, not all solutions.

Output:

"Bisection method solution for y=2-x+ln(x) ="

0.1585943

"equation value (theoretically should be 0 for the root)="

7.230D-11

To find the other solution to the problem, one must change the x range to eliminate the first solution to x = 1 to 4.

"Bisection method solution for y=2-x+ln(x) ="

3.1461932

"equation value (theoretically should be 0 for the root)="

-2.584D-11

To understand whether you have more solutions, the only easy way is to graph the function and look for x-axis crossing points.

//Lec 5.1 Nonlinear Equation in Single Variable
//Solving Nonlinear Equations - BiSection Method.
//https://www.youtube.com/watch?v=5W46xcVInL8&t=51s&ab_channel=MATLABProgrammingforNumericalComputation
//
// Solve 2-x+ln(x)=0 using the bisection method
// Start with a segment where you think the solution exists
// Look for a sign change at the ends of the segment.
// Bisect the segment and keep the segment that contains the 
// sign change.  Do it until the accuracy is achieved.
// 
//define the function
function [results]=
equation
(x)
    results = 2.0-x+log(x);
// note log is natural log (aka ln)    
//Theoretical roots are very complicated Inverse Lambert Function
//and Lambert Function solutions (approximately numerically)
// at .1585943, 3.1415926
endfunction

//Always graph your function to understand where the roots lie.
//   Below is the code for a quick plot with some bells and 
//   whistles (title, labels, grid, and box frame) to make it
//   look good.
//
x=linspace(0.1,4,1000);
results=
equation
(x);
plot2d(x',results');
title
("Y = 2 - X+ln(X)")
xlabel
("X")
ylabel
("Y")
xgrid;
gca
().box="on";

//define the segment where you think a solution lies
x = [0.1,3] //Grabs the first root
//x = [1,4] //Grabs the second root
//
while (x(2)-x(1))>1e-10;
    y = 
equation
(x);
//    disp("y =", y);
    xmid = (x(1)+x(2))/2
    ymid = 
equation
(xmid)
    
    if (sign(ymid)==sign(y(1))) then
        x(1)=xmid
    else
        x(2)=xmid
    end
end
disp("Bisection method solution =" ,xmid)
z = 
equation
(xmid);
disp("equation value (theoretically should be 0 for the root)=",z)

r/scilab • • Nov 23 '25

SciLab equivalents to Matlab for NPTEL Computer Numerical Analysis Course

2 Upvotes

Here's the course outline - I have files for every one except for those that didn't have any coding that session. The dropped in the 17th one as an example of the files (totally random selection).

(43) MATLAB Programming for Numerical Computation NPTEL - YouTube

Any requests for the next file to drop in. You can also tell me to stop spamming you but my intent is to show how to do practical things in SciLab without having to spend hours upon hours looking up the syntax (which is definitely different than MATLAB in places).

//NPTEL Computer Numerical Analysis Course

//1 = "Array Operations"

//2 = "Loops and Execution Control"

//3 = "Scripts and Functions in SciLab"

//4 = "Plotting and Output in SciLab"

//5 = "Ball Travel Example -with Plotting added"

//6 = "none"

//7 = "Round-Off Errors and Iterative Methods"

//8 = "Errors and Approximations: Step-Wise & Error Propagation"

//9 = "Differentiation in a Single Variable"

//10= "Higher Order Differentiation Formulae"

//11= "Numerical Differentiation:Partial Derivatives - MultiVariable Equations"

//12= "Numerical Integration: Newton-Cotes Integration Formulae"

//13= "Numerical Integration: Matlab Functions and Application"

//14= "Basics of Linear Algebra (Linear Equations)"

//15= "Linear Equations: Guassian Elimination"

//16= "LU Decomposition and Partial Pivoting"

//17= "Tri-Diagonal Matrix Algorithm"

//18= "Gauss Siedel Method - Iterative Methods for Solving Simultaneous Equations"

//19= "Nonlinear Equation in Single Variable Solving Nonlinear Equations - BiSection Method."

//20= "More on Non-Linear Algebraic Equations: Using Matlab Function fzero and fsolve"

//21= "Fixed Point Iteration in single variable functions"

//22= "Non-linear Algebraic Equations: Newton-Raphson (Single Variable)"

//23= "Non-linear Algebraic Equations: fsolve (Multi-Variable)"

//24 ="Non-linear Algebraic Equations: Newton-Raphson (Multi-Variable)"

//25 ="Introduction and Euler's Method of Integration: Ordinary Differential Equations- Initial Value Problems"

//26 ="Ordinary Differential Equations Initial Value Problems"

//27 ="Ordinary Differential Equations Initial Value Problems: MATLAB ODE45 Algorithm"

//28 = "Ordinary Differential Equations Initial Value Problems: Runge-Kutta Methods - higher order Algorithms"

//29 = "Ordinary Differential Equations Initial Value Problems: Solving Multi-Variable ODE Methods "

//30= "This file is the support file for "SolvingDiffEqwXcos1.xcos"

//31 ="Stiff Systems & Solution using Matlab ode15s"

//32 ="ODE-IVP in Multiple Variables"

//33 ="ODE-IVP in Multiple Variables"

//34= "Regression and Interpolation"

//35= "Functional and Non-linear Regression"

//36= "Interpolation Options in Matlab"

//37= "ODE-Boundary Value Problems" bvode in SciLab

//38= "ODE-BVD Shooting Method"

//39= "Extensions of ODE-BVP"

//40= "Introduction to Differential Algebraic Equations"

//41= "Partial Differential Equations (PDE's) Notes"

//42 = "Parabolic PDEs - Method of Lines"

//43 = "Hyperbolic PDEs - Method of Lines"

//44 ="Practical Applications and Course Wrap Up Elliptic PDEs - Finite Difference"