i use vb 6.0 -
-
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_Click()
-
-
'user input
-
-
If IsNumeric(txtN) And IsNumeric(txtEps) 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.Caption = "root : " & y & " iteration : " & z
-
-
-
-
End Sub
-
-
Private Sub cmdExit_Click()
-
Unload Me
-
End Sub
-
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..
6 2309
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 - Loop Until Error < 10^(-NDecimals)
Another change is to use the Abs (absolute value) function to replace - Error = y - x
-
If Error < 0 Then
-
Error = Error * -1
-
End If
with
Regards
Stewart
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.
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'
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:- -
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_Click()
-
-
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(txtEps) 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.Caption = "root : " & y & " iteration : " & z
-
MsgBox "root : " & y & " iteration : " & z
-
-
End Sub
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 to both of you..
this is the program after i corrected it.. -
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_Click()
-
-
'user input
-
-
If IsNumeric(txtN) And IsNumeric(txtEps) 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.Caption = "root : " & y & " iteration : " & z
-
-
End Sub
-
-
Private Sub cmdExit_Click()
-
Unload Me
-
End Sub
-
-
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'..
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.. - y = (x) - (x - (N ^ (1 / 2)))
-
its actually.. - y = x - (x - (N ^ (1 / 3)))
-
and check my previous post.. i still have something for you..haha..
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Phil Powell |
last post by:
Relevancy scores are normally defined by a MySQL query on a table that
has a fulltext index. The rules for relevancy scoring will exclude
certain words due to their being too short (minimum...
|
by: Building Blocks |
last post by:
Hi,
All I need is a simle calculate form script which contains this:
A script that can handle text input, radio buttons, checkboxes, and
dropdowns. Each one of these variables will contain a...
|
by: Cardman |
last post by:
Greetings,
I am trying to solve a problem that has been inflicting my self
created Order Forms for a long time, where the problem is that as I
cannot reproduce this error myself, then it is...
|
by: rick |
last post by:
Can anyone help, I am try to create a simple form using a table, where a
user can fill out
quanty and price and have a total automatically calculated and inserted in
another field.
I stuck trying...
|
by: Qwert |
last post by:
Hello,
I do:
Debug.WriteLine("A: " & DateTime.Now.Ticks.ToString)
REM Calculate a bunch of stuff. Some loops and math functions.
Debug.WriteLine("B: " & DateTime.Now.Ticks.ToString)
but...
|
by: milan.letic |
last post by:
Hello,
I believe that this has been answered somewhere but I cannot find it. I
have 51 textboxes on page. In 50 I can enter value. 51st should tell me
what is average of entered numbers.
As...
|
by: Ifoel |
last post by:
Hi all,
Sorry im beginer in vb.
I want making programm looping character or number.
Just say i have numbers from 100 to 10000.
just sample:
Private Sub Timer1_Timer()
if check1.value= 1...
|
by: happyhondje |
last post by:
Hello everyone,
I've got a little issue, both programming and performance-wise. I have
a set, containing objects that refer to other sets. For example, in a
simple notation: (<a, b, c>, <d, e>)...
|
by: AccessHunter |
last post by:
Hi,
I have a table (Admittance) that has birth dates of all cases admitted, fields being (Case Number and Birth Date). I also have a form that asks the user to select a year and click the 'Find'...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |