Sunteți pe pagina 1din 14

Tutorial 2 Vectors and Simple Matrix Operations

Date: 22/1/18

Aim:
To understand arithmetic and simple matrix operations using SCILAB.

Overview of vectors and simple matrix operations.


1) Array in SCILAB
Array is a collection of out of data in rows and column. They can be scalar, matrices, and
functions. SCILAB treats number as a matrix of size 1x1.
Eg:
- - > a=[5]
a=
5
2) Vectors as matrix
SCILAB treats a vector as matrix in form of rows and column which is given with the help of [ ] (square
bracket), and semicolon (;)
Eg: row vector
- - > b=[1 2 3]
b=
1. 2. 3.
Column vector
- - > c=[1;2;3]
c=
1.
2.
3.
3) Matrix arrangement
In SCILAB, matrix is a rectangular arrangement of a numbers in rows and column, enclosed in
brackets. It is given by either column or row vector with help of semicolon (;) and square
brackets.
Eg:
- - > x=[1 2 3]
x=
1. 2. 3.

4) - - > y=[1;2;5]
y=
1.
2.
5.

5) - - > z=[2 1 3]
z=
1. 1. 3.

RA1511007010075 Chemical Engineering 1


6) - - > x+z
Ans =
1. 3. 6.

7) Is it possible to add a row vector and a column vector, if there is inconsistency of


- - > b=x+y
! - - error

8) Term -by-term multiplication and division


You can * or / determine of same size vector term by term with array operator.
- - > a=x.*z
a=
1. 2. 9.
- - > b=x./z
b=
0.5 2. 1.

9) - - > k=[2]
k=
2.
- - > a.*k ans=
4. 4. 18.

10) Supress the screen


Semicolon is used to supress the output
- - > a=[1 2 3; 4 5 6; 7 8 9;]
a=
1. 2. 3.
4. 5. 6.
7. 8. 9.

11) Transpose of a matrix


‘ or ‘’ is used to transpose a matrix.
- - > b=a’
b=
1. 4. 7.
2. 5. 8.
3. 6. 9.

12) Use of square brackets


Square brackets are used to form concatenate vectors and matrices
- - > x=[1 2 3]
x=
1. 2. 3.
- - > y=[4 5 6 7]
y=
4. 5. 6. 7.

RA1511007010075 Chemical Engineering 2


- - > a=[x y]
a=
1. 2. 3. 4. 5. 6. 7.

13) Use of colon operation


A colon is used to specify range of in creating vectors
- - > x=[0:10]
x=
0. 1. 2. 3. 4. 5. 6. 7. 8.
9. 10.

14) Use of parenthesis


It is used to specify
i) precedence in arithmetic operation
ii) It is used to enclose matrix and vector indices
- - > sin(x)
y=
0. 0.8414710. 0.9092974 0.1411200

15) - - > z=sqrt(x.*y)


z=
0 0.9173173 1.3485529 0.6506612
1.7398879i 1.2947946i
2.144506 2.144506
2.8133372 1.9258937
2.332426i

16) Try equation of straight line y=mx+c in SCILAB where m and c are constant which are slope and
intercept m=0.5, c=2 compute y coordinates of line for given slope and intercept x= co-ordinates
given below
- - > x=[0 1.5 3 4 5 7 9 10]
x=
0 1.5 3. 4. 5. 7. 9. 10.
- - > m=[0.5]
m=
0.5
- - > c=[2]
c=
2.
- - > y=[m.*x+c]
y=
2. 2.75 3.5 4. 4.5 5.5 6.5 7.

RA1511007010075 Chemical Engineering 3


17) Multiply divide and expontiate vectors
a) - - > t=[1:10]
t=
1. 2. 3. 4. 5. 6. 7. 8. 9.
10.
- - > x=[t.*sin(t)]
x=
0.8414710 1.8185949 0.4233600
-3.02721 -4.7946214 -1.676493
4.599062 7.914866 3.7090664
-5.4402111

b) - - > y=[(t-1)/(t+1)]
y=
0. 0.333333 0.5 0.6 0.666667
0.7142852 0.75 0.777778 0.8
0.8181818

c) - - > z=sin(t.^2)/(t.^2)
z=
0.81414710 -0.1892006 0.457904
-0.0179940 -0.0052941 -0.0275494
-0.0194643 0.6143754 -0.607764
-0.0050637

18) Create a list of vectors 1 to 10


- - > x=1:10
x=
1. 2. 3. 4. 5. 6. 7.
8. 9. 10.

19) Create a linearly spaced vector between 0 to 20 with units space


a) - - > t=0:2:20
t=
0. 2. 4. 6. 8. 10. 12.
14. 16. 18. 20.

RA1511007010075 Chemical Engineering 4


b) Transpose length and size of vector
- - > t’
ans=
0.
2.
4.
6.
8.
10.
12.
14.
16.
18.
19.

c) Length of t
- - > length(t)
ans=
11.

d) Size of t
- - > size(t)
ans=
0. 11.

e) Use of linspace command for creating vectors


- - > x=linspace(0,10,5)
x=
0. 2.5 5.0 7.5 10.0

