me 142 engineering computation i using excel arrays in functions

9
ME 142 Engineering Computation I Using Excel Arrays in Functions

Upload: ann-manning

Post on 08-Jan-2018

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ME 142 Engineering Computation I Using Excel Arrays in Functions

ME 142Engineering Computation I

Using Excel Arrays in Functions

Page 2: ME 142 Engineering Computation I Using Excel Arrays in Functions

Key Concepts

Using Excel Arrays in Functions

Page 3: ME 142 Engineering Computation I Using Excel Arrays in Functions

Using Excel Arrays in Functions

Page 4: ME 142 Engineering Computation I Using Excel Arrays in Functions

Using Excel Arrays in Functions Techniques used earlier in the class for

matrices may also be used to pass array information to and from a function.

This effectively allows a function to return information to more than a single cell.

Page 5: ME 142 Engineering Computation I Using Excel Arrays in Functions

Using Excel Arrays in FunctionsFunction AddVect(V1, V2)'Adds two 2-dimensional vectors

Dim AV(1 To 2)

AV(1) = V1(1) + V2(1) AV(2) = V1(2) + V2(2)

AddVect = AV End Function

Page 6: ME 142 Engineering Computation I Using Excel Arrays in Functions

Example Problem Develop a function to subtract two 3-

dimensional vectors

Page 7: ME 142 Engineering Computation I Using Excel Arrays in Functions

Example ProblemFunction VDiff(v1, v2) Dim Results(1 To 3) If v1.Count <> 3 Or v2.Count <> 3 Then MsgBox (“Error-each vector must contain 3 cells") Else For i = 1 To 3 Results(i) = v1(i) - v2(i) Next i VDiff = Results End IfEnd Function

Page 8: ME 142 Engineering Computation I Using Excel Arrays in Functions

Tips for Using Excel Arrays in Functions

Use one array variable for each vector Vector arrays are passed to function via argument list Do NOT dimension argument list variables May use .Count method to determine vector size Dim output array to store results DO dimension output array variable Set output array equal to function name to return results Select entire output area in spreadsheet before launching Use Ctrl+Shift+Enter

Page 9: ME 142 Engineering Computation I Using Excel Arrays in Functions

Homework Help ‘n Hints