90 likes | 196 Views
Using Visual Basic for Equation Solving by Iteration. Introducing the Subroutine. If, Then, Else Statement. If(a < b) If a is less than b If(a = b) If a equals b If(a<>b) If a is not equal to b If(a<=b) If a is less than or equal to b If(a>=b) If a is greater than or equal to b
E N D
Using Visual Basic for Equation Solving by Iteration Introducing the Subroutine
If, Then, Else Statement If(a < b) If a is less than b If(a = b) If a equals b If(a<>b) If a is not equal to b If(a<=b) If a is less than or equal to b If(a>=b) If a is greater than or equal to b If(a>b) Then If a is greater than b do these statements ElseIf (a=b) 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
The 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
Continue to iterate until relerror is within some specified tolerance or until the number of iterations exceed some predetermined limit.
Public tiny Public Function NewtonRaphson(arguments, xguess, tolerance, iter) x = xguess tiny = 1E-20 For i=1 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 < 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)/(2*0.01*(x + tiny)) End Sub