Hohmann in Visual Basic

From NewMarsWiki
Revision as of 04:02, 21 January 2009 by Josh Cryer (talk | contribs) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Visual Basic is a Microsoft product which is promoted by Microsoft as one of the easier programming languages. The advantage of using visual basic is since it is a Microsoft product it is easy to use to communicate with many Microsoft applications including, Microsoft Word, Microsoft Excel and Microsoft Access.

Also a wide number of people already have the tools to use it since Microsoft freely builds a visual basic macro editor into Microsoft office software. Thus for many people that are comfortable using spreadsheets to do computation visual basic provides an “easy” means of extending the power of those spread sheets.

People generally associate visual basic with graphical programming. However, visual programming can be done in most languages with the right integrated development environment. For instance in JAVA there is JBuilder, and for C there is visual C++ and lab windows. Thus because you can program graphically in visual basic does not mean that the language is any easier. Moreover, it is much more difficult to generalize code in a graphical program environment. Hence, graphical programming is a less flexible way to build GUIs.

Another supposed advantage of visual basic is that the simplicity of the syntax. “Visual Basic is built on basic, which is the simplest programming language ever devised.” Such marketing sound bites are outright falsehoods. The syntax of the visual basic language is no simpler then any other language derived from the syntax of BCPL family. Most peoplar languages are based on the BCPL family.

Another problem with visual basic is that the code is difficult to generalize. For instance array functions and access calls do not work on scalar variables. Moreover, arrays must be addressed with the proper number of indices. Thus although visual basic has a variant datatype it’s is only a marginal improvement over fixed datatypes and not well suited for generic programming.

Visual Basic Code to calculate the period of a transfer orbit

A simple function using the variant data type

Function y=Hohmann_P(r1, r2, mu) 
    if nargin<3; mu=6.672E-11; % (N m^2 Kg^-2) 
   y = Sqrt(mu / r1) * Sqrt(2 * r2 / (r1 + r2) - 1); 
End Function

A more complicated example illustrating how datatypes can be translated in visual basic

This example also shows how to get values from spread sheets. It was not necessary to transform the numbers into double and the Hohmann function above could be used instead to calculate the period with this same test function. However, the example was done this way to explicitly show how to convert between datatypes should the author need to. Clearly this lengths the code and the situation becomes more difficult with multidimensional data types. A better way to handle matrix and vector quantities is with an environment like MATLAB or Octave.

Sub Hohmann_test() 
     Dim AU As Range 
     Dim G As Range 
     Dim r1 As Range 
     Dim r2 As Range 
     Dim answer As Double 
     Set AU = Worksheets("Sheet1").Range("B1") 
     Set G = Worksheets("Sheet1").Range("B2") 
     Set r1 = Worksheets("Sheet1").Range("B3") 
     Set r2 = Worksheets("Sheet1").Range("B4") 
     answer = Hohmann_P(r1 * AU, r2 * AU, G) 
End Sub 
Function Hohmann_P(r1_a, r2_a, mu_a) 
     Dim r1 As Double 
     Dim r2 As Double 
     Dim mu As Double 
     r1 = DblFrmRange(r1_a) 
     r2 = DblFrmRange(r2_a) 
     mu = DblFrmRange(mu_a) 
     Hohmann_P = Sqr(mu / r1) * Sqr(2 * r2 / (r1 + r2) - 1) 
End Function 
Function DblFrmRange(a) As Double 
     If StrComp(TypeName(a), "Range") Eqv 0 Then 
           DblFrmRange = CDbl(a.Value2) 
          DblFrmRange = CDbl(a) 
     End If 
End Function