


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A tutorial on vectors, arrays, and user-defined functions in the context of physics, using matlab as an example. It covers the concept of vectors, arrays, creating vectors and matrices from vectors, array addressing, and user-defined functions. It also includes examples and exercises.
Typology: Study notes
1 / 34
This page cannot be seen from the preview
Don't miss anything!
Richard Sonnenfeld (with some material from W. Palm)
n n VectorsVectors Physics definitionPhysics definition Use in MatlabUse in Matlab n n Concept of an ArrayConcept of an Array n n User Defined FunctionsUser Defined Functions n n Conditional statementsConditional statements
Matlab can easily represent vectors as defined in the classical physics sense.
The vector r can be specified by three components: x, y, and z, and can be written as: r = [x, y, z]. However, MATLAB can use vectors having more than three elements.
r
You can create vectors by ''appending'' one vector to another.
For example, if
r = [2,4,20] and w = [9,-6,3], Then u = [r,w]. The result is the vector u = [2,4,20,9,-6,3]. You can also create a subset of a vector. Assume we wanted to use the 2nd, 3rd, and 4th^ elements of u. How could this be done?
The colon operator (:) easily generates a large vector of regularly spaced elements.
Typing
x = [m:q:n]
creates a vector x of values with a spacing q. The first value is m. The last value is n if m - n is an integer multiple of q. If not, the last value is less than n.
The linspace command also creates a linearly
spaced row vector, but instead you specify the number of values rather than the increment.
The syntax is linspace(x1,x2,n), where x1 and x are the lower and upper limits and n is the number of points.
For example, linspace(5,8,31) is equivalent to [5:0.1:8].
If n is omitted, the spacing is 1.
Example: t=linspace(0,5);x=3t-4.9t.^2;plot(t,x)
Matlab flexibly and cleverly handles row and column vectors and matrices of different sizes.
Most operations are defined on vectors or matrices of the same size. For example, adding a row vector to a column vector causes an error.
a=[0:10]; b=a’+7; c=a+b
??? Error using ==> + Matrix dimensions must agree.
Get used to looking for these – they are one of the most common causes of strange behavior.
The linspace command also creates a linearly
spaced row vector, but instead you specify the number of values rather than the increment.
The syntax is linspace(x1,x2,n), where x1 and x are the lower and upper limits and n is the number of points.
For example, linspace(5,8,31) is equivalent to [5:0.1:8].
If n is omitted, the spacing is 1.
Example: t=linspace(0,5);x=3t-4.9t.^2;plot(t,x)
Magnitude, Length, and Absolute Value of a Vector
In physics, length, magnitude and absolute value of a vector all mean the same thing. In Matlab, they do not.
Magnitude of a vector x having elements x 1 , x 2 , …, xn is
a scalar, given by √( x 12 + x 22 + … + xn^2 ) , and is the same as the physics definition of magnitude.
length command gives the number of elements in the
vector.
The absolute value of a vector x is a vector whose elements are the absolute values of the elements of x.
Matrices
A matrix has multiple rows and columns. For example, the matrix
has four rows and three columns.
Vectors are special cases of matrices having one row or one column.
Creating Matrices from Vectors
Suppose a = [1,3,5] and b = [7,9,11] (row vectors). Note the difference between the results given by [a b] and [a;b] in the following session:
c = [a b]; c = 1 3 5 7 9 11 D = [[1,3,5];[7,9,11]]
alternatively
D = [a;b] D = 1 3 5 7 9 11
You can use array indices to extract a smaller array from another array. For example, if you first create the array B
then type C = B(2:3,1:3), you can produce the following array:
size(A) Returns a row vector [m n]
containing the sizes of the m x n array A. sort(A) Sorts each column of the array A in ascending order and returns an array the same size as A. sum(A) Sums the elements in each column of the array A and returns a row vector containing the sums. max(A)?
Additional Array Functions
The Array Editor
Element-by-element operations: Table 2.3–
Symbol
.* ./ .
.^
Examples [6,3]+2=[8,5] [8,3]-5=[3,-2] [6,5]+[4,8]=[10,13] [6,5]-[4,8]=[2,-3] [3,5].*[4,8]=[12,40] [2,5]./[4,8]=[2/4,5/8] [2,5].[4,8]=[2\4,5\8] [3,5].^2=[3^2,5^2] 2.^[3,5]=[2^3,2^5] [3,5].^[2,4]=[3^2,5^4]
Operation Scalar-array addition Scalar-array subtraction Array addition Array subtraction Array multiplication Array right division Array left division Array exponentiation
Form A + b A – b A + B A – B A.*B A./B A.\B A.^B