Title: Using Visual Basic for Equation Solving by Iteration
1Using Visual Basic for Equation Solving by
Iteration
- Introducing the Subroutine
2If, Then, Else Statement
If(a lt b) If a is less than b If(a b) If a
equals b If(altgtb) If a is not equal to
b If(altb) If a is less than or equal to
b If(agtb) If a is greater than or equal to
b If(agtb) Then If a is greater than b do
these statements ElseIf (ab) Then If a is
equal to b do these statements Else a is
neither greater do these statements than nor
equal to b End if
3The Subroutine
Public Function Functionname(arguments) Make
calculations Call Subroutinename(b1, b2, b3, ,
bn) Functionnname some number End
Function Public Sub Subroutinename(a1, a2, a3,
,an) Make calculations based on the arguments
passed to subroutine End Sub
4How to Solve by Iteration
5Newton-Raphson Iteration
6Central Difference Method to Estimate the
Derivative
7Calculating the Relative Error
8Continue to iterate until relerror is within some
specified tolerance or until the number of
iterations exceed some predetermined limit.
9 Public tiny Public Function NewtonRaphson(argumen
ts, xguess, tolerance, iter) x xguess tiny
1E-20 For i1 to iter Call func(arguments,x,
fx) Call deriv(arguments, x, dfdx, tiny) xnew
x fx/dfdx relerror abs(xnew - x)/(x
tiny) If(relerror lt tolerance) Then Exit
For Else x xnew Endif Next i NewtonRaphson
xnew End Function Public Sub func(arguments, x,
fx) Insert function here End Sub Public Sub
deriv(arguments, x, dfdx) Call func(arguments,
1.01(x tiny), fhigh) Call func(arguments,
0.99(x tiny), flow) dfdx (fhigh-flow)/(20.01
(x tiny)) End Sub