Showing posts with label SciLab. Show all posts
Showing posts with label SciLab. Show all posts

Getting Started with Scilab

1. Install and run Scilab (it is available in Ubuntu Software Center)
2. Type scinotes or  go to Applications-->SciNotes

//scalar variables
a=1+1
//expression
b=3+a^2

//Output
// show string variable in messagebox

sometext='variable b='+string(b)
messagebox(sometext)

//get current directory
path1=pwd()

//change current directory
//to where current file is saved
//at first save it as 'test'

pathname = get_absolute_file_path('test.sce')
chdir(pathname)

//get current directory again
path2=pwd()


//print sometext as output
print('out.txt',sometext)


//argument of a function
x=[-5:0.1:3]

//now the function
function y=f(x)
    if x>=0 then
        y=x^2
    else
        y=-x
    end
endfunction
//plot it
plot(x,f)


//Let's try arrays
c=[1,1;-2,3]
d=[5;2]

//solve linear system c x = d
//variant1, via Matrix Inverse

c1=c^(-1)
x1=c1*d

//variant2, very simple
x2=
c\d

//let's get and show 1-st element of array x2
messagebox(string(x2(1)))

//let's get 2nd column of array 'c'
cc=c(:,2)














https://youtu.be/N2gRYx5r5Xg


The example below shows to to build simple electrical circuit with xcos (scicos)

 https://youtu.be/d2FLcBLUub4













Next simple example shows simple hydraulic circuit





Scilab - ODE Second order (mass on spring with damping and external force)

debug

// diff equation
// m*y'' = m*g-c*y-r*y'+u(t)

m=1
g=9.81
r=1
c=15

Tm=6
y0=-4
v0=0

function ft=u(t)
    ft=500*sin(t*5*2*%pi)
endfunction

function wf=w(t)
    wf=Wm*sin(t*fw*2*%pi)
endfunction

function dz=f(t,y)
    dz=[y(2),g-c*y(1)/m-r*y(2)/m+u(t)/m]
endfunction


z0=[y0;v0];t0=0;t=0:Tm/1000:Tm;
y=ode(z0,t0,t,f)

y1=y(1,:)
plot(t,y1)



Arduino UNO + TMP36 temperature sensor + Scilab

1. Create circuit with TMP36 sensor (google for examples)
 



2. Connect arduino to computer via USB port (it is for data exchange and power). In Ubuntu 14 you should allow this device. Open terminal and run

sudo chmod a+rw /dev/ttyACM0

note 'ttyACM0' is optional. You may need to change this line.
In arduino software check for:  Tools>Port

3. Install and run arduino software. Create new sketch (google for details):


int outputpin= 0;

void setup()
{
Serial.begin(9600);
}

//main loop
void loop()
{
int rawvoltage= analogRead(outputpin);

float volts= (rawvoltage/1024.0) * 5;
float celsius= (volts-0.5)*100;
Serial.println(celsius);

delay(1000);

}


Go to Sketch > Upload.
Go Tools > Serial Monitor to see output (Temperature, degrees C, each 1 sec)















4. SciLab  > Applications > Module Manager - ATOMS > Real Time > Serial Communication Toolbox for Scilab

Install it and re-run scilab

5. Type Scinotes or go to Applications > SciNotes

Create code like (read help for details and commands):

n=300 // plot 300 data points from serial port "/dev/ttyACM0"
h=openserial("/dev/ttyACM0","9600,n,8,1")
i=1;
while i<=n
data(i) = strtod(readserial(h)); // char to number
plot(i,data(i),'b-o'); // real time plot
drawnow(); // show data
i=i+1;
end


And run it (arduino monitor should be closed).
You will see output in real time. Scilab is getting string output (temperature) and transfer it to the numbers.














SciLab/Xcos also has special tool for Arduino. You may google videos on youtube for that.

https://youtu.be/HZ88d_n7Q5M



SciLab ODE second order

Example #1
Cantilever beam under the concentrated moment


code
_______________________________________

debug
M=1
EJ=50
L=49.9
function dz=f(x,y)
dz=[y(2),(M/EJ)*(1+(y(2))^2)^1.5]
endfunction

z0=[0;0];x0=0;x=0:L/100:L;
y=ode(z0,x0,x,f)

plot(x,y)

_______________________________________

result is 1/4 of circle (curve of constant curvature - blue)
'y' is the vector in the code above, it is why we have 2 curves (solution and derivative)