SlideShare a Scribd company logo
1 of 24
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

More Related Content

What's hot

Artificial Neural Networks (ANNs) - XOR - Step-By-Step
Artificial Neural Networks (ANNs) - XOR - Step-By-StepArtificial Neural Networks (ANNs) - XOR - Step-By-Step
Artificial Neural Networks (ANNs) - XOR - Step-By-Step
Ahmed Gad
 
Mid-Point Cirle Drawing Algorithm
Mid-Point Cirle Drawing AlgorithmMid-Point Cirle Drawing Algorithm
Mid-Point Cirle Drawing Algorithm
Neha Kaurav
 

What's hot (20)

input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
 
TOC(CS-501) (19-49).pdf
TOC(CS-501) (19-49).pdfTOC(CS-501) (19-49).pdf
TOC(CS-501) (19-49).pdf
 
Applets
AppletsApplets
Applets
 
Chapter 3 Output Primitives
Chapter 3 Output PrimitivesChapter 3 Output Primitives
Chapter 3 Output Primitives
 
Applications of Mealy & Moore Machine
Applications of  Mealy  & Moore Machine Applications of  Mealy  & Moore Machine
Applications of Mealy & Moore Machine
 
backpropagation in neural networks
backpropagation in neural networksbackpropagation in neural networks
backpropagation in neural networks
 
Artificial Neural Networks (ANNs) - XOR - Step-By-Step
Artificial Neural Networks (ANNs) - XOR - Step-By-StepArtificial Neural Networks (ANNs) - XOR - Step-By-Step
Artificial Neural Networks (ANNs) - XOR - Step-By-Step
 
Knowledge representation In Artificial Intelligence
Knowledge representation In Artificial IntelligenceKnowledge representation In Artificial Intelligence
Knowledge representation In Artificial Intelligence
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 
Area filling algo
Area filling algoArea filling algo
Area filling algo
 
History of java'
History of java'History of java'
History of java'
 
Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"Theory of Computation "Chapter 1, introduction"
Theory of Computation "Chapter 1, introduction"
 
COA-QUESTION-BANK.pdf
COA-QUESTION-BANK.pdfCOA-QUESTION-BANK.pdf
COA-QUESTION-BANK.pdf
 
Mid-Point Cirle Drawing Algorithm
Mid-Point Cirle Drawing AlgorithmMid-Point Cirle Drawing Algorithm
Mid-Point Cirle Drawing Algorithm
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Fundamentals of JAVA
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVA
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
NFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushikNFA Non Deterministic Finite Automata by Mudasir khushik
NFA Non Deterministic Finite Automata by Mudasir khushik
 
Mealy and moore machine
Mealy and moore machineMealy and moore machine
Mealy and moore machine
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 

Similar to Basic of octave matlab programming language

More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
gilpinleeanna
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
asguna
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
agnesdcarey33086
 

Similar to Basic of octave matlab programming language (20)

Chapter 01.ppt
Chapter 01.pptChapter 01.ppt
Chapter 01.ppt
 
Chapter 01.ppt
Chapter 01.pptChapter 01.ppt
Chapter 01.ppt
 
Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 intro
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
 
Matlab
MatlabMatlab
Matlab
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Matlab1
Matlab1Matlab1
Matlab1
 
1.1Introduction to matlab.pptx
1.1Introduction to matlab.pptx1.1Introduction to matlab.pptx
1.1Introduction to matlab.pptx
 
Mmc manual
Mmc manualMmc manual
Mmc manual
 
From zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyondFrom zero to MATLAB hero: Mastering the basics and beyond
From zero to MATLAB hero: Mastering the basics and beyond
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked ExamplesAn Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
Raushan's MATLB PPT..pptx
Raushan's MATLB PPT..pptxRaushan's MATLB PPT..pptx
Raushan's MATLB PPT..pptx
 
presentation.pptx
presentation.pptxpresentation.pptx
presentation.pptx
 
Matlab Nn Intro
Matlab Nn IntroMatlab Nn Intro
Matlab Nn Intro
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Kevin merchantss
Kevin merchantssKevin merchantss
Kevin merchantss
 

Recently uploaded

Recently uploaded (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 

Basic of octave matlab programming language

  • 1. BASICS OF OCTAVE/MATLAB PROGRAMMING LANGUAGE AULIA KHALQILLAH, S.SI “Be thankful” sharing.auliakhalqillah.com
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 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 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 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: 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
  • 22. 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
  • 23. 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
  • 24. 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