1 / 9

Using Visual Basic for Equation Solving by Iteration

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

haracha
Télécharger la présentation

Using Visual Basic for Equation Solving by Iteration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Visual Basic for Equation Solving by Iteration Introducing the Subroutine

  2. 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

  3. 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

  4. How to Solve by Iteration

  5. Newton-Raphson Iteration

  6. Central Difference Method to Estimate the Derivative

  7. Calculating the Relative Error

  8. Continue 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(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

More Related