Telechargé par amine maiza

# PracticeWithVectors

publicité
```Practice with Vectors
So far, you've learned some fundamental operations to create and work with vectors. In this live script, you'll
review earlier concepts and learn new vector operations. The activities are divided into the following sections:
Creating Vectors....................................................................................................................................................... 1
Concatenation.......................................................................................................................................................1
Vectors with Set Spacing...................................................................................................................................... 2
Vectors with Set Number of Elements.................................................................................................................. 3
Summary.............................................................................................................................................................. 3
Accessing and Manipulating Vector Elements..........................................................................................................3
Vector Indexing..................................................................................................................................................... 3
Vector Manipulation.............................................................................................................................................. 4
Math Operations with Vectors...................................................................................................................................4
Arithmetic Operations........................................................................................................................................... 4
Math Functions..................................................................................................................................................... 5
Vector Characteristics...............................................................................................................................................6
Transpose............................................................................................................................................................. 6
Size and Length....................................................................................................................................................6
Conclusion................................................................................................................................................................ 7
Creating Vectors
There are many ways to create vectors. In this section, you will see how to
• Create vectors using concatenation.
• Create vectors with a set spacing.
• Create vectors with a set number of elements.
Concatenation
Square brackets are used to group elements together into vectors. Use a space between elements to create a
row vector, or add a semicolon between elements to create a column vector.
rowVector = [1 3 6 3 11]
colVector = [1; 3; 6; 3; 11]
% Row vector
% Column vector
Use concatenation to create a column vector v1 with the following elements: 0, 1, 1, 0.
v1=[0; 1; 1; 0]
1
v1 = 4&times;1
0
1
1
0
Create a second column vector v2 with the following elements: 0,5,0.
v2=[0;5;0]
v2 = 3&times;1
0
5
0
Square brackets are also used to combine vectors together. Create a new column vector v3 that combines the
v1 and v2 vectors together. This should create a new column vector with seven rows and one column.
v3=[v1;v2]
v3 = 7&times;1
0
1
1
0
0
5
0
Vectors with Set Spacing
The colon operator creates a row vector with a set spacing. If you do not specify the spacing value, it will use a
default spacing of 1.
v = startingValue:spacing:endingValue
v = 0:2:8
Use the colon operator to create a vector named v4 that starts at 45, ends at 90, with a spacing of 5.
v4=[45:5:90]
v4 = 1&times;10
45
50
55
60
65
70
75
80
85
90
Use the colon operator to create a vector named v5 that starts at -10 and ends at 5, with the default spacing of
1.
v5=[-10:5]
2
v5 = 1&times;16
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
Vectors with Set Number of Elements
The linspace function creates a vector with a set number of equally spaced elements:
v = linspace(startingValue,endingValue,numberOfElements)
v = linspace(0,8,5)
Use the linspace function to create a vector named v6 that starts at 0, ends at
elements. Use the pi function for the value of
, with 5 equally spaced
.
v6=linspace(0, 22*pi, 5)
v6 = 1&times;5
0
17.2788
34.5575
51.8363
69.1150
Summary
When creating vectors, choose the approach best suited for what you need to accomplish:
• Use concatenation when you need to combine multiple elements or vectors into a single vector.
• Use the colon operator when you need to create a vector with a set spacing between the elements.
• Use the linspace function when you need to create a vector with a set number of elements.
Accessing and Manipulating Vector Elements
Vector Indexing
To access elements from a vector, add parenthesis after the vector name. Inside the parenthesis, specify the
indices of the desired elements. Use an equal sign (=) before the indexing operation to assign the result to a
variable.
v(3)
val1 = v(end)
val2 = v([5 7 4])
val3 = v(1:2:end)
%
%
%
%
Get the third element
Store the last element into a variable val1
Store the fifth, seventh, and fourth elements into a vector val2
Store every other element, starting with the first, into a vector val3
3
Use indexing to obtain the fifth element from the vector v6 and store it in a variable named x.
v6(5)
ans = 69.1150
Vector Manipulation
Use an equal sign (=) after the indexing operation to assign new values to vector elements.
v(3) = 10;
v([1 4]) = [5 15];
v(3:end) = 0;
% Change value of 5th element to 10
% Change value of the 1st element to 5 and the value of the 4th element to 1
% Change the value to 0 for every element from the 3rd index to the end of t
Change the third value of the v6 vector to 0.
v6(3)=0
v6 = 1&times;5
0
17.2788
0
51.8363
69.1150
Math Operations with Vectors
Arithmetic Operations
Use elementwise operators to perform mathematical operations on the corresponding elements of two vectors.
Below are examples of common math operators in MATLAB.
[1;
[1;
[1;
[1;
[1;
3] + [5; 4]
3] - [5; 4]
3] .* [5; 4]
3] ./ [5; 4]
3] .^ [5; 4]
=
=
=
=
=
[6; 7]
[-4; -1]
[5; 12]
[0.2; 0.75]
[1; 81]
%
%
%
%
%
Subtraction
Elementwise multiplication
Elementwise division
Elementwise power
4
Given the vectors A and B, compute the elementwise multiplication of A times B. Store the result in a new vector
named C.
A = [1; 28; 40];
B = [16; 35; 120];
c=A.*B
c = 3&times;1
16
980
4800
Math Functions
There are many common mathematical functions in that operate on vectors. The function is automatically
applied to every element of the vector.
sin(x), cos(x), tan(x)
exp(x), log(x), log10(x)
abs(x)
sqrt(x)
%
%
%
%
Trigonometric operators
Exponential, natural log, and base-10 log
Absolute value
Square root
Given the vectors expected and experimental defined below, compute the percent error for each element
using the equation below.
Store the result in a new row vector named percentError. Use the abs function to compute the absolute
value.
expected = [50 55 60 65 70 75];
experimental = [48.8 55.2 54 62.8 65.7 74.6];
percentError=abs((experimental-expected)./expected)*100
percentError = 1&times;6
2.4000
0.3636
10.0000
3.3846
6.1429
0.5333
The resulting row vector should have six unique values ranging from about 0.36 to 10.
5
Vector Characteristics
Transpose
You can use the transpose operator to transform a row vector into a column vector (or a column vector into a
row vector).
colVector = rowVector'
rowVector = colVector'
% Transform row vector into a column vector
% Transform column vector back into a row vector
Transpose the percentError row vector calculated above into a column vector. Store the result in a new
vector named peCol.
peCol=percentError'
peCol = 6&times;1
2.4000
0.3636
10.0000
3.3846
6.1429
0.5333
Size and Length
The size function returns the number of rows and the number of columns of a vector. The length function is
similar but returns only the length of the longest dimension, regardless of whether it is a row or column vector.
size(rowVector)
length(rowVector)
Use the size function to get the size (rows and columns) of the peCol vector. Use the length function to get
the length of the peCol vector. Notice how the output of the length function differs from the size function.
size(peCol)
6
ans = 1&times;2
6
1
length(peCol)
ans = 6
Conclusion
In this reading, you practiced some essential operations with vectors. You learned about creating vectors,
accessing vector elements, manipulating vector data, and how to perform mathematical operations with vectors.
You also learned how to convert between row and column vectors and get a vector's number of rows and/or
columns.
The more you practice, the better you'll become at working with vectors. Feel free to use the space below to
explore more operations with vectors.
x=(-10:0.2:10)
x = 1&times;101
-10.0000
-9.8000
-9.6000
-9.4000
-9.2000
7
-9.0000
-8.8000
-8.6000
```