Activity 2 Vectors1 - TUGADE

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

DE LA SALLE UNIVERSITY – DASMARINAS

Dasmariñas, Cavite
COLLEGE OF ENGINEERING, ARCHITECTURE, AND TECHNOLOGY

CIVIL ENGINEERING PROGRAM

Name:TUGADE, FERDIERICK JANCIS LLOYD T. Date Performed: 02-10-20

Course-Year & Section: CEE23 Rating: _____________________

MATLAB BASICS

ACTIVITY 2

VECTORS

OBJECTIVES

 To Learn about vector and array properties in MATLAB, methods to create row and
column vectors, mathematical functions with vectors, and element-by-element vector
operations.

BASIC CONCEPTS / MATLAB FEATURES


EQUIPMENT AND MATERIALS REQUIRED:

Computer with MATLAB Software

PROCEDURE

MATLAB has two types of vectors (row and column) and offers several methods for creating
vectors for use. We will use a naming convention that vector variable names contain only lower
case letters.

(1) Creating row vectors in MATLAB.


(a) Enter the following at the Command Line prompt
» row_vec1 = [3, 9, -4]
row_vec1 =
3 9 -4
A row vector has been created.
Enter the following at the Command Line prompt
» row_vec1(1)
ans =
3
The value of the first element is displayed
Enter the following at the Command Line prompt
» row_vec1(3)
ans =
-4
The value of the third element is displayed

(b) Enter the following at the Command Line prompt


» row_vec2 = [7 -2 12]
row_vec2 =
7 -2 12
A second row vector has been created. Note: elements separated by spaces rather than
commas.

(c) Enter the following at the Command Line prompt


» row_vec3 = 1:4
row_vec3 =
1234
A row vector has been created using the colon operator, :.

(d) Enter the following at the Command Line prompt


» row_vec4 = [1:4]
row_vec4 =

(e) Enter the following at the Command Line prompt


» row_vec5 = 1:2:5
row_vec5 =
135
The colon operator has been used to create a row vector with elements spaced 2 apart.

(f) Enter the following at the Command Line prompt


» row_vec6 = [1:-3:-6]
row_vec6 =
1 -2 -5
The colon operator has been used to create a row vector with elements spaced -3 apart.

(g) Enter the following at the Command Line prompt


» start = 12;
» stop = 24;
» numel = 5;
» row_vec7 = linspace(start,stop,numel)
row_vec7 =
12 15 18 21 24
linspace has been used to create a row vector with ascending values.

(h) Enter the following at the Command Line prompt


» start = 12;
» stop = 0;
» numel = 5;
» row_vec8 = linspace(start,stop,numel)
row_vec8 =
12 9 6 3 0

(2) Creating column vectors in MATLAB.


(a) Enter the following at the Command Line prompt
» col_vec1 = [3; 9; -4]
col_vec1 =
3
9
-4
A column vector has been created. Rows are separated by a semi-colon.

Enter the following at the Command Line prompt


» col_vec1(1)
ans =
3
The value of the first element is displayed.

Enter the following at the Command Line prompt


» col_vec1(3)
ans =
-4
The value of the third element is displayed.

(3) Transposing vectors in MATLAB.


Enter the following at the Command Line prompt
» vec1 = [3, 9, -4]
vec1 =
3 9 -4
A row vector is created.

Enter the following at the Command Line prompt (use single quote key)
» vec1tr = vec1'
vec1tr =
3
9
-4
The row vector is transposed to a column vector.

Enter the following at the Command Line prompt (use single quote key)
» vec1trtr = vec1tr'
vec1trtr =
3 9 -4

(4) Extracting elements from vectors in MATLAB.


Enter the following at the Command Line prompt
» xvec = 1:6
xvec =
123456
»
» xvec_subset = xvec(2:4)
xvec_subset =
234

(5) Determining the number of elements in a vector.


Enter the following at the Command Line prompt
» length(xvec)
ans =
6
»
» numel_rv5 = length(row_vec5)
numel_rv5 =
3
» numel_cv1 = length(col_vec1)
numel_cv1 =
3

(6) Mathematical functions using vectors in MATLAB.


Enter the following at the Command Line prompt
» xvec = linspace(0,pi/2,5)
xvec =
0 0.3927 0.7854 1.1781 1.5708
»
» sin_xvec = sin(xvec)
sin_xvec =
0 0.3827 0.7071 0.9239 1.0000
»
» exp_xvec = exp(xvec)
exp_xvec =
1.0000 1.4810 2.1933 3.2482 4.8105

(7) Some basic vector operations in MATLAB.


MATLAB is a convenient engineering problem solving tool because it has many
“canned” routines or functions that find frequent use in solving problems. For example,
think of a vector consisting of grades on an exam. Questions that students frequently
ask about the exam are:
(1) What was the average?
(2) What is the median?
(3) What was the maximum score (students rarely ask about the minimum score)
(4) What was the standard deviation?
(5) Will the grades be “curved?” (MATLAB is not much use to answer this.)

Enter the following at the Command Line prompt


» grades = [97 67 78 88 92 94 84 79 62 95 81 73 91 85 84];
» average = mean(grades)
average =
83.3333
» mid = median(grades)
mid =
84
» [high, stnum] = max(grades)
high =
97
stnum =
1
» stdev = std(grades)
stdev =
10.2725

EXERCISES:

1. Equation of a line. The equation of a line is given by y mx b where m is the slope (a
scalar)
and b is the intercept (also a scalar).

2. Vector multiplication, division, and exponentiation. Create a vector, g, with 10 evenly spaced
elements starting at 1 and ending at 10. Compute the following with vector operations:
3. Parametric equation for a circle. The parametric equation for a circle is x r cos(Ø) and y
r sin(Ø)where r is the radius and Øis the angle of rotation counter-clockwise from the
positive x-axis. Defined this way, x and y satisfy the equation x2 y2 r 2 . Show this using
MATLAB. Use linspace to create an angle vector, theta, with values (0, π/3, 2π/3, π, 4π/3,
5π/3, 2π).

Compute the corresponding x- and y-vectors for r = 5. Show that the x- and y-vectors satisfy
the equation of a circle.

SCREENSHOTS: RESULTS AND CODES

1A-
1B-

1C-
1D-

2A-
2B-

2C-
3A-

You might also like