array operations engr 1181 matlab 4. aerospace engineers use turbulence data to calculate how close...

22
Array Operations ENGR 1181 MATLAB 4

Upload: hazel-kimbell

Post on 01-Apr-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Array OperationsENGR 1181MATLAB 4

Page 2: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane. These vortices can cause unstable flight conditions for smaller planes approaching the wake of a commercial jetliner. Thus this turbulent flow data is important in airspace management.

Array Operations In The Real World

Page 3: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Today's Learning Objectives After today’s class, students will be able to:• Explain meaning of element-by-element

operations.• Identify situations where the standard

operators in MATLAB (when used with arrays) are reserved for linear algebra, which is not always element-by-element.

• Apply dot operators for the six cases where linear algebra is not element-by-element and therefore dot operators are needed to produce element-by-element calculations.

Page 4: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Scalar Math Review

For scalar variables a and b:

>> a = 6;>> b = 2;

MATLAB has scalar math operations:

>> a + b>> a – b>> a * b>> a / b>> a ^ b

Page 5: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Scalar–Vector Addition Define the vector v and the scalar c:

>> v = [ 10 20 30 40 ] ;>> c = 4 ;

Add them:>> v + c>> c + v

ans = 14 24 34 44

Page 6: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Vector–Vector Addition Define the vector v and the vector c:

>> v = [ 10 20 30 40 ] ;>> c = [ 2 4 6 8] ;

Add them:>> v + c>> c + v

ans = 12 24 36 48

v and c must be the same length!

Page 7: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Scalar - Vector MultiplicationFor the vector x and the scalar c:

>> v = [ 10 20 30 40 ] ; >> c = 4 ;Multiply them:>> c * v>> v * c

ans = 40 80 120 160

Page 8: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Vector - Vector Multiplicationx = [ 10 20 30 40 ] y = [ 2 4 6 8 ]

Now multiply:

>> z = x * y

??? Error using ==> mtimesInner matrix dimensions must agree!!!

Page 9: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Vector - Vector Multiplicationx = [ 10 20 30 40 ] y = [ 2 4 6 8 ]

To multiply to arrays element by element we need to use the following syntax:

>> z = x .* yz = 20 80 180 320

Page 10: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Scalar - Vector Division>> v = [ 10 20 30 40 ]>> c = 4

Divide them:>> v / c

ans = 2.50 5.00 7.50 10.00

Page 11: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Scalar - Vector Division>> v = [ 10 20 30 40 ]>> c = 4

Divide them:>> c / vError using / Matrix dimensions must agree.

>> c./v

ans = 0.400 0.200 0.133 0.100

Page 12: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Vector - Vector Divisionx = [ 10 20 30 40 ]y = [ 2 4 6 8 ]

Divide them:

>> x . / yans = 5 5 5 5

Also, try

>> y . / xans = 0.20 0.20 0.20 0.20

Page 13: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Scalar - Vector Exponents>> v = [ 10 20 30 40 ]>> c = 4

Try:>> v ^ c

Try instead:>> v .^ c

Error using ^ Inputs must be a scalar and a square matrix.To compute elementwise POWER, use POWER (.^) instead.

Page 14: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Vector - Vector Exponentsx = [ 2 2 2 2 ] ;y = [ 2 4 6 8 ] ;

Try this:

>> x .^ y

Also try>> y .^ x

ans = 4 16 64 256

ans = 4 16 36 64

Page 15: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Scalar - Vector Math Summary

For a scalar c and a vector v:Addition v + c or c + vSubtraction v – c or c – v

Multiplication v * c or c * v or v.*c or c.*vDivision v / c or c./v or v ./ c

Exponent v .^ c or c .^ v

Page 16: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Vector - Vector Math Summary

For two vectors x and y :Addition x + y or y + xSubtraction x – y or y – x

Multiplication x .* y or y .* x

Division x ./ y or y ./ x

Exponent x .^ y or y .^ xYou must always use the dot operator

for Multiplication, Division, and Exponent

Page 17: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Example 1Calculate y = 4x2 for x = 1,2,3 and 4.

First define x

>> x = [1 2 3 4];

Then calculate y

>> y = 4.*x.^2 Which ‘.’ is required here?

y =

4 16 36 64

Page 18: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Example 2Calculate y = (4a2 + a)/(2+a) for a = 1,2,3 and 4.

First define a = [ 1 2 3 4 ]

>> a = [1 2 3 4];

a =

1 2 3 4

>> y = ((4*a.^2 )+a)./(2+a)

y =

1.6667 4.5000 7.8000 11.3333

Page 19: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Built - In Vector FunctionsMATLAB has built-in functions for vectorsWhen v is a vector:max(v) Returns the largest element in v

min(v) Returns the smallest element in v

mean(v) Returns the average value of the elements in v

sum(v) Returns the sum of the elements of v

length(v) Returns the number of elements in v

sort(v) Sorts the elements of v

Page 20: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Important Takeaways Know when to use a dot operator for

Multiplication, Division, and Exponents.

Only use a dot operator when appropriate and understand what you are trying to accomplish before you use it.

Vector functions operate on an entire set of numbers located inside of an array, or matrix.

Page 21: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Preview of Next Class Input and Output• Inputting data into and out of

programs• GPA calculator example

With and without use of vectors

• Inputting to a script file• Output to command window

Page 22: Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane

Review today’s Quiz #04

Open the in-class activity from the EEIC website and we will go through it together.

Then, start working on MAT-04 homework.

Before next class, you will read more detail about script files including global variables. There is also information on input and output (I/O) commands in MATLAB.

What’s Next?