XL: Visual Basic Function to Calculate Cross ProductLast reviewed: February 3, 1998Article ID: Q121820 |
The information in this article applies to:
SUMMARYThis article contains a sample Microsoft Visual Basic for Applications function that takes two arrays (three rows by one column) of numbers, each representing a vector, and returns an array of the same dimensions representing the cross product of the two vectors.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/default.aspThe cross product, c = a x b, of the vectors a and b is a vector that is perpendicular to the plane of a and b. It can be illustrated by the following table:
i j k ========= c = a x b = m n o = [(n*z)-(o*y)]i - [(o*x)-(m*z)]j + [(m*y)-(n*x)]k x y zFor example, given two vectors a and b:
a = (1,2,3) b = (4,5,6)vector c can be computed:
c = [(2*6)-(3*5)]i - [(3*4)-(1*6)]j + [(1*5)-(2*4)]k = [12-15]i - [12-6]j + [5-8]k = [-3]i - [6]j + [-3]k = (-3,6,-3)In Microsoft Excel, you can create a Visual Basic function to perform these calculations and return the results into an array.
Sample Visual Basic Procedure
' The following line must be placed at beginning of module: Option Base 1 ' Returns the cross product of the two input vectors. Function Cross_Product_VBA(Vec1 As Object, Vec2 As Object) As Variant ' TempArray is a temporary variable to store the cross product. Dim TempArray(3, 1) ' The following lines put elements of the product into TempArray. TempArray(1, 1) = Vec1.Cells(2, 1).Value * _ Vec2.Cells(3, 1).Value - Vec1.Cells(3, 1).Value * _ Vec2.Cells(2, 1).Value TempArray(2, 1) = Vec1.Cells(3, 1).Value * _ Vec2.Cells(1, 1).Value - Vec1.Cells(1, 1).Value * _ Vec2.Cells(3, 1).Value TempArray(3, 1) = Vec1.Cells(1, 1).Value * _ Vec2.Cells(2, 1).Value - Vec1.Cells(2, 1).Value * _ Vec2.Cells(1, 1).Value ' Assigns the TempArray variable to Cross_Product_VBA. Cross_Product_VBA = TempArray End FunctionBe sure to select three vertical cells before typing the function, and enter the function as an array formula by pressing CTRL+SHIFT+ENTER or COMMAND+RETURN. For example, given the ranges A1:A3 (vector a) and B1:B3 (vector b), calculate the cross product by highlighting C1:C3, type the following function, and press CTRL+SHIFT+ENTER or COMMAND+RETURN:
=Cross_Product_VBA(A1:A3,B1:B3)The results are shown below:
A1: 1 B1: 4 C1: -3 A2: 2 B2: 5 C2: 6 A3: 3 B3: 6 C3: -3 |
Additional query words: 5.00 7.00 XL98 XL97 XL7 XL5
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |