Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

CENG103:

Computer Programming &


Applications for Civil Engineering
Lecture Note #2: Introduction to MATLAB

Prepared by: Sayed Yousif Arif Mohammed


About MATLAB

 MATLAB stands for Matrix Laboratory.


 It was developed by Mathworks.
 It has a useful programming language.
 MATLAB language, tools, and built-in functions enables its users to reach
the desired solution faster than spreadsheets or other programming
languages, such as C/C++ or Jave.
 It can be used for multiple purposes, such as signal processing and
communications, image and video processing, control systems, test and
measurement, computational finance, and computational biology.

2
Software interface

3
Software interface – Home Ribbon Tab

 File group: used to create or open new script, function, or figure. Also, it can
be used to compare between two files.
 Variable group: used to import data from a file, save workspace variables, and
create, open, or clear a variable.
 Code group: used to analyze the written codes within the files that exist in the
opened directorate. Additionally, it runs and records the time required to run a
specified code. Finally, it can be used to clear the commands or history.
 Simulink group: used to create a code using predefined functions and links.
 Environment group: used to control the layout and preferences of MATLAB.
 Resources group: used for opening the help window and downloading add-ons.

4
Software interface – Plot Ribbon Tab

 Selection group: this ribbon will contain the selected variable names.
 Plots group: used to plot the selected variables. A large library of 2-D and 3-D
charts are already defined in MATLAB.
 Options group: used to identify whether the figure should be reused or a new
one.

5
Software interface – Apps Ribbon Tab

 File group: used to find new applications online or install a downloaded


application.
 Apps group: used to initiate one of the installed applications within MATLAB
library.

6
Software interface – Editor Ribbon Tab

This tab only appears when the editor window is opened


 File group: used to create or open new script, function, or figure. Also, it can
be used to compare between two files and print the opened script.
 Edit group: used to modify the script text by inserting sections (%%), comments
(%).
 Navigate group: used to navigate around the script.
 Breakpoints group: used to set breakpoints, enable/disable lines, and set a
conditional breakpoint.
 Run group: used to run entire or segment of the script.

7
Software interface – Publisher Ribbon Tab

This tab only appears when the editor window is opened

 The options allow the user to control the output format and content before
publishing the script or function on a web browser.

8
Software interface – View Ribbon Tab

This tab only appears when editor or variable window is opened

 The options allow the user to control the selected window view.

9
Command window – General information

 When MATLAB is started, the command window will open with the below prompt
being displayed
>>
 The command window can be used as a calculator for scalar, vector, matrix
operations.
 After inputting each command, the user will get a result. As an example, if the
user inputs the following:
Example 1 Example 2 Example 3

Input: Input: Input:


>> 4 >> 4-100 >> 4*100
Output: Output: Output:
ans = ans = ans =
4 -96 400

10
Command window – General information

 As shown in the previous examples, MATLAB automatically outputs the answer


into a newly defined variable called “ans”.
 At each operation, MATLAB overwrites the previous value of “ans”. So if the
previous examples were executed in MATLAB at the same order, “ans” value
would be 400. This can verified by typing the variable name “ans”
Input:
>> ans
Output:
ans =
400
 The user can also use “ans” in further operations. An example is shown below:
Input:
>> ans/4
Output:
ans =
100

11
Command window – General information
 Example: type the below commands in MATLAB command window and write the
output.
 (5+2)*5 =

 10/2+2^2 =

 5*2^2+5*(2^2)=

 As can be noted, the order of operations is as follows:


1. Operations inside parenthesis.
2. Exponential power.
3. Negation, multiplication, and division.
4. Subtract and addition.

12
Command window – General information
 As seen in previous examples, MATLAB automatically echo prints the operations
answer as an output.
 This property can be suppressed by terminating the command line with the
semicolon (;) character at its end. This is shown below:
Input:
>> 5-4;
Output:
No output
 The user can type multiple commands on the same line before executing them,
however; the commands must be separated by comma (,) or semicolon (;) as
shown below:
Input:
>> 5-4;2-2,5-2;
Output:
ans =
0

13
Command window – General information
 To define a variable, the user must write its name and value in the command
window.
Input:
>> a=5
Output:
a=
5
 If the user writes an undefined variable in the command window and executes
the command, MATLAB will automatically returns an error as shown below:
Input:
>> b
Output:
Undefined function or variable 'b'.

 MATLAB is case-sensitive meaning that if the user were to write “A” instead of
“a”, an error will be returned as shown in the above example.

14
Command window – General information
 While defining a variable, the user must start the variable name with a
character. Additionally, it must be written as one string (without any spaces in-
between)
Input:
>> a_9=5
Output:
a_9 =
5
 Example: write the following in the command window.
 a=(100/4)*5

 A=5

 A_a2=a/A/A

15
Command window – General information
 MATLAB also has predefined variables which can be called by the user simply by
typing them.
 The unit imaginary number −1 is preassigned to the variables “i” and “j”. They
can be used to create complex values.
 Pi and exp values are also defined as “pi” and “exp(1)”.
 Examples:

Example 1 Example 2 Example 3


Input: Input: Input:
>> a=5-2*i >> a=pi >> a=exp(1)
Output: Output: Output:
a= a= a=
5.0000 - 2.0000i 3.1416 2.7183

16
Command window – General information
 As shown in the previous examples, only four decimal places are displayed when
pi and exp are called.
 If additional precision is desired by the user, the following should be writen:
>> format long
 Now when pi and exp are entered the result is displayed to 15 significant
