VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Compute a root of an equation(function of 1 variable) using secant method. Subject : Methods of Num

by BasuDip (5 Submissions)
Category: Math/Dates
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sun 24th December 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Compute a root of an equation(function of 1 variable) using secant method. Subject : Methods of Numerical Analysis

Rate Compute a root of an equation(function of 1 variable) using secant method. Subject : Methods of Num



Dim Xp As Single, Xn As Single

Private Sub Form_Load()
Xp = 1: Xn = 2                      '                               Initial Values
End Sub
Rem     Modify here to type in the equation and set the initial values  ******
Private Function myFunc(ByVal X As Single) As Single
myFunc = X * X - Sqr(X) - 2     '                               Let F(x)=x*x-x^0.5-2
End Function

Private Function SecantRootX(ByVal X0 As Single, _
                                         ByVal X1 As Single) As Single
Dim F0 As Single, F1 As Single
    If X0 = X1 Then
    SecantRootX = X0
    Exit Function
    End If
F0 = myFunc(X0)
F1 = myFunc(X1)
SecantRootX = (X0 * F1 - X1 * F0) / (F1 - F0)
End Function
Private Sub Form_Activate()
Dim nextXroot As Single, xR As Single, fR As Single, iCount As Integer
iCount = 1
Print "================================================================="
Print "X0= " & FormatNumber(Xp, 5) & vbTab & vbTab & "|" & vbTab & vbTab & "F(X0)= " & FormatNumber(myFunc(Xp), 5)
Print "------------------------------------------------------------------------------------------------------------"
Print "X1= " & FormatNumber(Xn, 5) & vbTab & vbTab & "|" & vbTab & vbTab & "F(X1)= " & FormatNumber(myFunc(Xn), 5)
Do While Abs(Xp - Xn) > 0
nextXroot = SecantRootX(Xp, Xn)
xR = FormatNumber(nextXroot, 5)
fR = FormatNumber(myFunc(nextXroot), 5)
Print "------------------------------------------------------------------------------------------------------------"
iCount = iCount + 1
Print "X" & iCount & "= " & xR & vbTab & vbTab & "|" & vbTab & vbTab & "F(X" & iCount & ")= "; fR
If fR = 0 Then Exit Do
DoEvents
Xp = Xn: Xn = nextXroot
Loop
Print "=================================================================="
Print " Thus the root of the equation is " & vbTab & FormatNumber(nextXroot, 5) & " ."
End Sub

Download this snippet    Add to My Saved Code

Compute a root of an equation(function of 1 variable) using secant method. Subject : Methods of Num Comments

No comments have been posted about Compute a root of an equation(function of 1 variable) using secant method. Subject : Methods of Num. Why not be the first to post a comment about Compute a root of an equation(function of 1 variable) using secant method. Subject : Methods of Num.

Post your comment

Subject:
Message:
0/1000 characters