20) Size of matrix


-->A=[1 5 6; 7 4 2; -3 6 7]
A =
1. 5. 6.
7. 4. 2.
- 3. 6. 7.

-->size(A)
ans =
3. 3.

RA1511007010075 Chemical Engineering 5


21) -->x=zeroes(6,6)
x =
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
-->y=ones(3,3)
y =
1. 1. 1.
1. 1. 1.
1. 1. 1.
-->eye(3,3)*10
ans =
10. 0. 0.
0. 10. 0.
0. 0. 10.
-->rand(3,3)
ans =
0.2113249 0.3303271 0.8497452
0.7560439 0.6653811 0.6857310
0.0002211 0.6283918 0.8782165

-->y=int(rand(3,3)*100)
y =
6. 72. 23.
56. 19. 23.
66. 54. 21.

RA1511007010075 Chemical Engineering 6


22) Use of determinant an inverse of matrix A

To solve above equation matrix can be used to do so, it can write above equation in the matrix form

as

5x-3y+2z=10

-3x+8y+4z=20

2x+4y-9z=9

[A] {X} = {b}


--> A= [5 -3 2; -3 8 4; 2 4 -9]
A=
5. -3. 2.
-3. 8. 4.
2. 4. -9.
--> b= [10; 20; 90]
b=
10.
20.
90.
--> X=A\b
X=
8.6104784.
7.9954442.
-4.5330296.

RA1511007010075 Chemical Engineering 7


23) To determine the determinant of matrix A in X=a\b and inv(A)

S.No. SCILAB Operators Significance


1. [] Matrix definition
2. ; Statement separation
3. () Extractor
4. ‘ Transpose
5. + Addition
6. - Subtraction
7. * Multiplication
8. / Right Division
9. \ Left Division
10. ^ Exponent
11. .* Element wise multiplication
12. ./ Element wise right division
13. .\ Element wise left division

24)

S.No. Built-in Function Significance


1. Length Length of matrix
2. Size Size of matrix
3. linspace Linearly spaced vectors
4. zeros Zero matrix
5. eye Identity matrix
6. rand Random matrix
7. det Determinant
8. Inv Inverse of a matrix

25) What is the command used to clear screen

Ans. -->clear.
26) When do you think it is useful to use semicolon (;)?

Ans. It is used to supress the output of the user assigned variables


27) What is the size of empty matrix a=[ ]?

Ans. Zero, -->[0,0]


28) What is command to extract the diagonal elements of a square matrix into a vector

Ans. -->a= diag(X)


Where X is the matrix and a is a vector

RA1511007010075 Chemical Engineering 8


29) Extent of the off-diagonal terms of a square matrix into a vector.

Ans. B=diag(X,1) to extract terms above diagonal.

B=diag(X, -1) to extract terms below diagonal


30) Extract the last term column of A matrix and to store it in matrix B.

Ans. -->B=A(:,$)
31) Replace the even numbered columns of matrix A having 3X5 with ones.

Ans. -->A(B:, 2:2:$)=ones(:,:)


32) What is the sub-matrix of A extracted by the following a (1:3, $-1,$)?

Ans. It extracts 1st 3rows and the last 3 column in matrix A.


33) Assuming ‘A’ to be a 5X8 matrix, are the following valid commands? If so, what do they do? If not,
what is the correct command?

A(1:, 5) - false command; it will be correct if it indicates end row number.


A(:, 5) - correct; It represents all row and columns of a matrix.
a(1:3, $-1: $) - correct; Represents rows 1-3 and column 2nd last to last.
a(:$, 3:6) - false command, It will be correct if it shows starting row number.
34) What are the commands used for the following functions?
a. Identity matrix

Ans. -->a=eye(x,y)
b. Diagonal matrix

Ans. -->a=diag(x)
c. Determinant of square matrix

Ans. -->det(a)

d. Square root of each element of a matrix

Ans. -->sqrt(a)

RA1511007010075 Chemical Engineering 9


35)
-->a=[1 2 3] -->c=a+b -->e=a*b
a = -->x=[1 2 c = e =
1. 3];y=[4 5 6 7];
2. 3. 2. 6. 14. 32.
-->a=[x y] 10. 50.
-->a=[1;2;3] a = 6. 10. 32. 77.
a = 14. 122.
1. 1. 2. 10. 14. 50. 122.
2. 3. 4. 5. 18. 194.
3. 6. 7.
-->d=a-b -->f=[3 1 2;1 5
-->a=[1 2 3]' -->a=[1 2 3;4 5 d = 3;2 3 6]
a = 6;7 8 9] f =
1. a = 0. - 2. -
2. 4. 3. 1.
3. 1. 2. 2. 0. - 2.
3. 2. 1. 5.
4. 5. 4. 2. 3.
6. 0. 2. 3.
7. 8. 6.
9.
-->g=inv(f)
-->b=a' g =
b =
0.4285714
1. 4. 0. -
7. 0.1428571
2. 5. 0.
8. 0.2857143 -
3. 6. 0.1428571
9. - 0.1428571 -
0.1428571
0.2857143

RA1511007010075 Chemical Engineering 10


