Lab 2
Lab 2
BS ECE - 2E
Programming Exercises 2
Octave Programming Features
1. Functions
(a) function xx = coswav(f,dur)
t=[0:1/(20*f):(dur)];
xx=cos(2*pi*f*t);
plot(t,xx)
end
Check if your program is correct by typing the following example on the command window:
coswav(1,1)
(b) function [sum,prod]=sumprod(x1,x2)
sum=x1+x2;
prod=(x1)*(x2);
end
Now type the line of code below on the command window:
[sum,prod]=sumprod(1+2j,2+2j)
(c) yy=ones(7,1)*rand(1,4) Displays a ones 7 columns
and ones 4 rows.
xx=randn(1,3) Displays a ones 3 rows with
random negative numbers.
yy=xx(ones(6,1),:) It will return the xx
variable into 6 columns
with the same value and 3
rows.
(d) function z=expand(xx,ncol)
xx=xx(:);
yy=ones(1,ncol)
z=xx*yy
end
To check if correct type this in command window:
expand(2,3)
2. Vectorization
(a) A = randn(6,3) Displays matrix with 6 columns and 3
rows
B=A>0 In matrix A, the negative elements are
rounded to 0 and the positive are
rounded to 1.
C=A<0 In matrix A, the negative elements are
rounded to 1 and the positive elements
are rounded to 0.
A = A.*(A > 0) Each array of matrix A is multiplied
by each array of the resulted matrix B.
(b) function Z=replacez(A)
Z=(A < 0)*77+A.*(A > 0)
end
To check if correct type this in command window:
A=[1 -3 -8 5 6 7]
replacez(A)
3. Matrix Manipulation
a. A = magic(5) Places integers in the correct order
that the column number, row sum,and
diagonal sum are equal.
b. detA=det(A) Calculates A’s determinant.
c. traceA = trace(A) The sum of the elements in the main
diagonal is computed.
d. invA=inv(A) Matrix A’s inverse is computed.
e. eigA=eig(A) The eigen value of matrix is
calculated.
f. A1=fliplr(A);A2=flipud(A) The arrays were vertically flipped
from left to right, then array was
turned upside down.
g. A7=A(7) Horizontally selects the 7th element in
the matrix.
h. sumR3=sum(A(3,:)) Adds up all the components in the
third row.
I. sumC3=sum(A(:,3)) Calculates the sum of all elements in
the third column.
j. A3by3=A(2:4,2:4) From the 2nd to the 4th column it will
create a 3 by 3 matrix.
Now, answer the following:
i. Create a 10 by 10 matrix.
- T=magic(10)
ii. Adjoint of the matrix.
- detT=det(T)
iii. Sum of the 2nd to 8th element of the 3rd row.
- sumT1=sum(T(3,2:8))
iv. Sum of the 3rd to 9th element of the 6th column.
- sumT1=sum(T(3:9,6))
v. Trunacate the matrix to a 7 by 8 matrix.
- T7by8=T(4:10,3:10)
Additional:
1. Determinant
- scalar value that is a function of the entries of a square matrix. It allows characterizing
some properties of the matrix and the linear map represented by the matrix.
2. Trace
- the sum of the diagonal elements
3. Transpose
- n operator which flips a matrix over its diagonal. It switches the row and column indices
of the matrix A by producing another matrix, often denoted by Aᵀ.
4. Adjoint
- The adjoint of a matrix A is the transpose of the cofactor matrix of A . It is denoted by
adj A . An adjoint matrix is also called an adjugate matrix
5. Inverse
- reciprocal of a number
6. Eigenvalue
- a special set of scalars associated with a linear system of equations (i.e., a matrix
equation) that are sometimes also known as characteristic roots, characteristic values,
proper values, or latent roots
7. Truncated matrix
- Set all elements in a matrix, that has a value of greater than or less than the given value.
8. Minor
- Determinant of a small matrix is formed also by eliminating columns and also one more
A’s rows in the linear algebra.
9. Cofactor
- A matrix with elements that are the cofactors, term-by-term, of a given square matrix.
10. Pivot
- element of a matrix, or an array, which is selected first by an algorithm to perform a
calculations. Pivot entry is usually required to be at least distinct from zero, and often
distant from it; in this case finding this element is called pivoting.
Conclusion: