BASICS OF OCTAVE/MATLAB PROGRAMMING LANGUAGE AULIA KHALQILLAH, S.SI “Be thankful” sharing.auliakhalqillah.com
INTRODUCTION The OCTAVE and MATLAB is the one of the most programming language. It is simple to learn about the language. The main of these programming language is to visualization the data become a graph. There are no big different between OCTAVE and MATLAB. However, the MATLAB has more complete function compared to OCTAVE. Also, OCTAVE is OPEN SOURCE software https://www.gnu.org/software/octave/ https://www.mathworks.com/videos/programming-with-matlab- 86354.html sharing.auliakhalqillah.com
INTRODUCTION In OCTAVE and MATLAB have 4 panels, such as:  Command Window  Workspace = The whole variables will be store in the workspace  Current Folder = The active directory  Text Editor = To write full script (like atom, notepad++, Xcode, etc) In this note, we will use the command window to write the basic code of OCTAVE/MATLAB programming language. After the all of the basic code has been understood, we will explain how to write the full code using the OCTAVE/MATLAB programming language. sharing.auliakhalqillah.com
USING COMMAND WINDOW: VARIABLE You can type the command bellow in your Command Window: >> 5 ans = 5 When you type the command above, the output is ans = 5. The ans is name of variable what you typed before. If you type as the command bellow: >> x = 5 x = 5 The x is the new name of variable what you typed. sharing.auliakhalqillah.com
USING COMMAND WINDOW: VARIABLE If you add the (;) in the end of variable >> x = 5; The output variable doesn’t appear in the Command Window and it will be stored in the Workspace sharing.auliakhalqillah.com
USING COMMAND WINDOW: OPERATION OF MATH The simple operation of math in OCTAVE/MATLAB programming language is as follows: >> x = 5; >> y = 10; >> z = x + y z = 15 Operation Symbol add + substraction - multiplication * division / sharing.auliakhalqillah.com
USING COMMAND WINDOW: CONSTANTS In the OCTAVE/MATLAB programming language has a predefined constants, such as: Symbol Syntax information 𝜋 pi pi = 3.1416... Imaginary i atau j 0 + 1i epsilon eps 2.2204e-16 Infinity inf unlimited - NaN no value comment % to write some note in the code sharing.auliakhalqillah.com
USING COMMAND WINDOW: FUNCTION OF MATH In the OCTAVE/MATLAB programming language has a predefined function, such as: Symbol Syntax sin x sin(x) cos x cos(x) arcsin x asin(x) arccos x acos(x) ex exp(x) log10 x log10(x) log2 x log2(x) 𝑥 sqrt(x) sharing.auliakhalqillah.com
USING COMMAND WINDOW: FUCNTION OF MATH Example >> x = pi/2; >> sin(x) ans = 1 >> cos(x) ans = 6.1232e-17 >> asin(x) ans = 1.5708 - 1.0232i >> acos(x) ans = 0.00000 + 1.02323i Example >> exp(x) ans = 4.8105 >> log10(x) ans = 0.19612 >> log2(x) ans = 0.65150 >> sqrt(x) ans = 1.2533 >> log10(sin(x)) ans = 0 sharing.auliakhalqillah.com
USING COMMAND WINDOW: VECTOR >> x = [1 2] % Term 1 x = 1 2 >> x = [1;2] % Term 2 x = 1 2 Term 1 have the output horizontally or as the row Term 2 have the output vertically or as the column sharing.auliakhalqillah.com
USING COMMAND WINDOW: VECTOR If you want to make the vector (e.g. 1, 2, 3, ...., etc), you can type the command as follows: >> x = [1 2 3 4 5 6 7 8 9 10] % Row vector x = 1 2 3 4 5 6 7 8 9 10 >> x = [1;2;3;4;5] % Column vector x = 1 2 3 4 5 sharing.auliakhalqillah.com
USING COMMAND WINDOW: VECTOR You can generate the vector using linspace Term 1 => x = linspace(start, end) >> x = linspace(1,2); The vector will be start from 1 to 2 with the default of data length is 100. This output will be as a row vector. To produce the column vector, you can type: >> x = linspace(1,2)’; The output will be stored in Workspace sharing.auliakhalqillah.com
USING COMMAND WINDOW: VECTOR The another term of linspace is: Term 2 => linspace(start, end, N) >> x = linspace(1,2,10); The vector will be start from 1 to 2 with length of data is 10. The another one term to generate the vector: Term 3 => x = start:end >> x = 1:10 x = 1 2 3 4 5 6 7 8 9 10 sharing.auliakhalqillah.com
USING COMMAND WINDOW: VECTOR Term 4=> x = start:step:end >> x = 1:2:20 x = 1 3 5 7 9 11 13 15 17 19 sharing.auliakhalqillah.com
USING COMMAND WINDOW: FUNCTION AND OPERATION OF VECTOR Symbol Syntax Information ak a(k) The component k-th of vector a an a(end) The last component of vector a 𝑎 = 𝑘=1 𝑛 𝑎 𝑘 1/2 norm(a) normalization of vector ab a*b multiplication of vector max{ak}k=1, .. n max(a) maximum value of vector a max{ak}k=1, .. N min(a) minimum value of vector a n(a) length(a) size of vector a 𝑘=1 𝑛 𝑎 𝑘 sum(a) The summation of vector a 𝑘=1 𝑛 𝑎 𝑘 prod(a) The multiplication of vector a sharing.auliakhalqillah.com
USING COMMAND WINDOW: FUNCTION AND OPERATION OF VECTOR Symbol Syntax Information 𝑘=1 𝑛 𝑎 𝑘 𝑏 𝑘 dot(a,b) dot product between vector a and b (a1b1, ....., anbn) a.*b multiplication between components Example: >> x = [3 2 5]; >> y = [4 1 6]; >> length(x) ans = 3 >> length(y) ans = 3 Example: >> norm(x) ans = 6.1644 >> dot(x,y) ans = 44 >> z = x.*y z = 12 2 30 Example >> max(x) ans = 5 >> max(y) ans = 6 sharing.auliakhalqillah.com
USING COMMAND WINDOW: MATRIX To produce the matrix, you can type: >> x = [1 2 3;4 5 6;7 8 9] x = 1 2 3 4 5 6 7 8 9 The output is the example of matrix with size 3 x 3. The sign of (;) as the new row for the matrix. sharing.auliakhalqillah.com
USING COMMAND WINDOW: MATRIX INDEX OF MATRIX Columns 𝐴 = 𝑎1 𝑎2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎9 𝑎10 Rows In OCTAVE/MATLAB index of matrix can be written as A(i,j), where the i as the row and j as the column sharing.auliakhalqillah.com
USING COMMAND WINDOW: MATRIX EXAMPLE >> A = [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 If you want to take the element from the third row and the second column of the matrix A, you can type: >> AA = A(3,2) AA = 8 EXAMPLE >> A = [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 If you want to take the element from the first column, you can type: >> AA = A(:,1) AA = 1 4 7 sharing.auliakhalqillah.com
PENGGUNAAN COMMAND WINDOW: MATRIKS EXAMPLE >> A = [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 If you want to take the element from first row and second row, you can type: >> AA = A(1:2,:) AA = 1 2 3 4 5 6 EXAMPLE >> A = [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 If you want to take the element second column and third column, you can type: >> AA = A(:,2:3) AA = 2 3 5 6 8 9 sharing.auliakhalqillah.com
USING COMMAND WINDOW: MATRIX OPERATION The operation of math in matrix as follows:  ADDITION (+),  SUBSTRACTION(-),  MULTIPLICATION (*),  MULTIPLICATION BETWEEN ELEMENT(.*),  POWER (^),  TRANSPOSE (‘),  LEFT DIVISION (),  RIGHT DIVISION (/). EXAMPLE >> A = [1 2 3;4 5 6;7 8 9]; >> B = [2 4 6;8 10 12; 1 2 3]; >> C = A+B % Add C = 3 6 9 12 15 18 8 10 12 >> C = A-B % Substrac C = -1 -2 -3 -4 -5 -6 6 6 6 sharing.auliakhalqillah.com
USING COMMAND WINDOW: MATRIX OPERATION Example >> C = A^2 % Power C = 30 36 42 66 81 96 102 126 150 >> C = A’ % Transpose C = 1 4 7 2 5 8 3 6 9 Example >> C = A*B % Multiplication C = 21 30 39 54 78 102 87 126 165 >> C = A.*B % Element Multiplication C = 2 8 18 32 50 72 7 16 27 sharing.auliakhalqillah.com
USING COMMAND WINDOW: MATRIX OPERATION LEFT DIVISION () => C = AB = A-1B RIGHT DIVISION (/) => C = A/B = AB-1 Example: >> A = [1 2 3;4 5 6;7 8 9]; >> B = [2 4 6;8 10 12; 1 2 3]; >> C = AB % LEFT DIVISION C = -2.305556 -3.611111 -4.916667 -0.055556 -0.111111 -0.166667 2.194444 3.388889 4.583333 sharing.auliakhalqillah.com
USING COMMAND WINDOW: MATRIX OPERATION >> C = A/B % RIGHT DIVISION C = 4.0000e-01 1.5266e-16 2.0000e-01 4.8190e-16 5.0000e-01 3.8806e-16 -4.0000e-01 1.0000e+00 -2.0000e-01 SPECIFICALLY IN THE CASE OF MATRIX RULES, APPLICABLE REQUIREMENTS FOR ANALYSIS OF MATRICES. WHERE THE REQUIREMENT OF MATRIX MULTIPLICATION IS : THE NUMBER OF COLUMNS IN MATRICES A = NUMBER OF LINES IN B MATRICES THANK YOU AND GOOD LUCK Source: Matematika Numerik dengan Implementasi Matlab, Julan Hernadi, Penerbit Andi (2012) sharing.auliakhalqillah.com

Basic of octave matlab programming language

  • 1.
    BASICS OF OCTAVE/MATLAB PROGRAMMINGLANGUAGE AULIA KHALQILLAH, S.SI “Be thankful” sharing.auliakhalqillah.com
  • 2.
    INTRODUCTION The OCTAVE andMATLAB is the one of the most programming language. It is simple to learn about the language. The main of these programming language is to visualization the data become a graph. There are no big different between OCTAVE and MATLAB. However, the MATLAB has more complete function compared to OCTAVE. Also, OCTAVE is OPEN SOURCE software https://www.gnu.org/software/octave/ https://www.mathworks.com/videos/programming-with-matlab- 86354.html sharing.auliakhalqillah.com
  • 3.
    INTRODUCTION In OCTAVE andMATLAB have 4 panels, such as:  Command Window  Workspace = The whole variables will be store in the workspace  Current Folder = The active directory  Text Editor = To write full script (like atom, notepad++, Xcode, etc) In this note, we will use the command window to write the basic code of OCTAVE/MATLAB programming language. After the all of the basic code has been understood, we will explain how to write the full code using the OCTAVE/MATLAB programming language. sharing.auliakhalqillah.com
  • 4.
    USING COMMAND WINDOW: VARIABLE Youcan type the command bellow in your Command Window: >> 5 ans = 5 When you type the command above, the output is ans = 5. The ans is name of variable what you typed before. If you type as the command bellow: >> x = 5 x = 5 The x is the new name of variable what you typed. sharing.auliakhalqillah.com
  • 5.
    USING COMMAND WINDOW: VARIABLE Ifyou add the (;) in the end of variable >> x = 5; The output variable doesn’t appear in the Command Window and it will be stored in the Workspace sharing.auliakhalqillah.com
  • 6.
    USING COMMAND WINDOW: OPERATIONOF MATH The simple operation of math in OCTAVE/MATLAB programming language is as follows: >> x = 5; >> y = 10; >> z = x + y z = 15 Operation Symbol add + substraction - multiplication * division / sharing.auliakhalqillah.com
  • 7.
    USING COMMAND WINDOW: CONSTANTS Inthe OCTAVE/MATLAB programming language has a predefined constants, such as: Symbol Syntax information 𝜋 pi pi = 3.1416... Imaginary i atau j 0 + 1i epsilon eps 2.2204e-16 Infinity inf unlimited - NaN no value comment % to write some note in the code sharing.auliakhalqillah.com
  • 8.
    USING COMMAND WINDOW: FUNCTIONOF MATH In the OCTAVE/MATLAB programming language has a predefined function, such as: Symbol Syntax sin x sin(x) cos x cos(x) arcsin x asin(x) arccos x acos(x) ex exp(x) log10 x log10(x) log2 x log2(x) 𝑥 sqrt(x) sharing.auliakhalqillah.com
  • 9.
    USING COMMAND WINDOW: FUCNTIONOF MATH Example >> x = pi/2; >> sin(x) ans = 1 >> cos(x) ans = 6.1232e-17 >> asin(x) ans = 1.5708 - 1.0232i >> acos(x) ans = 0.00000 + 1.02323i Example >> exp(x) ans = 4.8105 >> log10(x) ans = 0.19612 >> log2(x) ans = 0.65150 >> sqrt(x) ans = 1.2533 >> log10(sin(x)) ans = 0 sharing.auliakhalqillah.com
  • 10.
    USING COMMAND WINDOW: VECTOR >>x = [1 2] % Term 1 x = 1 2 >> x = [1;2] % Term 2 x = 1 2 Term 1 have the output horizontally or as the row Term 2 have the output vertically or as the column sharing.auliakhalqillah.com
  • 11.
    USING COMMAND WINDOW: VECTOR Ifyou want to make the vector (e.g. 1, 2, 3, ...., etc), you can type the command as follows: >> x = [1 2 3 4 5 6 7 8 9 10] % Row vector x = 1 2 3 4 5 6 7 8 9 10 >> x = [1;2;3;4;5] % Column vector x = 1 2 3 4 5 sharing.auliakhalqillah.com
  • 12.
    USING COMMAND WINDOW: VECTOR Youcan generate the vector using linspace Term 1 => x = linspace(start, end) >> x = linspace(1,2); The vector will be start from 1 to 2 with the default of data length is 100. This output will be as a row vector. To produce the column vector, you can type: >> x = linspace(1,2)’; The output will be stored in Workspace sharing.auliakhalqillah.com
  • 13.
    USING COMMAND WINDOW: VECTOR Theanother term of linspace is: Term 2 => linspace(start, end, N) >> x = linspace(1,2,10); The vector will be start from 1 to 2 with length of data is 10. The another one term to generate the vector: Term 3 => x = start:end >> x = 1:10 x = 1 2 3 4 5 6 7 8 9 10 sharing.auliakhalqillah.com
  • 14.
    USING COMMAND WINDOW: VECTOR Term4=> x = start:step:end >> x = 1:2:20 x = 1 3 5 7 9 11 13 15 17 19 sharing.auliakhalqillah.com
  • 15.
    USING COMMAND WINDOW: FUNCTIONAND OPERATION OF VECTOR Symbol Syntax Information ak a(k) The component k-th of vector a an a(end) The last component of vector a 𝑎 = 𝑘=1 𝑛 𝑎 𝑘 1/2 norm(a) normalization of vector ab a*b multiplication of vector max{ak}k=1, .. n max(a) maximum value of vector a max{ak}k=1, .. N min(a) minimum value of vector a n(a) length(a) size of vector a 𝑘=1 𝑛 𝑎 𝑘 sum(a) The summation of vector a 𝑘=1 𝑛 𝑎 𝑘 prod(a) The multiplication of vector a sharing.auliakhalqillah.com
  • 16.
    USING COMMAND WINDOW: FUNCTIONAND OPERATION OF VECTOR Symbol Syntax Information 𝑘=1 𝑛 𝑎 𝑘 𝑏 𝑘 dot(a,b) dot product between vector a and b (a1b1, ....., anbn) a.*b multiplication between components Example: >> x = [3 2 5]; >> y = [4 1 6]; >> length(x) ans = 3 >> length(y) ans = 3 Example: >> norm(x) ans = 6.1644 >> dot(x,y) ans = 44 >> z = x.*y z = 12 2 30 Example >> max(x) ans = 5 >> max(y) ans = 6 sharing.auliakhalqillah.com
  • 17.
    USING COMMAND WINDOW: MATRIX Toproduce the matrix, you can type: >> x = [1 2 3;4 5 6;7 8 9] x = 1 2 3 4 5 6 7 8 9 The output is the example of matrix with size 3 x 3. The sign of (;) as the new row for the matrix. sharing.auliakhalqillah.com
  • 18.
    USING COMMAND WINDOW: MATRIX INDEXOF MATRIX Columns 𝐴 = 𝑎1 𝑎2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎9 𝑎10 Rows In OCTAVE/MATLAB index of matrix can be written as A(i,j), where the i as the row and j as the column sharing.auliakhalqillah.com
  • 19.
    USING COMMAND WINDOW: MATRIX EXAMPLE >>A = [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 If you want to take the element from the third row and the second column of the matrix A, you can type: >> AA = A(3,2) AA = 8 EXAMPLE >> A = [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 If you want to take the element from the first column, you can type: >> AA = A(:,1) AA = 1 4 7 sharing.auliakhalqillah.com
  • 20.
    PENGGUNAAN COMMAND WINDOW: MATRIKS EXAMPLE >>A = [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 If you want to take the element from first row and second row, you can type: >> AA = A(1:2,:) AA = 1 2 3 4 5 6 EXAMPLE >> A = [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 If you want to take the element second column and third column, you can type: >> AA = A(:,2:3) AA = 2 3 5 6 8 9 sharing.auliakhalqillah.com
  • 21.
    USING COMMAND WINDOW: MATRIXOPERATION The operation of math in matrix as follows:  ADDITION (+),  SUBSTRACTION(-),  MULTIPLICATION (*),  MULTIPLICATION BETWEEN ELEMENT(.*),  POWER (^),  TRANSPOSE (‘),  LEFT DIVISION (),  RIGHT DIVISION (/). EXAMPLE >> A = [1 2 3;4 5 6;7 8 9]; >> B = [2 4 6;8 10 12; 1 2 3]; >> C = A+B % Add C = 3 6 9 12 15 18 8 10 12 >> C = A-B % Substrac C = -1 -2 -3 -4 -5 -6 6 6 6 sharing.auliakhalqillah.com
  • 22.
    USING COMMAND WINDOW: MATRIXOPERATION Example >> C = A^2 % Power C = 30 36 42 66 81 96 102 126 150 >> C = A’ % Transpose C = 1 4 7 2 5 8 3 6 9 Example >> C = A*B % Multiplication C = 21 30 39 54 78 102 87 126 165 >> C = A.*B % Element Multiplication C = 2 8 18 32 50 72 7 16 27 sharing.auliakhalqillah.com
  • 23.
    USING COMMAND WINDOW: MATRIXOPERATION LEFT DIVISION () => C = AB = A-1B RIGHT DIVISION (/) => C = A/B = AB-1 Example: >> A = [1 2 3;4 5 6;7 8 9]; >> B = [2 4 6;8 10 12; 1 2 3]; >> C = AB % LEFT DIVISION C = -2.305556 -3.611111 -4.916667 -0.055556 -0.111111 -0.166667 2.194444 3.388889 4.583333 sharing.auliakhalqillah.com
  • 24.
    USING COMMAND WINDOW: MATRIXOPERATION >> C = A/B % RIGHT DIVISION C = 4.0000e-01 1.5266e-16 2.0000e-01 4.8190e-16 5.0000e-01 3.8806e-16 -4.0000e-01 1.0000e+00 -2.0000e-01 SPECIFICALLY IN THE CASE OF MATRIX RULES, APPLICABLE REQUIREMENTS FOR ANALYSIS OF MATRICES. WHERE THE REQUIREMENT OF MATRIX MULTIPLICATION IS : THE NUMBER OF COLUMNS IN MATRICES A = NUMBER OF LINES IN B MATRICES THANK YOU AND GOOD LUCK Source: Matematika Numerik dengan Implementasi Matlab, Julan Hernadi, Penerbit Andi (2012) sharing.auliakhalqillah.com