-->f*g -->a.*b -->a.+b -->c=eye(3,3)
ans = ans = !--error 7 c =
Dot cannot be
1. 0. 1. 8. used as modifier 1. 0.
0. 21. for this 0.
0. 1. 8. 25. operator. 0. 1.
1.110D-16 48. 0.
0. 0. 21. 48. -->a.-b 0. 0.
1. 81. !--error 7 1.
Dot cannot be
-->det(f) -->a.*b used as modifier -->c=eye(3,3)*10
ans = ans = for this c =
operator.
49. 1. 8. 10. 0.
21. -->a=zeros(3,2) 0.
-->log(a) 8. 25. a = 0. 10.
ans = 48. 0.
21. 48. 0. 0. 0. 0.
0. 81. 0. 0. 10.
0.6931472 0. 0.
1.0986123 -->a.^2
1.3862944 ans = -->b=ones(4,3) -->y=rand(2,2)
1.6094379 b = y =
1.7917595 1. 4.
1.9459101 9. 1. 1. 0.2113249
2.0794415 16. 25. 1. 0.0002211
2.1972246 36. 1. 1. 0.7560439
49. 64. 1. 0.3303271
81 1. 1.
1.
1. 1.
1.

RA1511007010075 Chemical Engineering 11


-->y=[1:5;6:10;11:15] -->y(6) -->a=[]
y = ans = a =

1. 2. 3. 12. []
4. 5.
6. 7. 8. -->y(3,2) -->size(a)
9. 10. ans = ans =
11. 12. 13.
14. 15. 12. 0. 0.

-->size(y) -->y(1,5)
ans = ans = -->a=[1 2 3;4 5 6]
a =
3. 5. 5.
1. 2. 3.
-->length(y) -->1:5 4. 5. 6.
ans = ans =
-->b=a(2,2)
15. 1. 2. 3. b =
4. 5.
-->y(1) 5.
ans =
-->a=[1:5] -->a(2,2)=100
1. a = a =

-->y(2) 1. 2. 3. 1. 2. 3.
ans = 4. 5. 4. 100. 6.

6. -->c=[0:2:10]
c =

0. 2. 4.
6. 8. 10.

RA1511007010075 Chemical Engineering 12


-->a=int(rand(5,8)*100) -->c=a(1:2:$,1:2:$)
a = c =

66. 6. 54. 65. 66. 54. 36. 40.


36. 59. 40. 41. 84. 23. 56. 4.
62. 56. 23. 30. 87. 88. 33. 26.
29. 50. 91. 28.
84. 66. 23. 93. -->d=a(:,$:-1:1)
56. 43. 4. 12. d =
68. 72. 21. 21.
48. 26. 48. 77. 41. 40. 59. 36.
87. 19. 88. 31. 65. 54. 6. 66.
33. 63. 26. 21. 28. 91. 50. 29.
30. 23. 56. 62.
12. 4. 43. 56.
-->b=a(3:4,2:5) 93. 23. 66. 84.
b = 77. 48. 26. 48.
21. 21. 72. 68.
66. 23. 93. 56. 21. 26. 63. 33.
72. 21. 21. 48. 31. 88. 19. 87.

-->a(3:4,2:5)=zeros(2,4)
a = -->a=rand(5,3)
a =
66. 6. 54. 65.
36. 59. 40. 41. 0.1121355 0.4062025
62. 56. 23. 30. 0.5618661
29. 50. 91. 28. 0.6856896 0.4094825
84. 0. 0. 0. 0.5896177
0. 43. 4. 12. 0.1531217 0.8784126
68. 0. 0. 0. 0.6853980
0. 26. 48. 77. 0.6970851 0.1138360
87. 19. 88. 31. 0.8906225
33. 63. 26. 21. 0.8415518 0.1998338
0.5042213
-->c=a(:,2:3)
c =
-->s=sum(a,'r')
6. 54. s =
56. 23.
0. 0. 2.4895836 2.0077673
0. 0. 3.2317255
19. 88.
-->p=sum(a,'c')
-->a(3:4,2:5)=b(1:2,1:4) p =
a =
1.080204
66. 6. 54. 65. 1.6847899
36. 59. 40. 41. 1.7169322
62. 56. 23. 30. 1.7015435
29. 50. 91. 28. 1.5456069

RA1511007010075 Chemical Engineering 13


84. 66. 23. 93.
56. 43. 4. 12. -->m=mean(a,1)
68. 72. 21. 21. m =
48. 26. 48. 77.
87. 19. 88. 31. 0.4979167 0.4015535
33. 63. 26. 21. 0.6463451

-->a(:,3:$) -->sd=stdev(a,1)
ans = sd =

54. 65. 36. 59. 0.3393827 0.2962057


40. 41. 0.1514517
23. 30. 29. 50.
91. 28.
23. 93. 56. 43.
4. 12.
21. 21. 48. 26.
48. 77.
88. 31. 33. 63.
26. 21.

Result:
Thus, we practiced and understood the following operations in SCILAB:
a. Creating matrices and simple matrix operations
b. Sub-matrices

RA1511007010075 Chemical Engineering 14

S-ar putea să vă placă și