Вы находитесь на странице: 1из 30

Vectors & Matrices

Chapter 2
August 8, 2019 Basic Simulation Lab with MATLAB
Basics
• MATrix LABoratory
• One Object – Matrix
– Scalar , 1x1 Matrix
• Case sensitive
• Works with the entire matrix easily
– FORTRAN, C….??
• Very fast for MATRIX operations

August 8, 2019 Basic Simulation Lab with MATLAB


Variables
• Name assigned to a value
• Must begin with a letter
– Restrict length for convenience
• Can’t be same as that of a pre-defined
function
• No need to declare
• Workspace – who, whos, clear

August 8, 2019 Basic Simulation Lab with MATLAB


Expressions and Statements
• Expression
– Consists of nos, var names, p d functions
– Arith ops, rel ops, spl chars
• Evaluation produces a matrix
• Default – ans
• Complex?
• ; - suppresses result
• Variable = Expression
August 8, 2019 Basic Simulation Lab with MATLAB
Arithmetic and Relational Ops

Arithmetic
Relational
Operator Operation
Operator Operation
+ Addition
== Equal
- Subtraction
~= Not Equal
* Matrix Multiplication
< Less Than
.* Array Multiplication
<= Less Than or Equal
/ Division
> Greater Than
^ Matrix Power
>= Greater Than or Equal
.^ Array Power
' Transpose

August 8, 2019 Basic Simulation Lab with MATLAB


Pre – Defined Functions
Elementary Functions Complex Variable Functions
exp Exponential abs Absolute value
log Natural Logarithm angle Phase Angle
log10 Common Logarithm complex Form complex number
sqrt Square root
nthroot Nth root conj Conjugate
real Real part
sin Sine
imag Imaginary Part
cos Cosine i √1
j √1

August 8, 2019 Basic Simulation Lab with MATLAB


Vectors
• Row vector of arbitrary elements
– C = [ 2 -1 0 4];
• Row vector of equally spaced elements
– D = 0 : 2 : 10;
– [C D] ?
• Column vector of arbitrary elements
– E = [ 7 ; 5 ; 2; -3];
• Column vector of equally spaced elements

August 8, 2019 Basic Simulation Lab with MATLAB


Operations on Vectors
MATLAB Function Meaning Example
a=[52143]
b=[867] min Smallest element min (a) = 1

max Largest element max (b) = 8

length Total number of elements length (b) = 3

sum Sum of all elements sum (b) = 21

prod Product of all elements prod (a) = 120

mean Average value mean (a) = 3

std Standard deviation std (a) = 1.5811

median Median value median (b) = 7

August 8, 2019 Basic Simulation Lab with MATLAB


More Operations
• Addition, Subtraction
– Concatenate
• Sort
– Ascending or decending
• Array Power
– Dot operator
• Array Multiplication
– Dot operator

August 8, 2019 Basic Simulation Lab with MATLAB


Matrices
• Enter an explicit list of elements.
– A = [1 2 3; 4 5 7; 2 1 4]
– A(2,3)
– A’
• Built in functions

August 8, 2019 Basic Simulation Lab with MATLAB


Matrix Functions
eye Identity matrix size Size of matrix
zeros Matrix of zeros length Length of vector
ones Matrix of ones sum Sum of elements
diag Diagonal matrix inv Inverse of matrix
rand Matrix with eig Eigen value
random elements rank Rank of matrix
det Determinant of
triu Upper triangular matrix
part of matrix norm Norm of matrix
tril Lower triangular poly Characteristic
part of matrix polynomial
magic Magic square matrix trace Trace of matrix
prod Product of elements

mean Average value

August 8, 2019 Basic Simulation Lab with MATLAB


Operations on Matrices
• Size
– C = ones(4,7)
– [M,N] = size(C)
• Sum, Transpose, Diagonal
• Fliplr
• Determinant and Inverse
• Multiplication by scalar
• Addition
August 8, 2019 Basic Simulation Lab with MATLAB
Sub-Matrices
• Portion of the matrix
– Powerful : Operator
• A = magic(6)
– A(: , 2:4)
– A(3:4 , :)
– A(2:3 , 2:4)
– A(end , :)
– A(: , 2) = [ ]

