Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Vectors, Arrays and Functions in Physics: A New Mexico Tech Tutorial, Study notes of Physics

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

Pre 2010

Uploaded on 08/08/2009

koofers-user-3wz
koofers-user-3wz 🇺🇸

10 documents

1 / 34

Toggle sidebar

Related documents


Partial preview of the text

Download Vectors, Arrays and Functions in Physics: A New Mexico Tech Tutorial and more Study notes Physics in PDF only on Docsity! <9/6/2007> Page 1 Physics 241 -- New Mexico Tech Vectors, Arrays and Functions Richard Sonnenfeld (with some material from W. Palm) n Vectors Physics definition Use in Matlab n Concept of an Array n User Defined Functions n Conditional statements <9/6/2007> Page 2 Physics 241 -- New Mexico Tech 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 <9/6/2007> Page 5 Physics 241 -- New Mexico Tech 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. <9/6/2007> Page 6 Physics 241 -- New Mexico Tech For example >> x = [0:2:8] x = [0,2,4,6,8] >> x = [0:2:7] x = [0,2,4,6] To create row vector z consisting of the values from 5 to 8 in steps of 0.1, type z = [5:0.1:8]. If the increment q is omitted, then q=1. y = [-3:2] y = [-3,-2,-1,0,1,2]. <9/6/2007> Page 7 Physics 241 -- New Mexico Tech 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 x2 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=3*t-4.9*t.^2;plot(t,x) <9/6/2007> Page 10 Physics 241 -- New Mexico Tech 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 x2 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=3*t-4.9*t.^2;plot(t,x) Vectors and relative motion <9/6/2007> Page 11 Physics 241 -- New Mexico Tech 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 x1, x2, …, xn is a scalar, given by √(x12 + x22 + … + xn2), 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. <9/6/2007> Page 12 Physics 241 -- New Mexico Tech For example: >> x = [2,-4,5] • its length is 3 (length(x)) • its magnitude is √[22 + (–4)2 + 52] = 6.7082 (sqrt(x’*x)) • its absolute value is [2,4,5] (abs(x)). <9/6/2007> Page 15 Physics 241 -- New Mexico Tech Array Addressing The colon operator selects individual elements, rows, columns, or ''subarrays'' of arrays. Examples: n v(:) represents all the elements of the vector v. n v(2:5) represents the second through fifth elements; that is v(2), v(3), v(4), v(5). n A(:,3) denotes all the elements in the third column of the matrix A. n A(:,2:5) denotes all the elements in the second through fifth columns of A. n A(3,:) denotes all the elements in the third row <9/6/2007> Page 16 Physics 241 -- New Mexico Tech You can use array indices to extract a smaller array from another array. For example, if you first create the array B B = C = 16 3 7 8 4 9 2 4 10 13 16 3 7 18 8 4 9 25 3 12 15 17 then type C = B(2:3,1:3), you can produce the following array: <9/6/2007> Page 17 Physics 241 -- New Mexico Tech 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 <9/6/2007> Page 20 Physics 241 -- New Mexico Tech Element-by-element operations: Table 2.3–1 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 <9/6/2007> Page 21 Physics 241 -- New Mexico Tech User defined functions Richard Sonnenfeld <9/6/2007> Page 22 Physics 241 -- New Mexico Tech Operations on Arrays MATLAB will treat a variable as an array automatically. For example, to compute the square roots of 5, 7, and 15, type >>x = [5,7,15]; >>y = sqrt(x) y = 2.2361 2.6358 3.8730 <9/6/2007> Page 25 Physics 241 -- New Mexico Tech User-Defined Functions: Example (continued) Call this function with its output argument: >>z = fun(3,7) z = 303 The function uses x = 3 and y = 7 to compute z. <9/6/2007> Page 26 Physics 241 -- New Mexico Tech User-Defined Functions: Example (continued) Call this function without its output argument and try to access its value. You will see an error message. >>fun(3,7) ans = 303 >>z ??? Undefined function or variable ’z’. <9/6/2007> Page 27 Physics 241 -- New Mexico Tech User-Defined Functions: Example (continued) Assign the output argument to another variable: >>q = fun(3,7) q = 303 You can suppress the output by putting a semicolon after the function call. For example, if you type q = fun(3,7); the value of q will be computed but not displayed (because of the semicolon). <9/6/2007> Page 30 Physics 241 -- New Mexico Tech You can use arrays as input arguments: >>r = fun([2:4],[7:9]) r = 300 393 498 <9/6/2007> Page 31 Physics 241 -- New Mexico Tech A function may have more than one output. These are enclosed in square brackets. For example, the function circle computes the area A and circumference C of a circle, given its radius as an input argument. function [A, C] = circle(r) A = pi*r.^2; C = 2*pi*r; <9/6/2007> Page 32 Physics 241 -- New Mexico Tech The function is called as follows, if the radius is 4. >>[A, C] = circle(4) A = 50.2655 C = 25.1327
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved