Beruflich Dokumente
Kultur Dokumente
Lab no.1
Introduction to Matlab
What is MATLAB?
MATLAB is a numerical computing environment
and programming language.
Matlab is a commercial MATrix LABoratory
Package and it operates as an interactive
programming environment with the graphical
output.
History
MATLAB was invented in the late 1970s by Cleve
Moler, then chairman of the computer science
department at the University of New Mexico.
The purpose was to use certain software packages
without any need to learn Fortran.
In 1984 MATLAB was rewritten in C language.
Current
Directory
Command
Window
Command
History
6
Command Window
It is the primary user interface
type commands
Workspace
view program variables
clear to clear all the variables of work space
double click on a variable to see it in the Array
Editor
Command History
view past commands
save a whole session using diary
Current Directory
directory into which generated output files are
written
LEAVING MATLAB
A MATLAB session may be terminated by
simply typing
>> quit
or by typing
>> exit
at the MATLAB prompt.
Or simply clicking the cross button
10
11
ONLINE HELP
>> help
[a long list of help topics
follows]
For specific commands:
>> help demo
A demonstration of various MATLAB
functions can be explored with:
>> demo
A performance test of the machine running
MATLAB can be done with:
>> bench
12
VARIABLES
MATLAB has built-in variables like pi, ans etc.
You can learn their values from the
MATLAB interpreter.
>> pi
ans =
3.1416
>> help pi
PI 3.1415926535897....
The variable ans will keep track of the last output
which was not assigned to another variable.
13
Semicolon
Output can be suppressed by appending a
semicolon to the command lines.
>> x = 3;
>> y = x^2;
>> y
y =
9
16
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division (left and right)
^ power
17
prompt
continue statement on next line
separate statements and data
start comment which ends at end of line
(1) suppress output
(2) used as a row separator in a matrix
specify range
19
VARIABLE ARITHMETIC
MATLAB uses some fairly standard
notation. More than one command may be
entered on a single line, if they are
separated by commas.
>> 2+3;
>> 3*4, 4^2;
Order of Precedence
Powers are performed before division and
multiplication, which are done before
subtraction and addition
20
Example
>> 2+3*4^2;
generates ans = 50. That is:
2+3*4^2 ==> 2 + 3*4^2 <==
exponent has the highest
precedence
==> 2 + 3*16 <== then
multiplication operator
==> 2 + 48 <== then addition
operator
==> 50
21
23
Matrices
A Matrix is a rectangular array of numbers, every
Matlab variable we define refers to a matrix
[ a single no is a 1 by 1 matrix]
There are many applications of matrices related to
the field of Engineering e.g. families of equations
representing the state of the system( Equilibrium
equations, Energy & momentum conservation
equations). These all can be easily analyzed using
matrices.
Due to the above stated facts MATLAB helps us
in defining the matrices and their solution.
24
Example
Here is an example which shows how a
matrix is defined in a MATLAB:
a=[1 2 4; 5 8 2; 7 4 1] (3 by 3 order)
similarly
b=[4 5; 9 0] (2 by 2 order)
or in other way
c=[2 8 4; 0 2 1] (2 by 3 order).
25
Operations on Matrices
Matrices can be added or subtracted using Matlab
but their order should be same,
try few examples in matlab
26
Built-in Matrices
There are many built in matrices available in the
Matlab
1) Zeros(3) is a 3x3 Zero matrix
Zeros(2,3) is a 2x3 Zero matrix
2) eye(3) is a 3x3 identity matrix
eye(3x2) is a 3x2 identity matrix
27
28
29
Columns
30
Transpose of a matrix
Transpose of a matrix is interchanging the
rows and columns
Matlab is used to find the transpose of a
matrix with the help of (apostrophe) like a
or b or c
31
round(a)
fix(a)
ceil(a)
floor(a)
32
a=
ceil(a)
Round towards plus infinity.
ans =
1 1 1 1 0 0 0
floor(a)
Round towards minus infinity.
ans =
0 0 0 0 -1 -1 -1 -1
34
Indexing Matrices
Indexing using parentheses
>> A(2,3)
Index submatrices using vectors
of row and column indices
>> A([2 3],[1 2])
35
36
who
whos
help
clear
clear x
clc
39
what
dir
ls
pwd
40
why
41
<
<=
>
>=
==
~=
42
not
and
or
~
&
|
% highest precedence
% equal precedence with or
% equal precedence with and
43
if expression1
% is true
% execute these commands
elseif expression2
% is true
% execute these commands
else
% the default
% execute these commands
end
44
for x = array
Thank You
&
and you can explore it much more by
practice.
46