figures:
Example 2 Example 3
Input: Input:
>> a=pi >> a=exp(1)
Output: Output:
a= a=
3.141592653589793 2.718281828459046
 To return to the four decimal version, type
>> format short

17
Command window – General information
 The following is a summary of the format commands you will employ routinely in
engineering and scientific calculations. They all have the syntax: format type.

18
Command window – Arrays, vectors, and Matrices
 An array is a collection of values that are represented by a single variable.
 One dimensional arrays are called vectors and two-dimensional arrays are called
matrices.
 The scalars discussed in previous slides are actually matrices with one row and
one column.
 To initiate a variable as an array, brackets are used in the command window.
Row elements can either be separated by commas (,) or spaces and column
elements can either be separated by semicolon (;) or line separators.
Example 1 Example 2 Example 3
Input: Input: Input:
>> a=[1 2,3,4 5] >> a=[1;2;3] >> a=[1,2,3
Output: Output: 4,5,6;7,8,9]
a= a= Output:
1 2 3 4 5 1 a=
2 1 2 3
3 4 5 6
7 8 9
19
Command window – Arrays, vectors, and Matrices
 Additionally, arrays can be transposed by writing single quote mark (‘).
Input:
>> a=[1 2,5]’
Output:
a=
1
2
5
 Example: write down the output of the following command.
 a=[[1 1 1]’ [2 3 2]’ [3 3 3]’]’

20
Command window – Arrays, vectors, and Matrices
 The user can call a specific element within the array using the following
methods:
Let a=[1,2,3;4,5,6;7,8,9];
1. By element number: using a(m) where m indicates the element number counted
from column-wise then row-wise.
Input:
>> a(4)
Output:
ans =
2
2. By row and column number: using a(m,n) where m indicates the row number and n
indicates the column number.
Input:
>> a(1,2)
Output:
ans =
2
21
Command window – Arrays, vectors, and Matrices
 The colon operator is a useful tool in MATLAB that allows the user to create and
control arrays. There are two ways to use colon operator as shown below:

1. By indicating the start and end of an array: to do this, the user only needs to place
the starting number and ending number separated by a colon.
>> 1:7
Output:
ans =
1 2 3 4 5 6 7
2. By indicating the start, increment, and end of an array: to do this, the user must
place the starting number first followed by a constant increment and finally the ending
number. The three numbers must be separated by a colon.
Input:
>> 1:3:7
Output:
ans =
1 4 7

22
Command window – Arrays, vectors, and Matrices
 Examples: try using the colon operator to perform the following commands and
record your results.
 a=5:0.5:8

 a=10:-1:5

 a=10:-2:5

 a=[1:3;4:6;7:9]

23
Command window – Arrays, vectors, and Matrices
 Examples: try using the colon operator to perform the following commands and
record your results.
 a=[1:3’;4:6’;7:9’]

 a=[(1:3)’;(4:6)’;(7:9)’]

 a=10:5

24
Command window – Arrays, vectors, and Matrices
 The colon operation can also be used to select an array of elements from a pre-
defined matrix:
Let a=[1,2,3,4;5,6,7,8];
1. By element number: using a(i:j) where i indicates the starting element position and
j indicates the ending element position.
Input:
>> a(1:4)
Output:
a=
1 5 2 6
2. By row and column number: using a(m,i:j) where m indicates the row number, i
indicates the starting column number and j indicates the ending column number.
Another option is to use a(i:j,n) where i indicates the starting row number, j indicates
the ending row number, and n indicates the column number.
Input:
>> a(1,1:4)
Output:
a=
1 2 3 4
25
Command window – Arrays, vectors, and Matrices
 Examples: try using the colon operator to perform the following commands and
record your results.
Let a=[1:5;6:10;11:15];
 a(1:7)

 a(2,3:5)

 a(2:3,2)

26
Command window – Arrays, vectors, and Matrices
 Examples: try using the colon operator to perform the following commands and
record your results.
Let a=[1:5;6:10;11:15];
 a(1,3:6)

 a(3,3:2:5)

 a(3,3:0.5:5)

27
Command window – Arrays, vectors, and Matrices
 Examples: try using the colon operator to perform the following commands and
record your results.
Let a=[1:5;6:10;11:15];
 a(3,:)

 a(3,:)’

 a(1:2,2:2:4)

28
Command window – Characters and Strings
 Variables in MATLAB can also contain a character or a string instead of numbers.
 A character (Char for short), is a single visual object used to represent text,
number, or symbol.
 On the other hand, a string is an ordered sequence of characters placed
together in a single variable.
 To define a character or string, the user must place a single quote mark (‘)
before and after the string.

1. Char: 2. String:
Input: Input:
>> a=‘H’ >> a=‘Hello world’
Output: Output:
a= a=
H Hello world

29
Command window – Characters and Strings
 It is possible to combine multiple characters or strings and print the output as
shown below.
Example 1: Example 2: Example 3:
Input: Input: Input:
>> a=‘Hi’ >> a=‘Hi’ >> a=‘Hi’
>> b=‘Friend’ >> b=‘Friend’ >> b=‘Friend’
>> x=[a b] >> x=[a ‘ ’ b] >> x=[a ‘ Best ’ b]
Output: Output: Output:
x= x= x=
HiFriend Hi Friend Hi Best Friend

 Try performing the following commands:


 a+b

 2*a

30
Command window – Characters and Strings
 The character values are set according to ASCII (American Standard Code for
Information Interchange).

31
Command window – Characters and Strings

32

You might also like