Professional Documents
Culture Documents
Homework 1: Variables
For the following matrix compute the summation of all Matlab can understand only first 31 characters of the
elements along each and every row separately. variable name. For the variable name, essentially you
123 have to start with a letter. Matlab is case sensitive
A= 456 hence it can distinguish between uppercase letters and
789 lowercase letters. For instance A and a are not the
same variable.
To get the diagonal elements of a matrix, diag (A)
To flip the matrix from left to right, fliplr (A) Numbers
Matlab uses conversional decimal notation. Scientific
Homework 2: notation uses the letter e to specify a power of ten.
For matrix A compute the summation of all elements Ex. 3 9.362 -99 0.002 3e-20 3e20
along the anti-diagonal. -9i 9j
Imaginary numbers use either i or j as a suffix for
Subscripts imaginary part.
To refer to the element in ith row and jth column you can Operators
use the notation A(i,j). + addition
for instance to add up some elements - subtraction
A(1,2) + A(2,3) + A(3,1) * multiplication
But the most elegant way of referring to an element is / division
to use a single subscript because Matlab will recognize ^ power
the matrix as just a column matrix, which has been ' complex conjugate transpose
formed by the columns of the original matrix. () specify evaluation order
A(3) Functions
If you want to include a value in an element outside the abs(x) - absolute value and complex magnitude
matrix you can do it in this way, sqrt(x) - square root
X = A; exp(x) - returns the exponential(ex)
X (1,4) = 10 sin(x) - sin value of angle x given in radian
1
Some special functions give the values of useful
constant
inf - infinity
NaN - Not a number
Pi - 3.14159
i or j - -1 [ ]
These functions are built in-functions. They are not
reserved. It is possible to overwrite any of them with a Deleting rows and columns
new variable. You can create your own functions with To delete 2nd column of A,
M-files. X=A;
Examples of expressions. X (:, 2) = [ ] then the answer,
nu_3 = 2 + abs(-5)
LR = ( 3 + sqrt(4)) /2 [ ]
Matrix Manipulation
Linear Algebra
Other ways of generating matrices.
Here we consider mathematical operations on matrices.
P = zeros (3,4) P = ones (2,3)
Matrix is a two-dimensional numeric array that
0000 111 represents a linear transformation.
0000 111 eg:-
0000 C= A+B means addition of two matrices.
C= det (A) means the determinant of A.
Q = 5*ones (2,3) C= inv (A) means the inverse of A.
555 C= eig (A) gives the eigen values of A.
555 C= A/5 means the division of A by a scalar.
C= A^2 means the 2nd power of A.
R = rand (3,3) uniformly distributed random elements To find the characteristic polynomial we can get it’s
Q = randn (3,3) normally distributed random elements coefficients in this way,
C= poly (A)
Load command Then we can write the characteristic polynomial since
If you save matlab files or text files containing numeric It gives the coefficients in the descending order of
data, under a name that ends in<.dat>. powers of i.e. [n , n-1 , n-2…………., 0]
for eg: - Green.dat then the command,
load Green.dat will read the file and create the Arrays
variable Green containing the relevant documents. For arrays we do arithmetic operations element-by-
element. Matlab uses dot or decimal point to represent
M-files element-by-element operation.
You can create your own matrices with M- files. + Addition
- Subtraction
Homework 4 .* Element by element multiplication
Using some text editor create a file containing . / Element by element division
following matrices A&B and save it under a name that . ^ Element by element power
ends in <.m>. Then read the relevant file in Matlab and
take the output A+B. Array operations are useful in building tables
eg:-
[ ] [ ] A = (0:, 5)’;
LUM = [A A.^2 A.^3 ]
Concatenation
Concatenation is the process of joining small matrices
to form bigger ones.
Concatenation operator is a pair of square brackets [ ].
e.g:- [ ]
If B= [ A A+10 ; A+16 A+20 ]
Then the resulting matrix is a 6×6 matrix A=(1:0.2:2)’;
PEN = [A log10(A)]
2
2. if you want to save it in graphics format so that you
can use it in other applications also, select export in
the file menu. Then you will get a dialog box. Select
a suitable save as type and give a file name with an
extension.
[ ]
Polynomials
command line editing
Representing polynomials
ctrl+p recall previous line
ctrl+n recall next line In MATLAB, polynomials are represented as a row
ctrl+b move back one character matrix containing coefficients ordered by descending
ctrl+f move forward one character powers of the polynomials.
Home ctrl+a move to the beginning of the line For eg. p(x) = x2-5x+6 is represented by
End ctrl+e move to the end of the line
Esc ctrl+u clear the line P= [1 -5 6] and to the find roots, use the roots
Del ctrl+d delete the character at cursor command as given below.
Backspace ctrl+h delete the character before cursor Q= roots (p)
Homework 7
If the roots of a quadratic equation are 1+1.732i
and 1-1.1732i find the relevant polynomial.
saving a figure