Introduction To Computing - Matlab
Introduction To Computing - Matlab
Introduction to Computing
by
Engr. Mohammad Haroon Yousaf
Topics
• Introduction
• MATLAB Environment
• Getting Help
• Variables
• Vectors, Matrices, and Linear Algebra
• Plotting
• Built in Functions
• Selection Programming
• M-Files
• User Defined Functions
• Specific Topics
Introduction
What is MATLAB ?
• MATLAB is a computer program that combines computation and
visualization power that makes it particularly useful tool for engineers.
• MATLAB is an executive program, and a script can be made with a list
of MATLAB commands like other programming language.
To start MATLAB:
START PROGRAMS
MATLAB 7.0
MATLAB 7.0
Display Windows
Display Windows (con’t…)
• Graphic (Figure) Window
– Displays plots and graphs
– Created in response to graphics commands.
• M-file editor/debugger window
– Create and edit scripts of commands called M-files.
Getting Help
• Variable names:
– Must start with a letter
– May contain only letters, digits, and the underscore “_”
– Matlab is case sensitive, i.e. one & OnE are different variables.
– Matlab only recognizes the first 31 characters in a variable name.
• Assignment statement:
– Variable = number;
– Variable = expression;
• Example:
>> tutorial = 1234; NOTE: when a semi-colon
>> tutorial = 1234 ”;” is placed at the end of
tutorial =
1234 each command, the result
is not displayed.
Variables (con’t…)
• Special variables:
– ans : default variable name for the result
– pi: = 3.1415926…………
– eps: = 2.2204e-016, smallest amount by which 2 numbers can differ.
– Inf or inf : , infinity
– NaN or nan: not-a-number
• Commands involving variables:
– who: lists the names of defined variables
– whos: lists the names and sizes of defined variables
– clear: clears all varialbes, reset the default values of special
variables.
– clear name: clears the variable name
– clc: clears the command window
– clf: clears the current figure and the graph window.
Vectors, Matrices and Linear Algebra
• Vectors
• Array Operations
• Matrices
• Solutions to Systems of Linear Equations.
MATLAB BASICS
Variables and Arrays
• Array: A collection of data values organized into rows
and columns, and known by a single name.
Row 1
Row 2
Row 3
arr(3,2)
Row 4
1
c= 3 3x1 array 3 elements, column vector
5
Row # Column #
Vectors
• A row vector in MATLAB can be created by an explicit list, starting with a left bracket, entering
the values separated by spaces (or commas) and closing the vector with a right bracket.
• A column vector can be created the same way, and the rows are separated by semicolons.
• Example:
>> x = [ 0 0.25*pi 0.5*pi 0.75*pi pi ]
x=
0 0.7854 1.5708 2.3562 3.1416
>> y = [ 0; 0.25*pi; 0.5*pi; 0.75*pi; pi ]
x is a row vector.
y=
0
0.7854
1.5708
2.3562 y is a column vector.
3.1416
Vectors (con’t…)
y = x’ transpose of vector x
dot (x, y) returns the scalar dot product of the vector x and y.
Array Operations
•Scalar-Array Mathematics
For addition, subtraction, multiplication, and division of an array by a
scalar simply apply the operations to all elements of the array.
•Example:
>> f = [ 1 2; 3 4]
f=
1 2
3 4
>> g = 2*f – 1
g= Each element in the array f is
1 3 multiplied by 2, then subtracted
5 7
by 1.
Array Operations (con’t…)
• Element-by-Element Array-Array Mathematics.
• Example:
>> x = [ 1 2 3 ];
>> y = [ 4 5 6 ]; Each element in x is multiplied by
>> z = x .* y the corresponding element in y.
z=
4 10 18
Matrices
A Matrix array is two-dimensional, having both multiple rows and multiple columns,
similar to vector arrays:
it begins with [, and end with ]
spaces or commas are used to separate elements in a row
semicolon or enter is used to separate rows.
•Example:
A is an m x n matrix.
>> f = [ 1 2 3; 4 5 6]
f=
1 2 3
4 5 6
>> h = [ 2 4 6
1 3 5]
h=
2 4 6
1 3 5
the main diagonal
Matrices (con’t…)
• Matrix Addressing:
-- matrixname(row, column)
-- colon may be used in place of a row or column reference to select the
entire row or column.
Example:
recall:
>> f(2,3) f=
ans = 1 2 3
6 4 5 6
>> h(:,1) h=
ans = 2 4 6
1 3 5
2
1
Matrices (con’t…)
more commands
Transpose B = A’
Identity Matrix eye(n) returns an n x n identity matrix
eye(m,n) returns an m x n matrix with ones on the main
diagonal and zeros elsewhere.
Addition and subtraction C=A+B
C=A–B
Scalar Multiplication B = A, where is a scalar.
Matrix Multiplication C = A*B
Matrix Inverse B = inv(A), A must be a square matrix in this case.
rank (A) returns the rank of the matrix A.
Matrix Powers B = A.^2 squares each element in the matrix
C = A * A computes A*A, and A must be a square matrix.
Determinant det (A), and A must be a square matrix.
A, B, C are matrices, and m, n, are scalars.
Solutions to Systems of Linear Equations
Let :
3 2 1 x1 10
A 1 3 2 x x2 b 5
1 1 1 x3 1
Ax = b
Solutions to Systems of Linear Equations (con’t…)
NOTE:
left division: A\b b A right division: x/y x y
Initializing with Keyboard Input
• Simple if statement:
if logical expression
commands
end
• Example: (Nested)
if d <50
count = count + 1;
disp(d);
if b>d
b=0;
end
end
• Example: (else and elseif clauses)
if temperature > 100
disp (‘Too hot – equipment malfunctioning.’)
elseif temperature > 90
disp (‘Normal operating range.’);
elseif (‘Below desired operating range.’)
else
disp (‘Too cold – turn off equipment.’)
end
Flow Control (con’t…)
• The switch statement:
switch expression
case test expression 1
commands
case test expression 2
commands
otherwise
commands
end
• Example:
switch interval < 1
case 1
xinc = interval /10;
case 0
xinc = 0.1;
end
Loops
• Introduction
• MATLAB Environment
• Getting Help
• Variables
• Vectors, Matrices, and Linear Algebra
• Mathematical Functions and Applications
• Plotting
• Selection Programming
• M-Files
• User Defined Functions
• Specific Topics