Ch1 - Introduction To MATLAB
Ch1 - Introduction To MATLAB
Faculty of Engineering
Department of Electronic Engineering
Semester 2 2020/2021
Introduction to MATLAB
Instructor: Ammar Haidar
1
Email: a.haidar.ust@gmail.com
MATLAB is a program for doing numerical
computation.
5
MATLAB has since been expanded and now
has built-in functions for solving problems
requiring
› data analysis,
› signal processing,
› optimization,
› and several other types of scientific computations.
6
Application kernel, e.g., matlab-7.7
Computational engine
Built-in low level algorithms (no source code)
Includes a variety of graphical capabilities
Suite with contemporary releases, e.g. R2008b
Programming language
Designed to solve problems numerically
(Particularly good for matrix operations)
Easy to learn and use (Simplier syntax rules than
Fortran or C)
7
Auxiliaries – usually bundled with kernel
› Simulink (dynamic systems modeling)
› Stateflow (event and logic-driven modeling)
9
MATLAB is relatively easy to learn
b=
2.5000
14
Command history: a list of instructions executed by
MATLAB is shown here.
Online:
› Online documentation for Matlab at the MathWorks
website
http://www.mathworks.com/access/helpdesk/help/techdo
c/matlab.html
› There are also numerous tutorials online that are easily
found with a web search.
24
25
26
27
“%” is the neglect sign for Matlab (equaivalent
of “//” in C). Anything after it on the same line
is neglected by Matlab compiler.
28
>> clear % clears all objects in
workspace
>> clear x y % clears values of
objects x and y
>> clc % clears command
window scroll buffer
>> which <filename> % finds first occurrence
in search path
>> who % lists all objects in the
workspace
>> <command> ; % semicolon →
% execute, no display
in command window
29
>> <control> c % stops program
execution
>> <control> q % stops execution and
exits Matlab
>> <begin command text > … % three dots → continue
to next line
>> whos % List known variables plus
their size
>> help sqrt % Help on using sqrt
>> lookfor sqrt % Search for keyword sqrt
in on MATLABPATH.
>> what ('directory') % List MATLAB files in
directory
30
>> dir %List all files in current directory
>> ls % Same as dir
>> type test %Display the content of test.m
in command window
>> delete test % Delete test.m
>> cd a: % Change directory to a:
>> chdir a: % Same as cd
>> pwd % Show current directory
>> which test % Display directory path to
‘closest’ test.m
31
Top navigation bar with pull-down menus
File
Edit
Debug
Desktop
Window
Help
› Import Data
Edit
› Find, Delete, Copy, Paste, etc.
33
Desktop
› Choose layout for display of windows
Original on launch
Desktop > Save Layout (if you like it) with a name
Modify original if desired
Desktop > Save Layout (if you like it) with a name
Default layout (new in R2008b)
You can always start over from this configuration
Desktop > Desktop Layout > Default
34
Numerical values
37
Boolean logicals (Bernoulli variables)
› Truth Values
1 for true;
0 for false
› Primary logic symbols
& logical AND
== logical equal
| logical inclusive OR
~ logical NOT
› Compound logic symbols
< Less Than
<= Less Than or Equal
> Greater Than
>= Greater Than or Equal
== Equal To
~= Not Equal To
38
MATLAB also supports some logical
functions.
› any(x) returns 1 if any element of x is nonzero
› all(x) returns 1 if all elements of x are nonzero
› isnan(x) returns 1 at each NaN in x
› isinf(x) returns 1 at each infinity in x
› finite(x) returns 1 at each finite value in x
39
Separators and delimiters
40
Data Types:
41
Have not to be previously declared type in a variable
name and type in its value.
42
Example of illegal variable names:
43
Matlab Special Variables
ans Default variable name for results
pi Value of π
eps Smallest incremental number
inf Infinity
NaN Not a number e.g. 0/0
realmin The smallest usable positive real number
realmax The largest usable positive real number
44
Assignment = a = b (assign b to a)
Addition + a+b
Subtraction - a -b
1. >> for = 5
2. >> else =6
3. >> cos = 3;
>> cos(0)
4. >> A = [1,2,3];
>> sum(A)
>> sum = 7;
>> sum(A)
Type the following expressions into MATLAB at
the command window, and observe the
results:
5. >> 5>2
6. >> 5<4
7. >> 1.5<=1.5
8. >> 2>=pi
9. >> 1.8==1.801
10. >> 1.8~=2
11. >> 1.8==1.80000000000000000000000001 (see
what happen)