|
-
Apr 26th, 2019, 11:49 AM
#7
New Member
Re: Goal Seek in VB
Check if the two-steps coding below work:
Private Sub Command1_Click()
'GoalSeek code for Visual Basic 6.0
'This code aims to find the approximate value of x of y(x)=0
y0 = 10 ^ 2 'set infinite
'Step1: given x from [0;1000], find the minimum value of y(x)
'the range of x is choisen positive since we want only positive answer of x
'the range of x shall be decided by user to be close to the expected answer x
'Loop i, j ,k, m,.... for tolerant value of x (for exampe x=0.001)
For i = 0 To 10
For j = 1 To 9
For k = 1 To 9
For m = 1 To 9
x = i + j / 10 + k / 100 + m / 1000
yx = Math.Abs(3 * x ^ 2 + 2 * x - 9 / x)
If yx > y0 Then y = y0
If yx <= y0 Then y = yx
y0 = y 'important to go back to next x
'If y = 0 Then GoTo 10
'MsgBox ("y=") & y
Next m
Next k
Next j
Next i
'The answer is the minimum value of y(x) within x=[0,10^10]
'Step2: with minimum value of y, find x that is correspond to that minimum value of y
'that x is the answer of goalseek function.
For i = 0 To 10
For j = 1 To 9
For k = 1 To 9
For m = 1 To 9
x = i + j / 10 + k / 100 + m / 1000
yx = Math.Abs(3 * x ^ 2 + 2 * x - 9 / x)
If yx = y Then GoTo 10
Next m
Next k
Next j
Next i
10
MsgBox ("x=") & Format(x, "0.0000")
MsgBox ("y=") & Format(y, "0.0000")
End Sub
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
|