HyperMath

HMath-2000: Working with HyperMath – Arithmetic and Relational Expressions and Control Structures

HMath-2000: Working with HyperMath – Arithmetic and Relational Expressions and Control Structures

Previous topic Next topic No expanding text in this topic  

HMath-2000: Working with HyperMath – Arithmetic and Relational Expressions and Control Structures

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

In this tutorial, expressions and control structures in the HyperMath scripting language will be examined.

Step 1: Launch HyperMath.

1.From the Start menu, select All Programs > Altair HyperWorks > HyperMath.

This launches the HyperMath in the HyperMath GUI.  Notice that by default, a file named Untitled1.hml exists in the Editor window.  By default, the Authoring Mode is displayed as well.

tut1.1_fig1

Step 2: Using the Editor window, initialize a variable and print to the HMath window.

1.In the Editor window, under the Untitled1.hml tab, enter the following line:

a=100

This declares the variable a as a base variable and sets its value to 100.

2.Enter the following line to print the variable a to the HMath window:

print(a)

3.Click Run File, run_file_icon, to execute the two commands.  In the HMath window, notice that the value of a, which is 100, is printed.
4.Save the file by going to File > Save As.  Browse to the Desktop and save the file as Tutorial_2000.hml.

The tab in the Editor window now says Tutorial_2000.hml.

Step 3: Update the script file to use the if Control Structure as well as Arithmetic and Relational Expressions.

1.In the Editor window, under the Tutorial_2000.hml tab, an if statement is added.  Start the if statement on line 3 of the script file.  For the initial if statement and the body to be evaluated if it is true, enter the following 2 lines:
if a<90 then
b=a*(a-50)

The initial if statement declares that if the variable a is less than 90, then execute the body.  Notice that a Relational Expression (<) has been used in the if statement. In the body which gets executed, if the if statement is true, notice that a new variable (b) has been created.  Variable b is also declared as a base variable and its value is an Arithmetic Expression based on the value of variable a.

2.Next, the elseif statement as well as the body to the statement are added.  Enter the following two lines into Tutorial_2000.hml:
elseif a==90 then
b=a

This elseif statement is the second condition of the if statement that is reached if the initial if statement isn’t true. This elseif statement declares that if the variable a is equal to 90, then set the value of global variable b to a. The Relational Expression, ==, is used in the elseif condition.

3.Finally, the else statement is entered.  This is the statement that gets executed if neither the if or the elseif conditional expressions are true.  Enter the following two lines in Tutorial_2000.hml:
else
b=a*50

Notice that the value of variable b is an Arithmetic Expression of variable a.

4.To finish the if statement, the end command must be used.  Enter the following on line 9 in Tutorial_2000.hml:

end

The commands entered to this point in Tutorial_2000.hml should look like the following:

tut2.1_fig2

5.Click Run File, run_file_icon , to execute the commands.  In the HMath window, notice that only the value of a is printed.

Step 4:  Add a command to print the value of "b" to the HMath window.  Also, modify the value of "a" to see how the different conditions to the if statement are evaluated.

1.Add the following line at line 10.

print(b)

2.Click Run File, run_file_icon.  That the value of a (100) as well as the value of b (5000) is printed to the HMath window.  Because the value of a is 100, neither the if condition nor the elseif condition were true, therefore the else statement was evaluated.
3.Next, the value of variable a will be edited to see the different conditions to the if statement.  On line 1 of Tutorial_2000.hml, change the value of a from 100 to 90. Notice that by changing the value to 90, the elseif statement will be met and therefore will set the value of b.
4.Click Run File, run_file_icon.  The value of a (90) as well as the value of b (90) is printed to the HMath window.
5.Once again, update the value of a in line 1.  This time, update the value from 90 to 40. This will ensure that the initial if statement is met to determine the value of b.
6.Click Run File. The value of a (40)as well as the value of b (-400) is printed to the HMath window.

Step 5: Save the script Tutorial_2000.hml.

1.From the File menu, click Save File to save the current file Tutorial_2000.hml.

See Also:

HMath-1000: Editing, Executing, Saving, and Plotting in HyperMath

HMath-1010: Working with HyperMath Authoring Mode

HMath-1020: Working with HyperMath Debugging Mode

HMath-2010: Working with HyperMath – Logical and Relational Expressions and Control Structures

HMath-2020: Working with HyperMath – Functions and Matrix Operators

HMath-2030: Working with HyperMath – Plot Commands

HMath-3000: Working with HyperMath – String Library

HMath-3010: Working with HyperMath – Input/Output Library

HMath-3020: Working with HyperMath – Input/Output Library Continued

HMath-3030: Working with HyperMath – Batch Mode

HMath-4000: Using HyperMath Functions for Curve Fitting

HMath-4001: Using HyperMath for Material Characterization

HMath-4010: Solving Ordinary Differential Equations

HMath-4020: Solving Differential Algebraic Equations

HMath-4030: Optimization Algorithms in HyperMath

HMath-5000: Using HyperMath in HyperView Results Math

HMath-5001: Post Processing Results from FEA

HMath-5002: Registering a Function in HyperGraph 2D

HMath-5003: HyperMesh-HyperMath Cross Execution of a Tcl Script

HMath-5004: HyperMesh-HyperMath Cross-debugging of a Tcl Script