August 8, 2019 Basic Simulation Lab with MATLAB


Loops and Vectorization
• For, while
• Flow control
– If-else-end, switch-case
– Return, continue
• Tic, Toc
• Computational efficiency

August 8, 2019 Basic Simulation Lab with MATLAB


Loops and Vectorization
1. tic
2. x = 0.01;
3. for k = 1 :1000
4. y(k) = log10(x);
5. x = x + 0.01;
6. end
7. toc

August 8, 2019 Basic Simulation Lab with MATLAB


Loops and Vectorization
1. tic
2. x = 0.01 : 0.01 : 10;
3. y = log10(x);
4. toc

August 8, 2019 Basic Simulation Lab with MATLAB


Graphs

August 8, 2019 Basic Simulation Lab with MATLAB


Graphs

August 8, 2019 Basic Simulation Lab with MATLAB


Graphs

August 8, 2019 Basic Simulation Lab with MATLAB


Graph Commands
MATLAB Command Explanation
plot (x , y) Plots vector y versus vector x
stem (x , y) Discrete sequence or "stem" plot
subplot (m , n , p) Divides the graph into m-by-n portions, selects the p-th portion for
the current plot
bar (x) Bar graph of a vector or matrix
hist (x) Histogram of elements of vector x
axis ( [xmin xmax ymin ymax] ) Sets scaling for the X- and Y-axes
xlabel ( 'text' ) Adds a label to the X-axis
ylabel ( 'text' ) Adds a label to the Y-axis
legend ( 'text' ) Displays a legend on the current plot
title ( 'text' ) Adds a title to the current graph
grid Adds major grid lines to the current axes
loglog (x , y) Plots xy graph using log scale for x and y axes
semilogx (x , y) Plots xy graph using log scale for x axis
semilogy (x , y) Plots xy graph using log scale for y axis
August 8, 2019 Basic Simulation Lab with MATLAB
Line Plot
• t = 0 : pi/100 : 2*pi;
• x = sin(t);
• y = cos(t);
• plot ( t , x , t , y );

August 8, 2019 Basic Simulation Lab with MATLAB


Decorated Plot
• plot ( t , x , '-' , t , y , '--' );
• legend ( 'sin', 'cos');
• xlabel ( 't');
• ylabel ( 'Sin(t) , Cos(t)' );
• axis ( [ 0 2*pi -1 1 ] );
• grid;
• title ( ' Sine and Cosine
Plot from 0 to 2\pi ');

August 8, 2019 Basic Simulation Lab with MATLAB


Subplot
• subplot ( 2,1,1 );
• plot ( t , x , ' : ' );
• subplot ( 2,1,2 );
• plot ( t , y , '-.' );

August 8, 2019 Basic Simulation Lab with MATLAB


Stem Plot
• t = 0 : pi/10 : 2*pi;
• x = sin(t);
• stem ( t , x )
• axis ( [ 0 2*pi -1 1
] );

August 8, 2019 Basic Simulation Lab with MATLAB


Bar Graph
• z = magic (3)
• bar(z)
• grid

August 8, 2019 Basic Simulation Lab with MATLAB


Histogram
• x = randn ( 1 , 5000 );
• hist (x)

August 8, 2019 Basic Simulation Lab with MATLAB


Creating Lab Record

Create a stem plot of


one cycle of sin(t) for
t between 0 and 2π.

August 8, 2019 Basic Simulation Lab with MATLAB


Notebook

August 8, 2019 Basic Simulation Lab with MATLAB


Lab Record
• Notebook
• Copy and Paste the Code
– t = 0 : pi/10 : 2*pi;
– x = sin(t);
– y = [t ; x]'
– stem ( t , x )
– axis ( [ 0 2*pi -1 1 ] );
• Define Input Cell
• Evaluate Cell.
• Undefine Input Cell

August 8, 2019 Basic Simulation Lab with MATLAB

Вам также может понравиться