our ferative process may require fewer iterations or more iterations and we want to make sure that our sub can
be adaptable.
Now, we are going to adapt our Iteration sub such that it will keep iterating until a certain tolerance is met. The
flowchart for this is shown on the next page.
CHEN 1310
BIO
begin
inputx
Lab 900
Tel-0.001
xnew-x)
Er-x-
E
Tol?
And the code for this new flowchart is shown here:
xxnow
Loop Until Err Pol
MogBox FormatNumber (3)
End Sub
option Explicit
Sub Iteration ()
Din xinit As Double, x As Double
Din Err As Double, Tol As Double, new As Double
xinit InputBox("Please enter initial guess.")
x-xinit
701 = 0.001
Do
xnow = sqr(x (1/3) + 5*x)
Err- Abs (xnew- x)
output x
and
Page 3
Adapt your code to that shown above. Notice that we have a Do...Loop Until in which we calculate an error
(Err) and compare it to a tolerance (Tol). Tol is a value that can be set by the user-we iterate until (and hence
the Loop Until)-the difference between subsequent x-values is less than or equal to Tol. Note that we also
need to add Dim statements for Err, Tol, and xnew. We have eliminated in and I and the Dim statements for
n and I because they are no longer needed. Now would be a good time to save!/nStep through (using FB) your code to make sure it is working. Enter 4 as the initial guess. You should have
gotten the same answer as you did before using a fixed number of iterations.
The code is a bit more involved, but why do you think implementing the loop might be more advantageous than
a fixed number of terations?
CHEN 1310
Lab 905
Page 4
What is the significance of xnew? In other words, why do we need to define xnew as a separate variable?
Fig: 1
Fig: 2