how to calculate the number of looping?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vbwire
    New Member
    • Jan 2008
    • 19

    how to calculate the number of looping?

    i use vb 6.0


    [CODE=vb]
    Option Explicit

    Dim Error As Double
    Dim x As Integer
    Dim y As Double
    Dim z As Integer
    Dim eps As Double
    Dim N As Integer

    Private Sub cmdCalculate_Cl ick()

    'user input

    If IsNumeric(txtN) And IsNumeric(txtEp s) And IsNumeric(txtX) Then
    N = txtN.Text
    eps = txtEps.Text
    x = txtX.Text
    Else
    MsgBox "all input must be numeric", vbExclamation, "numeric test"
    txtN.SetFocus
    End If

    'calculate

    Do
    y = (x) - (x - (N ^ (1 / 2)))

    Error = y - x
    If Error < 0 Then
    Error = Error * -1
    End If

    z = z + 1

    Loop Until Error < eps

    'display the result
    lblDisplay.Capt ion = "root : " & y & " iteration : " & z



    End Sub

    Private Sub cmdExit_Click()
    Unload Me
    End Sub
    [/CODE]

    the problems..

    the 'eps' is the decimal places.in my program above it need user to insert the number of decimal places like this '0.001'.. but actually i just want the user put the value as '3'...

    the iteration value must be in integer data type as the result of number of looping it takes to complete the loop.. the number of looping (iteration) didn't match its actually value..
    Last edited by debasisdas; Feb 27 '08, 09:13 AM. Reason: added code=vb tags
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi vbwire. It would be helpful to know what the loop calculation is for. When I test it the calculated error value remains constant and the loop does not exit.

    I wonder if your reference to root N (N ^ (1/2)) in the loop is correct? N is not changing after it is entered by the user, and I guess there should a use of the loop counter z somewhere to change the value of N if the iterative calculation is intended to reduce the error value below the loop threshold. This is just a guess, though.

    To refer to the number of decimal places for the loop threshold as an integer value you could define a variable NDecimals as an integer, get the value of NDecimals from the user instead of the double eps, then replace your loop exit test with
    [CODE=vb]Loop Until Error < 10^(-NDecimals)[/Code]
    Another change is to use the Abs (absolute value) function to replace
    [CODE=vb]Error = y - x
    If Error < 0 Then
    Error = Error * -1
    End If[/CODE]
    with
    [CODE=vb]Error = abs(y-x)[/CODE]
    Regards

    Stewart

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      As far as I could see, z should correctly return the number of iterations.

      As for the calculation, I haven't looked into it in detail. But if you have any Integer data types in there, VB may convert interim results to Integer, thus losing precision. Try using all Double data types in there. And that includes for the literal values. If you say "1#" instead of just "1", you will force VB to use double data type for the value. In any case, why use (1/2), when you could just use the actual value, 0.5?

      Oh, one other thing. Between lines 20 and 21, I believe you should Exit Sub.

      Comment

      • vbwire
        New Member
        • Jan 2008
        • 19

        #4
        Originally posted by Stewart Ross Inverness
        Hi vbwire. It would be helpful to know what the loop calculation is for. When I test it the calculated error value remains constant and the loop does not exit.
        the loop calculate the value for y until the different between y and x is smaller than 'eps' ..

        z is the number of looping it takes to complete the loop..

        the value of x will take the value of y for the next looping.. for example in the first looping, x=2, then get the value of y.. then it will loop,use the value of y we get to calculate the new y. its continue give a new y until the different of the new y and y before it smaller than 'eps'

        Comment

        • MikeTheBike
          Recognized Expert Contributor
          • Jun 2007
          • 640

          #5
          Originally posted by vbwire
          the loop calculate the value for y until the different between y and x is smaller than 'eps' ..

          z is the number of looping it takes to complete the loop..

          the value of x will take the value of y for the next looping.. for example in the first looping, x=2, then get the value of y.. then it will loop,use the value of y we get to calculate the new y. its continue give a new y until the different of the new y and y before it smaller than 'eps'
          Hi
          I started to look at this yesterday, but work got in the way (such a pain!).

          Apart from the use of Abs() and changing the loop exit condition to

          Loop Until Error < 0.1 ^ eps

          there does not seem to be any iteration in the loop, ie. nothing changes from one loop to the next! So I've moded the code as follows:-

          [code=vb]
          Option Explicit

          Dim Error As Double
          'Dim x As integer
          Dim x As Double
          Dim y As Double
          Dim z As Integer
          Dim eps As Double
          Dim N As Integer

          Private Sub cmdCalculate_Cl ick()

          Dim txtX As Integer
          'Dim txtY As Double
          Dim txtZ As Integer
          Dim txtN As Integer
          Dim txtEps As Integer


          'user input
          txtX = 3
          txtZ = 5
          txtN = 1000
          txtEps = 3

          z = 0
          If IsNumeric(txtN) And IsNumeric(txtEp s) And IsNumeric(txtX) Then
          N = txtN
          eps = txtEps
          x = txtX
          Else
          MsgBox "all input must be numeric", vbExclamation, "numeric test"
          'txtN.SetFocus
          Exit Sub
          End If

          'calculate

          Do
          y = (x) - (x - (N ^ (1 / 2)))

          Error = Abs(y - x)
          x = y
          z = z + 1

          Loop Until Error < 0.1 ^ eps

          'display the result
          'lblDisplay.Cap tion = "root : " & y & " iteration : " & z
          MsgBox "root : " & y & " iteration : " & z

          End Sub[/code]

          ie have added x=y in the loop (as you indicted in the last post), but this always calculated the squre root of N in 2 iterations !!??

          I still don't know what you are trying to do ?

          Is this homework ?

          ps. as this is not written in a form module I've hard coded values of txtN etc.



          MTB
          Last edited by debasisdas; Feb 27 '08, 09:14 AM. Reason: added code=vb tags

          Comment

          • vbwire
            New Member
            • Jan 2008
            • 19

            #6
            thank to both of you..

            this is the program after i corrected it..

            [CODE=vb]
            Option Explicit

            Dim Error As Double
            Dim x As Double
            Dim y As Double
            Dim z As Integer
            Dim eps As Double
            Dim a As Integer
            Dim N As Integer

            Private Sub cmdCalculate_Cl ick()

            'user input

            If IsNumeric(txtN) And IsNumeric(txtEp s) And IsNumeric(txtX) Then
            N = txtN.Text
            a = txtEps.Text
            x = txtX.Text
            Else
            MsgBox "all input must be numeric", vbExclamation, "numeric test"
            txtN.SetFocus
            End If

            'calculate

            eps = (10 ^ (-a))

            Do
            y = x - (x - (N ^ (1 / 3)))

            Error = Abs(y - x)

            x = y

            z = z + 1

            Loop Until Error < eps

            'display the result
            lblDisplay.Capt ion = "root : " & y & " iteration : " & z

            End Sub

            Private Sub cmdExit_Click()
            Unload Me
            End Sub

            [/CODE]

            please look at my code for displaying the result.. how to make it in two line but i want it in same label.

            also.. i want to display the result for root in the decimal palaces chosen by user,'eps'..
            Last edited by debasisdas; Feb 27 '08, 09:19 AM. Reason: added code=vb tags

            Comment

            • vbwire
              New Member
              • Jan 2008
              • 19

              #7
              Originally posted by MikeTheBike

              ie have added x=y in the loop (as you indicted in the last post), but this always calculated the squre root of N in 2 iterations !!??

              I still don't know what you are trying to do ?

              Is this homework ?

              ps. as this is not written in a form module I've hard coded values of txtN etc.



              MTB
              thank you mike.. =)

              i just realise my mistake in the calculation..

              [CODE=vb]y = (x) - (x - (N ^ (1 / 2)))
              [/CODE]
              its actually..

              [CODE=vb]y = x - (x - (N ^ (1 / 3)))
              [/CODE]

              and check my previous post.. i still have something for you..haha..
              Last edited by debasisdas; Feb 27 '08, 09:20 AM. Reason: added code=vb tags

              Comment

              Working...