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 2315
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 default is 4
letters).
This is the Fed. Everything is a TLA (three-letter acronym).
Therefore, since I'm building a PORTABLE web application,...
|
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 number. That number
will appear in a seperate box at the bottom. So basically whatever you choose
has a corresponding number associated with it...
|
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 difficult to know what
is going on.
One of these Order Forms you can see here...
http://www.cardman.co.uk/orderform.php3
|
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 to figure out how expand this script to recalculate when rows
are added or
removed.
My code so far.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML...
|
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 both A and B have the exact the same value. Is this method incorrect?
When I make the function wait for 1 millisecond, A en B differ. Is there
| |
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 user types I want script to iterate trough form count and sum
non-empty fields and than divides this two numbers.I need to sum values
from fields that...
|
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 then
|
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>) (or in a more object-like
display: set(obj1.choices=set(a, b, c) ). There may be obj1..objN
objects in the outer set, and the amount of items in the...
|
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' button to get a list of all the Cases that were 18 yrs old on the year selected.
The way I want to do this is, invoke an event on 'Find' button...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |