|
-
Jun 17th, 2013, 11:24 AM
#10
Re: Trial and error process to find a variable that its function value is known
OK, there are a lot of problems with this code. The first of which is that calling this trial and error is a little misleading. Trial and error is for middle school math... what you are attempting to do is much more sophisticated, and has a much more sophisticated name: binary search.
Now that we have a name for what you are doing, let's compare your code to standard binary search pseudocode (taken directly from Wikipedia):
Binary Search Pseudocode:
Code:
int binary_search(int A[], int key, int imin, int imax)
{
// continue searching while [imin,imax] is not empty
while (imax >= imin)
{
/* calculate the midpoint for roughly equal partition */
int imid = midpoint(imin, imax);
// determine which subarray to search
if (A[imid] < key)
// change min index to search upper subarray
imin = imid + 1;
else if (A[imid] > key)
// change max index to search lower subarray
imax = imid - 1;
else
// key found at index imid
return imid;
}
// key not found
return KEY_NOT_FOUND;
}
Your code:
Code:
Public T1, T2, T3, h1, h2, h3, h_dis, T_dis As Single
Public Sub Tdiscalcs()
' h is incremental between 0 < T < 220 Celcius
T1 = 20 'Celcius
T2 = 220
T3 = (T1 + T2) / 2
h3 = enthalpySatVapTW(T3 + 273.15)
h1 = enthalpySatVapTW(T1 + 273.15) 'kJ/kg
h2 = enthalpySatVapTW(T2 + 273.15)
If (h_dis < h1) Or (h_dis > h2) Then
Call MsgBox("Out of range outlet Enthalpy", vbOKCancel + vbExclamation, "Range Error !")
End If
If (h_dis > h1) And (h_dis < h3) Then
Do While Abs(h3 - h_dis) > 1
T2 = T3
T3 = (T1 + T2) / 2
h3 = enthalpySatVapTW(T3 + 273.15)
Loop
ElseIf (h_dis > h3) And (h_dis < h2) Then
Do While Abs(h3 - h_dis) > 1
T1 = T3
T3 = (T1 + T2) / 2
h3 = enthalpySatVapTW(T3 + 273.15)
Loop
End If
T_dis = T3
End Sub
So there are numerous problems with your code. Let's start with some VB6/programming basics first:
- Always declare Option Explicit at the start of a module. It's just good practice
- use Doubles instead of Singles, which gets you better precision, especially since this is a scientific application
- This is a single subroutine. Get rid of all the global variables! Instead of "Public T1, T2, T3, h1, h2, h3, h_dis, T_dis As Single" outside of Tdiscalcs(), use "Dim T1, T2, T3, h1, h2, h3, h_dis, T_dis As Single" inside of Tdiscalcs()
- But not so fast... when you declare variables in line like "Dim T1, T2, T3, h1, h2, h3, h_dis, T_dis As Single", T_dis is a Single, but all the other ones are Variants, which is clearly not your intention, so instead declare them like this (and remember to use Double, not Single): "Dim T1 As Double, T2 As Double,... h_dis As Double, T_dis As Double". Better yet, declare each variable on its own line.
- This routine accepts a single value as input and calculates a single value as output. Why is this a Subroutine? This should be a Function, just like enthalpySatVapTW() is a Function: Public Function TDiscalcs(h_dis As Double) As Double
- It would be helpful to use more descriptive names. For example, instead of T1, T2, and T3, use something like dblLowTemp, dblHighTemp and dblMidTemp
- Constant values, such as the difference between absolute zero in Kelvin and Celsius (273.15), should be stored as a constant: Private Const KELVIN_CONVERSION As Double = 273.15
Now let's compare the algorithms:
- You have just one problem, but it's pretty major: In the pseudocode, there is a single loop, with an If-Else statement inside. In your code, there is a single If-Else statement with loops inside each branch. That's your problem. You need to change those loops into a single outer loop and have your If-Else inside it. That's it!
- Also, a little helpful tip. I find it easier, in scientific applications like this in VB6, to use square powers instead of absolute values. Squaring a number gets rid of negative values, and in VB6, lets you avoid Type conversion issues with the Abs() function, which uses Variants.
So now that you know where your mistake is, it's your job to fix it. Good luck!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|