LECTURE (7) Mathematics Functions
Mathematics Functions
What are the functions:
Functions are names reserved and defined by Visual Basic to perform a specific
action, such as arrays, absolute values, and others. Or, in a more superficial and more
general sense, it is a small pre-written program stored within the Visual Basic
language that can be called through your program and benefit from it. In addition to
that, you can write a function and call it more than once inside your program. There
are several types of functions, including mathematical functions.
Mathematical functions
Abs function: finds the absolute value of any number, and the absolute value is the
value of the number without a sign. For example, the absolute value of (-13) is (13),
and so on. For example, if we write the following code.
A=Abs (-45.6)
[Link] = A
The result of executing the function is A=45.6
Note that the value entered for the function must be a number, so if the value entered
for the function is Null, the result will be Null. If the value entered for the function is
an empty variable or no value is assigned to it, the result will be an error.
The Text box can be used to enter any value into the program, and the Label can be
used to output any output:
[Link] = Abs ([Link])
Sqr Function: This function is used to determine the square root of a specific number
and takes the following general form.
A=Sqr(25)
The result of executing the function is A=5
Log Function: This function is used to determine the value of the decimal logarithm
of a number and takes the following general form:
A=Log (20)
The result of executing the function is A=2.9957327
Exp Function: This function is used to determine the value (e), which is the base of
the natural logarithm raised to the power of the number it contains, where (e) is
approximately equal to 2.7182818 and takes the following general form:
A=Exp (number)
1
LECTURE (7) Mathematics Functions
Rnd Function: This function is used to generate random numbers between zero and
one with a maximum of 15 decimal digits and takes the following general form:
A=Rnd
For example, the Rnd function may give a number such as 0.7055475, and when
rerunning the function, it produces another number such as 0.533424, and so on.
To generate random numbers from one to ten.
A=Int (10 * Rnd) + 1
And to generate random numbers from one to one hundred.
A=Int (100 * Rnd) + 1
Sin Function: This function is used to determine the value of the sine of a specific
angle and takes the following general form:
A=Sin (number)
Cos Function: This function is used to determine the cosine value of a particular
angle and takes the following general form:
A=Cos (number)
Tan Function: This function is used to determine the value of the tangent of a
particular angle and takes the following general form:
A=Tan (number)
Atn Function: This function is used to calculate the common tangent "cot" of a
number that includes an expression in radians and takes the following general form:
A=Atn (number)
Round Function: It is the rounding function through which you can specify the
number of decimal digits and takes the following form:
A= Round (4.52696,2)
Where 4.52696 is the number to be rounded, and the number 2 is the number of
places to be rounded after the decimal point, and the result will be 4.53
Int Function: This function is used to calculate only the integer part of a number that
includes integers and decimals, or in other words, to delete decimal numbers after the
decimal point without rounding, and takes the following form:
A=Int (332.54)
The result of executing the function is A=332
2
LECTURE (7) Mathematics Functions
Fix Function: It is similar to the Int function, but it is used to calculate the integer
part only and takes the following form:
A= Fix (4.52696)
The result of executing the function is A=4
Val Function: which converts text strings into a numeric value, and is the opposite of
the Str() function, which converts numbers into text strings and takes the following
form:
A= Val ([Link])
Mathematical Calculations
The operations used in arithmetic expressions are addition +, subtraction -,
multiplication *, division /, integer division \, power ^, and brackets ( ). For example,
mathematical expressions are written in BASIC, as follows:
in BASIC mathematically
3 cos(2 x)
3*cos(2*x)/(1+x^2)
1 x2
1 t3
(1+t^3)/sin(4*t+7)
sin(4t 7)
| x4|
abs(x-4)/(x^2+6)
x2 6
sqr(1+a*b) 1 ab
The priority of calculations
Priority means the sequence of operations that are executed first. For example, the
statement: We cannot determine its value before knowing the execution order of the
arithmetic operations in the statement. As in most programming languages, the
priorities for executing arithmetic operations in Visual Basic are as follows: -
1- Unpack the brackets ()
2- Foundation lifting operations.
3- Multiplication or division operations, or whichever comes first.
4- Divide integers.
5- Addition or subtraction or whichever comes first.
For example, the result of the following equations is as follows:
1) 2 + 3 * 2 8
1
2
2) 2 * 3 ^ 2 + 1 19
1
2
3
3
LECTURE (7) Mathematics Functions
Example 1: It is required to create a program to calculate the volumetric and
gravitational flow rates in one program, with the addition of a key to close the
program, noting that the information entered into the program is diameter, speed and
Density. Note that:
Area=(3.14/4) * Diameter2
Volumetric flow rate =Area * Velocity
Mass flow rate = Density * Volumetric flow rate
To make the program, we need three text boxes to enter the values of diameter,
velocity in the tube, and Density of the liquid. We also need a set of Labels and two
Command buttons. Then we set the properties of the tools as in the following:
Dear Student: In the current example and subsequent examples, we will not explain
the design process and adjusting the properties of the tools because that has become
obvious to you.
After setting the properties, write the following code:
4
LECTURE (7) Mathematics Functions
When entering the values of diameter, speed and Density in the text boxes and executing
the program, the following occurs:
Example 2: A program is required to compute the values of sine, cosine, and
tangent of a particular angle, find random values, and zero the values.
In the beginning, put two text boxes to enter the value of the angle and output the
result and five Command buttons to execute commands. Then set the properties as
shown in the following figure.
5
LECTURE (7) Mathematics Functions
Type the code in the following figure:
Executed when the Sin key is pressed
Executed when the Cos key is pressed
6
LECTURE (7) Mathematics Functions
Executed when the Tan key is pressed
Executed when the clear key C is pressed
Executed when the Rnd key is pressed
7
LECTURE (7) Mathematics Functions
Example 3: A program is required to calculate the flow rate and concentrations of
materials entering the distillation tower to separate materials B and A based on the
values of flow rates and concentrations of materials leaving the top of the distillation
tower (the dripping) and the bottom of the capturing tower (the remaining).
In this program, we will use the Shape tool to draw a rectangle and the Line tool to
draw lines, and then add the Text, Label, and Command tools, as shown in the
following figure:
8
LECTURE (7) Mathematics Functions
Set the properties of the form and its objects, as shown in the following figure:
Write the programming as follows:
9
LECTURE (7) Mathematics Functions
When the program is executed, the following occurs:
11
LECTURE (7) Mathematics Functions
Homework
Q1: It is required to create a program to calculate the vapour pressure of any
substance, with the addition of a key to zero the values. Note: The values of the
constants A, B, and C are entered, as well as the temperature values.
Pe = exp (A- B/(T+C))
Q2: Write a program to convert the temperature from Celsius to:
1 - Fahrenheit TF = TC * 1.8 + 32
2 -Kelvin TK=TC+273
3 -TR=Tf+460
Note: Round the value of (TR) to the nearest two decimal places.
11