Basic Matrix operations:
--> ones(2,3) //generate 2*3 matrix with all elements one.
ans =
1. 1. 1.
1. 1. 1.
--> zeros(3,2) //generate 3*2 matrix with all elements zero.
ans =
0. 0.
0. 0.
0. 0.
--> eye(3,3) //generate matrix with diagonal elements '1'.
ans =
1. 0. 0.
0. 1. 0.
0. 0. 1.
--> rand(2,2) //generate 2*2 matrix of elements with random values
ans =
0.2113249 0.0002211
0.7560439 0.3303271
--> [1 2 3] //generate row vector
ans =
1. 2. 3.
--> [1;2;3] //generate column vector
ans =
1.
2.
3.
--> a = [1 2 3; 4 5 6; 7 8 9] //generate 3*3 matrix.
a =
1. 2. 3.
4. 5. 6.
7. 8. 9.
--> b = [2 1 3; 3 1 2; 1 1 2]
b =
2. 1. 3.
3. 1. 2.
1. 1. 2.
--> a+b //addition of matrix.
ans =
3. 3. 6.
7. 6. 8.
8. 9. 11.
--> a-b //subtraction of matrix.
ans =
-1. 1. 0.
1. 4. 4.
6. 7. 7.
--> a*b //multiplication of matrix.
ans =
11. 6. 13.
29. 15. 34.
47. 24. 55.
--> a.*b //element wise multiplication.
ans =
2. 2. 9.
12. 5. 12.
7. 8. 18.
--> a./b //element wise division.
ans =
0.5 2. 1.
1.3333333 5. 3.
7. 8. 4.5
--> a(2) //returns second element of matrix.
ans =
4.
--> a(2,3) //returns element at position second row & third column.
ans =
6.
--> diag(a) //returns diagonal elements of matrix.
ans =
1.
5.
9.
--> det(b) //returns determinant of matrix 'b'
ans =
2.
--> tr = a' //returns transpose of matrix 'a'
tr =
1. 4. 7.
2. 5. 8.
3. 6. 9.
--> sum(a) //returns sum of all elements of matrix 'a'
ans =
45.
--> sum(a,'r') //returns sum of all columns of matrix 'a'
ans =
12. 15. 18.
--> sum(a,'c') //returns sum of all rows of matrix 'a'
ans =
6.
15.
24.
--> min(a) //returns minimum value in matrix 'a'.
ans =
1.
--> min(a,'r') //returns minimum value in each column of matrix 'a'.
ans =
1. 2. 3.
--> min(a,'c') //returns minimum value in each row of matrix 'a'.
ans =
1.
4.
7.
--> max(a) //returns maximum value in matrix 'a'.
ans =
9.
--> max(a,'r') //returns maximum value in each column of matrix 'a'.
ans =
7. 8. 9.
--> max(a,'c') //returns maximum value in each row of matrix 'a'.
ans =
3.
6.
9.
--> prod(a) //returns product of all elements of matrix 'a'.
ans =
362880.
--> prod(a,'r') //returns product of all elements of each column of matrix 'a'.
ans =
28. 80. 162.
--> prod(a,'c') //returns product of all elements of each row of matrix 'a'.
ans =
6.
120.
504.
--> mean(a) //return mean of all elements of matrix 'a'
ans =
5.
--> mean(a,'r')
ans =
4. 5. 6.
--> mean(a,'c')
ans =
2.
5.
8.
--> a(:,3) //extract third column of matrix 'a'.
ans =
3.
6.
9.
--> a(2,:) //extract second row of matrix 'a'.
ans =
4. 5. 6.
--> rank(a) //returns rank of matrix 'a'.
ans =
2.
--> length(a) //returns no. of elements of matrix 'a'.
ans =
9.
--> size(a) //returns dimensions of matrix 'a'.
ans =
3. 3.
--> [m,n] = size(a)
n =
3.
m =
3.
--> d = ['hello' 'how' 'are' 'you']
d =
!hello how are you !
--> length(d)
ans =
5. 3. 3. 3.
--> sqrt(4)
ans =
2.
--> sqrt(-4)
ans =
2.i
--> %pi
%pi =
3.1415927
--> %e
%e =
2.7182818
--> exp(1)
ans =
2.7182818
--> 2 * %i
ans =
2.i
--> e = 1:5 //creating incremental loops
e =
1. 2. 3. 4. 5.
--> f = 1:2:10
f =
1. 3. 5. 7. 9.
Solving linear equations using Scilab
Lets take an example-
2x1 + 3x2 – 5x3 = 13
x1 – 3x2 + 8x3 = -13
2x1 – 2x2 + 4x3 = -6
We have to solve above three equations & find out values of x1, x2 & x3.
--> a = [2 3 -5 ; 1 -3 8 ; 2 -2 4]
a =
2. 3. -5.
1. -3. 8.
2. -2. 4.
--> b = [13 ; -13 ; -6]
b =
13.
-13.
-6.
--> [x0,nsa] = linsolve(a,b)
nsa =
[]
x0 =
-1.
-2.
1.
nsa, null space returned for 'a' is empty indicating unique solution.
There is also another way to solve equation -
above eq's are in format-
a.x = b
or x = inv(a)*b
Solving Cramers rule using Scilab
Lets take an example-
X + 2Y + 3Z + 4R = 4
-X + 5Y + 2Z + 7R = 11
4X + 2Y – Z + 6R = 2
2X + Y – 4Z + 7R = 9
We have to solve above four equations & find out values of X , Y , Z & R.
The above equations are in the form-
a.x = b
--> a = [1 2 3 4; -1 5 2 7; 4 2 -1 6; 2 1 -4 7]
a =
1. 2. 3. 4.
-1. 5. 2. 7.
4. 2. -1. 6.
2. 1. -4. 7.
--> b = [4 ; 11 ; 2 ; 9]
b =
4.
11.
2.
9.
--> a0 = det(a)
a0 =
-258.
--> [b , a(:,2) , a(:,3) , a(:,4)]
ans =
4. 2. 3. 4.
11. 5. 2. 7.
2. 2. -1. 6.
9. 1. -4. 7.
--> a1 = det([b , a(:,2) , a(:,3) , a(:,4)])
a1 =
516.
--> [a(:,1) , b , a(:,3) , a(:,4)]
ans =
1. 4. 3. 4.
-1. 11. 2. 7.
4. 2. -1. 6.
2. 9. -4. 7.
--> a2 = det([a(:,1) , b , a(:,3) , a(:,4)])
a2 =
258.
--> [a(:,1) , a(:,2) , b , a(:,4)]
ans =
1. 2. 4. 4.
-1. 5. 11. 7.
4. 2. 2. 6.
2. 1. 9. 7.
--> a3 = det([a(:,1) , a(:,2) , b , a(:,4)])
a3 =
3.908D-14
--> [a(:,1) , a(:,2) , a(:,3) , b]
ans =
1. 2. 3. 4.
-1. 5. 2. 11.
4. 2. -1. 2.
2. 1. -4. 9.
--> a4 = det([a(:,1) , a(:,2) , a(:,3) , b])
a4 =
-516.
--> X = a1/a0
X =
-2.
--> Y = a2/a0
Y =
-1.
--> Z = a3/a0
Z =
-1.515D-16
--> R = a4/a0
R =
2.
There is another way also-
--> inv(a)*b
ans =
-2.
-1.
0.
2.
Functions in Scilab:
A function definition starts with the keyword function and ends with the keyword
endfunction.
Lets see how to define functions in Scilab-
function <lhs_arguments>=<function_name><rhs_arguments> <statements> endfunction
Examples:
// we make function to find out square of any number. For making function we make program in Scilab editor window i.e SciNotes.
function y = sq(x)
y = x^2
endfunction
On executing above file on Scilab console window, type-
--> sq(3)
ans =
9.
Let’s take one more example:
function[x,y] = myfct(a,b)
x = a+b
y = a-b
endfunction
On executing above file on Scilab console window, type-
[x,y] = myfct(5,6)
y =
-1.
x =
11.
Definite integration:
Lets take an example-

We can solve above integration on console window-
--> integrate('x','x',0,1)
ans =
0.5
We can also solve above integration by making function on SciNotes-
function c = f(x)
c = x;
endfunction
a = input("enter initial limit: ");
b = input("enter final limit: ");
integral = intg(a,b,f);
disp(integral);
On executing above code, output on console window looks like -
enter initial limit:
0
enter final limit:
--> 1
0.5
First order Ordinary differential equation (ODE):
Lets take an example-

We will solve above differential equation by making function on SciNotes-
function dx = f(x,y)
dx = -2*x-y;
endfunction
y0 = -1;
x0 = 0;
t = 0.4;
sol = ode(y0,x0,t,f);
disp(sol,"answer");
On executing above code, output on console window looks like -
answer
-0.8109602