Blog description
Welcome to my blog. Here in my blog, we will be converting mathematical equations to computer programs, specifically into visual basic for applications codes, even more specifically, using only few statements like loop, if, and operational statements. Note that the codes in the blog is just one of the possible codes we can use.
Questions, suggestions and comments are highly appreciated. You may want to post any questions regarding anything related to the topic.
Wednesday, July 4, 2012
Blog merge
I'm glad to announce that this blog will be merged to Apps, games, and more.. All upcoming blog posts will be posted in that blog. seeyou.. :)
Thursday, June 28, 2012
Topic #3: Trendline Analysis
Our next topic will be Trendline Analysis.
In this topic, we will be making a program that would require us to input the
size and the data. Then the outpout would be the equation of the trendline.
Assuming that there is no seasons
involved. To start, we need to declare all the variables we will be using. Here
are the variables:
Dim x As Single
Dim y() As Single
Dim xsum As Single
Dim ysum As Single
Dim xsqsum As Single
Dim xysum As Single
Dim n As Integer
Dim b1 As Single
Dim b0 As Single
Dim response As Single
Then, we need to put a code in order for
us to input the data and the data size.
n = Application.InputBox("Enter
number of data.")
ReDim y(1 To n) As Single
For x = 1 To n
y(x) = Application.InputBox("enter data " & x)
Next
After the data input, we need to do the
necessary solutions to solve for the trendline equation. In my work, i used the
normal equation method instead of the conventional way to minimize rounding
errors. Here is how normal equation works:
Now, if we solve
it manually, you will be using systems of equations involving two variables to
determine the value of b0 and b1.
Now for solving
using visual basic, we need to put the code below:
xsum = 0
ysum = 0
xsq = 0
xysum = 0
For x = 1 To n
xsum = xsum + x
ysum = ysum + y(x)
xsqsum = xsqsum + x ^ 2
xysum = xysum + y(x) * x
Next
b1 = (ysum * xsum - n * xysum) / (xsum ^ 2 - n * xsqsum)
b0 = (ysum - b1 * xsum) / n
Now for the output, we need to put this
code:
response = MsgBox("the equation is
y=" & b1 & "x + " & b0 & ".",
vbOKOnly)
Topic #2: Mode
The next thing we would be doing is to
create a program that will generate the mode of a given data. By definition,
mode is the number which appears most often in a set of numbers.. We would be
using the code for arranging data from the previous topic. I’ll put the code
below:
countmin = 0
For indexdata = 1 To
sizedata
If min =
data(indexdata) Then
countmin =
countmin + 1
End If
Next
For indexsort = 1 To
countmin
For indexdata = 1 To
sizedata
If min =
data(indexdata) Then
sorteddata(indexsort) = data(indexdata)
End If
Next
Next
datanum = countmin
For indexrange = 1 To
range
For indexdata = 1 To
(sizedata)
If data(indexdata)
= min + indexrange Then
sorteddata(datanum + 1) = data(indexdata)
datanum =
datanum + 1
End If
Next
Next
The code above will be be sorting the
data. Don’t forget to declare every variables. Next is to count every
repeatitions in the data. All we have to do is to input the code below:
For indexcount = 1 To sizedata
If (indexcount + 1) <= sizedata Then
If sorteddata(indexcount) = sorteddata(indexcount + 1) Then
Else
For indexdata = 1 To sizedata
If sorteddata(indexcount) =
sorteddata(indexdata) Then
count(indexcount) =
count(indexcount) + 1
End If
Next
End If
Else
For indexdata = 1 To sizedata
If sorteddata(indexcount) =
sorteddata(indexdata) Then
count(indexcount) =
count(indexcount) + 1
End If
Next
End If
Next
After getting the count of every value in
the data, we would be getting the most count. We would be using the code below:
For indexcount = 1 To sizedata
If (indexcount + 1) <= sizedata Then
If sorteddata(indexcount) = sorteddata(indexcount + 1) Then
Else
For indexdata = 1 To sizedata
If sorteddata(indexcount) =
sorteddata(indexdata) Then
count(indexcount) =
count(indexcount) + 1
End If
Next
End If
Else
For indexdata = 1 To sizedata
If sorteddata(indexcount) =
sorteddata(indexdata) Then
count(indexcount) =
count(indexcount) + 1
End If
Next
End If
Next
There would be a problem if we have
multiple modes. Because the codes given will only show only one mode. To fix
the problem, we will be inserting the code below:
If maxcount > 1 Then
countmode = 0
For indexdata = 1 To sizedata
If maxcount = count(indexdata) Then
countmode = countmode + 1
End If
Next
ReDim mode(1 To countmode) As Single
modenum = 0
For indexdata = 1 To sizedata
If maxcount = count(indexdata) Then
modenum = modenum + 1
mode(modenum) =
sorteddata(indexdata)
End If
Next
End If
Next, we would be applying the appropriate
words to our output. We just have to put the code below:
If countmode = 1 Then
isare = "is"
modesmode = "mode"
Else
isare = "are"
modesmode = "modes"
End If
The last step is to show our the results.
The code below also is flexible for bimodal and trimodal problems.
response = MsgBox("there " & isare & " "
& countmode & " " & modesmode)
For indexmode = 1 To countmode
response = MsgBox("the " & modesmode & " "
& isare & ": " & mode(indexmode))
Next
Topic #1: Median
Welcome to my blog. Here in my blog, we will be converting
mathematical equations to computer programs, specifically into visual basic for
applications codes, even more specifically, using only few statements like
loop, if, and operational statements. Note that the codes in the blog is just
one of the possible codes we can use.
Questions, suggestions and comments are highly appreciated.
You may want to post any questions regarding anything related to the topic.
Well, let’s start from statistics. In statistics, we have a
term called median. And by definition, median is the middle number (in a sorted
list of numbers). To find the Median, place the numbers you are given in value
order and find the middle number. We will be doing a program where it will
generate the median of a given group of data.
First, let’s start from the usual steps we do when doing a
program using visual basic for applications. Let’s declare every variable we
will be using. I will be discussing the variables as we go along. For now lets
start from declaring the following variables:
Dim data() As Single
Dim sizedata As Integer
The variable ‘data’ represents the array of data and
‘sizedata’ represents the size. As you can see, ‘data’ still don’t have an
exact array.
Next, we will input the size of the data. We won’t have too
much problem if the size is known. But not all data groups have the same size.
So we will be making a computer program where the user will input the size. In
order for us to do that, we will be using the code below:
Do
sizedata =
InputBox(prompt:="enter the size", Default:=0)
Loop While
sizedata = 0
I added a loop statement in the code to minimize bugs. Also,
I have set the default value to zero (0).
Here’s what the code will show you:
After entering the data size. We will need to redimension
the variable ‘data’. So we will be using the following statement:
ReDim data(1 To sizedata) As Single
Notice that we have set the dimension of the variable
‘data’. Example if ‘sizedata=3’, then we will have an array of data from 1 to
3.
data(1)
data(2)
data(3)
Next, we need to input the data in order for us to solve the
median. So I’ll be using the following codes:
For indexdata = 1 To sizedata
data(indexdata) = InputBox("enter data " & indexdata)
Next
Here’s what will be shown on the screen:
Next, we need to determine the data with the lowest value.
Now we will be using conditional statements.
min = data(1)
For indexdata = 1 To sizedata
If min >
data(indexdata) Then
min =
data(indexdata)
End If
Next
Then we will be determine the element with the highest
value. Here’s the code:
max = data(1)
For indexdata = 1
To sizedata
If max <
data(indexdata) Then
max =
data(indexdata)
End If
Next
Next we determin the range:
range = max - min
Remember to declare the variables ‘min’, ‘max’, and ‘range’.
Dim min as single
Dim max as single
Dim range as single
Now, we will be introducing more variables. The next step
that we need to do is to count the number of elements with the lowest value.
And in order for us to do that, we need to enter this code:
countmin = 0
For indexdata = 1
To sizedata
If min = data(indexdata)
Then
countmin =
countmin + 1
End If
Next
Now we need to sort the data. Let’s start from the lowest.
Then sort all the other elements using this code:
For indexsort = 1
To countmin
For indexdata
= 1 To sizedata
If min =
data(indexdata) Then
sorteddata(indexsort) = data(indexdata)
End If
Next
Next
datanum = countmin
For indexrange = 1
To range
For indexdata
= 1 To (sizedata)
If
data(indexdata) = min + indexrange Then
sorteddata(datanum + 1) = data(indexdata)
datanum = datanum + 1
End If
Next
Next
Now we are done sorting the data. Just don’t forget to
declare all the variables we have used. Now we can get the median already. But
first, we need to settle a problem. We can have the median immediately if the
data size is an odd number. But what if it’s an even? We just have to determine
if the size is even or odd using this code:
halfint = sizedata
/ 2
halfsin = sizedata
/ 2
The variables represent the half of the value of the size.
Basically, there would be no difference between the 2 except their data type.
The first will be an integer and the other will be single. By doing so, there
would be a difference in the 2 if the size is an odd number. Example:
sizedata= 5
halfint=3 (5/2)
halfsin=2.5 (5/2)
If halfsin is not equal to halfint, then the size is an odd
number. Now just declare the previous variables.
Dim halfint As
Integer
Dim halfsin As
Single
We can now determine the median. I’ve decided to use a
function for that. Just follow the code below:
median =
getmedian(sorteddata(), sizedata, halfint, halfsin)
And for the function:
Function getmedian(sorteddata() As Single, sizedata As
Integer, halfint As Integer, halfsin As Single) As Single
'logical test: if
true then sizedata is an even number else sizedata is odd
If halfint =
halfsin Then
getmedian =
(sorteddata(sizedata / 2) + sorteddata(sizedata / 2 + 1)) / 2
Else
getmedian =
sorteddata(sizedata / 2 + 0.5)
End If
End Function
There you go. We now have a program that will automatically
solve the median of a group of data. The last thing to do is to show the
resuls, use the code below:
response =
MsgBox("the median is " & median)
Subscribe to:
Posts (Atom)