View File - University of Engineering and Technology, Taxila

Download Report

Transcript View File - University of Engineering and Technology, Taxila

BASICS OF MATLAB
Engr.Mian Shahzad Iqbal
Lecturer ,Telecom Department
University of Engineering & Technology Taxila
In this presentation …
•You will identify the different windows
in the MATLAB desktop.
•You will learn some of the basics of
programming in the MATLAB.
•You will learn to use MATLAB as an
interactive calculator.
MATLAB interactive session
VARIABLES
•A Variable in MATLAB is a symbol used to contain a value.
•A scalar variable is a variable that contains a single
number.
•A global variable is that variable which is available in the
workspace.
•Local variable is that variable which is created by a
function and is not available outside the function.
•A string variable is composed of text (alphanumeric
characters).
•Logical variables are the variables containing the logical
values.
Predefined constants
• The several predefined constants
are listed in the table below
Command
Description
ans
Temporary variable containing the most recent answer.
eps
Specifies the accuracy of floating point precision.
i,j
The imaginary unit
Inf
Infinity
pi
The number 3.14
NaN
Indicates an undefined numerical result
SCALAR OPERATIONS
• Different operations that MATLAB performs
on the scalars are listed, in their order of
precedence, in the table below
symbol
Operation
MATLAB form
^
Exponentiation ab
a^b
*
Multiplication ab
a*b
/
Right division a/b
a/b
\
Left division b/a
a\b
+
Addition a + b
a+b
-
Subtraction a - b
a-b
The assignment operator
• The = sign in MATLAB is called the
assignment or replacement operator.
• It is used to assign the numerical values
to different variables in MATLAB.
• The variable on the left-hand side of
the = operator is replaced by the righthand side.
• General rules of using the = operator.
Managing the work session
1.
clc
clears the command window
2.
clear
3.
clear var1 var2
Removes all variables from the memory.
Removes the variables var1 and var2 from the memory.
4.
exist (‘name’)
5.
quit
6.
who
Determines if a file or variable exists having the name ‘name’. A 1
indicates that a variable exists and 0 indicates variable does not exists.
stops MATLAB.
List the variables currently in the memory.
7.
whos
List the current variables and sizes, and indicates if they have the
imaginary parts.
8.
:
Generates an array having regularly spaced elements.
9.
,
separates elements of an array. separates several commands on
the same line.
10. ;
Suppresses screen printing. Denotes a new row in an array.
11. …
continues a line
12. Arrow and control keys.
13. Smart recall.
14. Tab completion.
COMPLEX NUMBER
OPERATIONS
• MATLAB handels complex number
algebra automatically.
• For example,
c1= 1- 2i
is entered as follow
c1 = 1-2i
• Common errors
Formatting commands
•Table below gives many variants of
the format command
Commands Description and example.
format short
Four decimal digits (the default); 13.6745.
format long
16 digits; 17.27549865424597.
format short e
Five digits (four decimals) plus exponent; 6.3792e+03.
format long e
16 digits(15 decimals) plus exponent;
format bank
Two decimal digits; 126.73.
format +
Positive, negative or zero; +.
format rat
Rational approximations; 43/7.
format compact
Suppresses some line feeds.
6.379243784781294e-04.
format ----------Resets to less compact display.
--
FUNCTIONS
The table below lists some of the builtin functions of the MATLAB
Function
MATLAB syntax
ex
exp(x)
x
sqrt (x)
ln x
log(x)
log10x
Log10(x)
cos x
cos(x)
sin x
sin(x)
tan x
tan(x)
cos-1x
acos(x)
sin-1x
asin(x)
tan-1x
atan(x)