Introduction To Matlab
SARA VIQAR
What is MATLAB
MATLAB :- Matlab is a program for doing numerical
computation. It was originally designed for solving
linear algebra type problems using matrices. It’s name
is derived from MATrix LABoratory.
Introduction
High level language for technical computing
MATLAB is relatively easy to learn
In MATLAB everything is a matrix
MATLAB may behave like a calculator or as a
programming language
MATLAB is specifically designed for scientific
computation
Can be linked to C/C++, JAVA, SQL, etc
For MATLAB tutorials
Visit the following website
http://
www.mathworks.com/academia/student_center/tutoria
ls
Use Of MATLAB
Examples:
Matrix computations and linear algebra
Solving nonlinear equations
Numerical solution of differential equations
Mathematical optimization
Statistics and data analysis
Signal processing
Modelling of dynamical systems
Solving partial differential equations
Simulation of engineering systems
MATLAB window
Workspace
Command
Window
History
Variables
No need to declare the variable as you do in C++
Variable names can contain up to 63 characters
Variable names must start with a letter followed by
letters, digits, and underscores.
Variable names are case sensitive
Variables
To define a scalar we may simply use the equal sign
a=3
To define a vector we use the equal sign and square
brackets v=[3 ,5, 7]
To define character variables we use the equal sign and
single quotes c=‘abc’
Matlab Special Variables
‘ans’ Default variable name for results
‘pi’ Value of π
‘eps’ (epsilon) 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
‘I’ or ‘j’ for imaginary numbers i-e iota
Matlab Matrices
Matlab treats all variables as matrices. For
our purposes a matrix can be thought of as
an array, in fact, that is how it is stored.
Vectors are special forms of matrices and contain only
one row OR one column.
Scalars are matrices with only one row AND
one column
A matrix with only one row is called a row vector.
Matrices
Elements of a row are separated by comma (,)
while that of a column are separated by semi-colon (;)
Example
rowvec = [12 , 14 , 63]
colvec = [13 ; 45 ; -2]
Practice Exercise
Create a 3x4 matrix i-e a matrix having three rows and
four columns. The matrix elements can be of your own
choice
Matrix Operations
-- Accessing Single Elements of a Matrix
A(a,b)
-- Accessing Multiple Elements of a Matrix
A(a:c,b)
-- Deleting Rows and Columns
A(:,b) =[] :- this command will delete the bth col of
matrix A
-- Concatenating Matrices A and B
C=[A;B]
Some Pre-defined Matrices
zeros(m, n): matrix with all zeros
ones(m, n): matrix with all ones.
eye(m, n): the identity matrix
rand(m, n): uniformly distributed random
randn(m, n): normally distributed random
magic(m): square matrix whose elements have the
same sum along the column
Examining Numbers and Characters
who This command lists the names of all currently
defined variables.
size Size (name) returns the size of the variable
(name).
clear Erases the values of all defined variables.
Basic Mathematical Functions
+ Is the addition operator.
-Is the subtraction operator.
* Performs multiplication.
/ Performs right division. (b/a = b*Inv(a))
\ Performs left division. (b\a= Inv(a)*b)
^ Performs exponentiation.
Matlab Relational Operators
MATLAB supports six relational operators.
Less Than <
Less Than or Equal <=
Greater Than >
Greater Than or Equal >=
Equal To ==
Not Equal To ~=
Matlab Logical Operators
MATLAB supports three logical operators.
Not ~ highest precedence
and & equal precedence with or
or | equal precedence with and
Some Built In Functions
mean(A):mean value of a vector
max(A), min (A): maximum and minimum.
sum(A): summation.
sort(A): sorted vector
median(A): median value
std(A): standard deviation.
det(A) : determinant of a square matrix
dot(a,b): dot product of two vectors
Cross(a,b): cross product of two vectors
Inv(A): Inverse of a matrix A
MATLAB HELP
MATLAB HELP
Some useful links
https://engineering.purdue.edu/ECN/Support/KB/Do
cs/MatlabCharlesBoumans/index_html
http://www.stanford.edu/~wfsharpe/mia/mat/mia_m
at3.htm
http://www.cyclismo.org/tutorial/matlab/
http
://www.mathworks.com/access/helpdesk/help/techdo
c/matlab.html
Lab Task
Questions:
1. What are MATLAB command(s) to create the
matrix:
2. Provide MATLAB commands(s) to create a vector
consisting of all the numbers between 21 and 65 with a
step size of 4 in ascending order without having to
exhaustively enter them
Lab Task
Assume we have a 1×4 vector x = [4 5 2 6] stored in
MATLAB. Translate the following operations into
simple MATLAB commands one would enter at the
MATLAB prompt that make use of x:
Add 16 to each element of the vector x to produce
another vector with the results.
Create a vector where each element value is the square
root of each element of x.
Lab Task
Evaluate the following MATLAB expressions by hand
first and then verify via MATLAB. Please
provide the final numerical results next to the
corresponding expression.
a. 2 / 2 * 3
b. 6 - 2 / 5 + 7 ^ 2 – 1
c. 10 / 2 \ 5 - 3 + 2 * 4
d. 3 ^ 2 / 4
e. 3 ^ 2 ^ 2
f. 2 + round(6 / 9 + 3 * 2) / 2 – 3
Working with M-Files
M-files
Matlab can also be used as a programming language. To
program in Matlab you simply create a text file containing
Matlab commands exactly as you would type them
interactively in the Matlab window
There are two types of m-files in matlab
One is called a script. This is simply a list of Matlab
commands with noheader.
The other type is a function. Functions have a header line that
may look something like:
function y=fun1(x)
THANK YOU!!!