Setif -1- University Computer Science III
Faculty of Technology
TP1- Introduction to Matlab ING-ST 2nd year
EBT Department 1 session A.Y:2023/2024
_______________________________________________________________________________________________________________________________
0- Introduction :
The name MATLAB® stands for MATrix LABoratory. Is a high-performance modern matrix-based
language for technical computing and express computational mathematics. It integrates computation,
visualization, and programming environments and built-in graphics make it easy to visualize and gain
insights from data. These factors make MATLAB an excellent tool for teaching and research.
Official site : https://www.mathworks.com/
1- Matlab desktop (Interface- version 2013a ):
When you start MATLAB, the default MATLAB desktop appears.
3 4
2
1- Menu and Tools bars.
2- Current Folder : This panel allows you to access the project folders and files.
3- Command window : This is the main area where commands can be entered at the command line.
It is indicated by the command prompt (>>). ( section 2.1)
4- Workspace : The workspace shows all the variables created and/or imported from files.
5- Command history : This panel shows or return commands that are entered at the command line.
n.b. : to display these default panels go to menu : HOME/ layout/default
2- Working mode :
Matlab propose 2 working modes : interactive mode (command window) and programming mode (script)
1
2.1 interactive Mode (command window):
just enter the command or mathematical expression at the prompt (>>) in command window and Matlab
will response;
The 1st expression : 2* 4
ans = default variable to hold result
=8 (result)
2nd expression 5/3
Old value of ans is overwritten by the new one :
= 1.6667 (new ans value)
The prompt wait another command
a- Using variables :
A variable is only a computer memory location identified by a name and it has a type (real, integer,
complex, characters string).
We have defined x a variable that holds 2
x immediately is displayed
The same thing with y
z holds an expression, adding x and y
z immediately is computed and displayed
to hide the value of variable just follow the command
by semicolon ; here w is not displayed.
This command will not be executed because it's
preceded by % (comment). Variable P will never be
created
To see all variables used in Matlab session, do one of the following method:
i. Using workspace panel (HOME/layout/workspace)
ii. Using command window with whos command
- To save all variables in file filename.mat in current folder use this command : save('filename.mat' )
- To load the variables from file named filename.mat use this command : load('filename.mat' )
- To delete a specific variable use this command: clear x
- To delete all variables use this command: clear all
b- Variables types:
In Matlab, the basic element of variables is a Matrix. Each element of this matrix has a type among the
following basic types: an integer , a double, a complex, a character string, a logical type (Boolean).
2
n.b. : Don’t use these names as variables names : pi(pi value =3.14), i, j (complex number i2 = j2 =-1), eps
(error precision)
Example : c- Some useful commands:
>>a= 2 % a is an integer but it’s clc : clear console
coded as double (Real number). who : Displays variables list
>>b = 1.3 % b is double.
whos : Displays detailed variables list (size, type).
>>c = 3+i % c is complex number.
>>d = 'bonjour’ % d is characters string. clear x : delete variable x.
>>e= true % or e = logical(1) , e is boolean clear all : Delete all variables.
Task 1:
Let giving this set of matlab commands, complete this table:
Command Effect
>> x = 2 Creates variable named x with value 2
>> X = x*2 ………………………………………………….
>> y = sqrt(X) ………………………………………………….
>> % Y = y * y ………………………………………………….
>> Y ………………………………………………….
>> clc ………………………………………………….
>> save('myvars.mat') …………………………………………………..
>> clear all ………………………………………………….
>> z=x*4 ………………………………………………….
>> load('myvars.mat') ………………………………………………….
>> whos ………………………………………………….
>> x ………………………………………………….
>> z=x*4 ………………………………………………….
d- Arrays in Matlab :
An array is a collection of elements with the same data type. In MATLAB, the most commonly used types
of arrays are 1D arrays (vectors), and 2D arrays(matrices)
Row vector Column vector Matrix
V1 2 2.3 -6 7 V2 3 M 5 2 3
-7 7 0 5
55 4 9 3
6
>> V1 = [2 2.3 -6 7 ] % V1 is a row vector
>> V2 = [3 ; -7 ; 55 ; 6] % V2 is column vector, or V2 = [ 3 -7 55 6 ]'
>> M = [5 2 3 ; 7 0 5 ; 4 9 3] % M is (3x3) Matrix : 3 rows, 3 columns
3
Equidistant 1D-arrays:
An equidistant 1D-array is a vector with the distances between adjacent values are the same.
Using colon operator ( : ):
a:b generates the following row vector [ a a+1 a+2 …… b ]
a:p:b generates the following row vector [ a a+p a+2p …… b ]
ex : v1 = 1:7 this creates vector v1 = 1 2 3 4 5 6 7
v2 = 1:2:7 this creates vector v2 = 1 3 5 7
Using linspace(): linspace Linearly spaced vector.
linspace(a, b) generates a row vector of 100 linearly equally spaced points between 'a' and 'b'.
𝒃−𝒂
linspace(a, b, N) generates N points between 'a' and 'b'. (Equivalent to 𝒂: :𝒃 )
𝑵−𝟏
𝟐−𝟏
ex : v3 = linspace(1, 2, 5) Equivalent to 𝟏: : 𝟐 and this creates v3 = 1 1.25 1.50 1.75 2
𝟓−𝟏
Task 2:
Execute the following MATLAB commands manually and check the results in the command window:
Command Effect
>> v1 = 5:10 Creates row vector: v1 =[5 6 7 8 9 10]
>> v2 = [2 4 55 -4] ………………………………………………….
>> v3 = [12 ; -4 ; 54 ; -64] ………………………………………………….
>> v4 = 9:2 ………………………………………………….
>> v5 = 9:-1:2 ………………………………………………….
>> v6 = linspace(1,5,5) ………………………………………………….
>> v7 = linspace(7,1,7) ………………………………………………….
>> M1 = [4 5 2 ; 6 -4 3] ………………………………………………….
>> M2 = [1:3 ; 4:6 ; 7:9] ………………………………………………….
>> M3 = [linspace(1,3,5) ; 4:8 ; 7:0.25:8] ………………………………………………….