Results 1 to 5 of 5

Thread: trouble with results of equation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Posts
    118

    Post

    I am having trouble with getting the proper results from an equation. I have simplified the equation for this example. The numbers on the left increment properly, but the results are wierd. The numbers on the right should two times the number on the left. ( in each column )

    Option Explicit
    'This code assumes a text box called Text1 set to multiline
    'true and a command button named Command1
    Private Sub Command1_Click()
    Dim ResultsArray() As Single 'Or whatever
    Dim g As Single
    Dim r As Single
    Dim l As Single
    Dim h As Single
    Dim v As Single
    Dim TempStr As String

    r = 20
    ReDim ResultsArray(1 To 2 * r)
    l = 72
    For h = 1 To (2 * r) - 1 Step 0.5
    v = h * 2
    ResultsArray(h) = v
    Next h
    TempStr = ""
    For h = 1 To r Step 0.5
    TempStr = TempStr & h & Chr(9) & ResultsArray(h) & _
    Chr(9) & Chr(9) & Chr(9) & _
    h + r & Chr(9) & ResultsArray(h + r) & vbCrLf
    Next h


    Text1.Text = TempStr


    End Sub

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Post Your array indexes must be integers

    I think the problem is that you aren't using whole numbers for your array indexes in the loops. I have never seen an array use singles as an index, and I don't think you can do that. (I could be wrong...?).

    I changed your code using an integer variable for your array index, so now it works just fine (although I can't for the life of me figure out what the purpose is!).

    Try this:
    Code:
    Option Explicit
    'This code assumes a text box called Text1 set to multiline
    'true and a command button named Command1
    Private Sub Command1_Click()
        Dim ResultsArray() As Single
        Dim g As Single
        Dim r As Single
        Dim l As Single
        Dim h As Single
        Dim v As Single
        Dim TempStr As String
        Dim ArrayIndex As Integer
        
        r = 20
        TempStr = ""
        ArrayIndex = 1
        
        ReDim ResultsArray(1 To 4 * r)
        
        For h = 1 To (2 * r) Step 0.5
            v = h * 2
            ResultsArray(ArrayIndex) = v
            ArrayIndex = ArrayIndex + 1
        Next h
        
        ArrayIndex = 1
        
        For h = 1 To r Step 0.5
            TempStr = TempStr & h & Chr(9) & ResultsArray(ArrayIndex) & _
            Chr(9) & Chr(9) & Chr(9) & _
            h + r & Chr(9) & ResultsArray(ArrayIndex + r * 2) & vbCrLf
            ArrayIndex = ArrayIndex + 1
        Next h
    
        Text1.Text = TempStr
    End Sub

    Edited by seaweed on 03-10-2000 at 05:01 AM
    ~seaweed

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Posts
    118

    Post Unsimplified version

    Seaweed, if you are still out there I can explain. This is a program to measure the volume of liquid in a cylindrical tank, laying horizontally. In the code below I have commented out some of your code to show you what I mean. I guess I simplified my code too much on my first submission. Your code did exactly what I wanted on the simplified versiopn, but it wont work on the real code. This assumes Command1 and Text1 set to multiline. FYI: (L)variable is the length of the tank, (r) is the radius(explaining r*2) and (h) is the height in inches of liquid in the tank. (v) is the volume. It will work with one inch increments, but I would like to use a Step variable of 1, .5, at least and maybe .250. Option Explicit
    Private Sub Command1_Click()
    Dim ResultsArray() As Single 'Or whatever

    Dim r As Single
    Dim L As Single
    Dim h As Single
    Dim v As Single
    Dim TempStr As String
    Dim Title As String
    Dim ArrayIndex As Integer
    'then when you need to use the arrays:

    r = 20
    TempStr = ""
    'ArrayIndex = 1

    ReDim ResultsArray(1 To 4 * r)

    L = 72

    For h = 1 To (2 * r) - 1 'Step 0.5
    v = (r ^ 2 * (ArcCos((r - h) / r))) _
    - (r - h) * (Sqr(2 * (r * h) - h ^ 2))
    v = l * v / 231
    v = Format(v, "######.##")
    ResultsArray(h) = v
    'ArrayIndex = ArrayIndex + 1

    Next h

    'ArrayIndex = 1

    For h = 1 To r 'Step 0.5
    TempStr = TempStr & h & Chr(9) & ResultsArray(h) & _
    Chr(9) & Chr(9) & Chr(9) & _
    h + r & Chr(9) & ResultsArray(h + r) & vbCrLf
    Next h

    Title = "This is a test"
    Text1.Text = Title & vbCrLf & TempStr


    End Sub

    'Inverse Cosine
    Function ArcCos(x As Double) As Double
    ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
    End Function


  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2000
    Posts
    118

    Post Anyone

    Does anyone know how to do this?

  5. #5
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Post Same problem, same solution...

    From what I can tell, you are still going to run into the same problem of using a single as an array index.

    Now, if the commented code that you last posted returns correct results, then we can infer that if the Step value is 1, then the upper bound of the array is 4 * r from this line of code:
    Code:
    ReDim ResultsArray(1 To 4 * r)
    We also know that if you change the step value to 0.5, that the number of elements in the array will double. If you change the step to 0.25, the number of elements will quadruple. This leads into a formula, 1/StepValue. If the step value is .5 then 1/.5 = 2 (doubling the array size), and if it's .25 then 1/.25 = 4 (quadrupling the array size). Therefore, you can use the following formula to calculate the upper bound of the array:
    Code:
    UpperBound = (1 / StepValue) * 4 * r
    What I did is in the Form_Load, ask the user what the step value will be and hold that value in a global variable (Increment). Then just plug that value into the formula and it should work. Try the following and see if you get the correct results:
    Code:
    Option Explicit
    
    Private Increment As Single   'This holds the step increment
    
    Private Sub Form_Load()
        'Step increment is assigned here. You could put this in a
        'command button also, if you wanted to try different
        'increments without starting and stoping your project each time
        
        Me.Show
        On Error Resume Next    'In case user clicks cancel
        Increment = CSng(InputBox("What increment do you want to use?"))
        If Increment <= 0 Then
            Increment = 1
        End If
    End Sub
    
    Private Sub Command1_Click()
        Dim ResultsArray() As Single
        Dim Radius As Single
        Dim Length As Single
        Dim Height As Single
        Dim Volume As Single
        Dim TempStr As String
        Dim Title As String
        Dim ArrayIndex As Integer
        
        Radius = 20
        Length = 72
        TempStr = ""
        ArrayIndex = 1
        
        ReDim ResultsArray(1 To (1 / Increment) * 4 * Radius)
            
        For Height = 1 To (2 * Radius) - 1 Step Increment
            Volume = Format(Length * ((Radius ^ 2 * (ArcCos((Radius - Height) / _
                Radius))) - (Radius - Height) * (Sqr(2 * (Radius * Height) - _
                Height ^ 2))) / 231, "######.##")
            ResultsArray(ArrayIndex) = Volume
            ArrayIndex = ArrayIndex + 1
        Next Height
        
        ArrayIndex = 1
        
        For Height = 1 To Radius Step Increment
            TempStr = TempStr & Height & Chr(9) & ResultsArray(ArrayIndex) & _
            Chr(9) & Chr(9) & Chr(9) & _
            Height + Radius & Chr(9) & ResultsArray(ArrayIndex + ((1 / Increment) _
                * Radius)) & vbCrLf
            ArrayIndex = ArrayIndex + 1
        Next Height
        
        Title = "This is a test"
        Text1.Text = Title & vbCrLf & TempStr
    
    End Sub
    
    Function ArcCos(x As Double) As Double
        ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
    End Function
    Let me know if this doesn't work! We'll keep plugging away at it.

    ~seaweed
    ~seaweed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width