Search for question
Question

4. We are now going to adapt our iteration sub such that it can act on any function f(x) (and not just the one we've

used in our example on the previous pages). We'll do this two different ways, the second way is really slick!

Subs and functions can "call other subs and functions from within them. We are going to create a separate

function fund that we can call from within our Iteration sub. Adapt your code for Iteration to be the following

and also add a new function func (can be in the same module, just below Iteration, or could be in a separate

module but in the same workbook).

option Explicit

Sub Iteration ()

Dim xinit As Double, x As Double

Din Err As Double, Tel As Double, new As Double

xinit = InputBox("Please enter initial guess.")

x-xinit

Tol = 0.001

Do

znow=fune (x)

Err-Aba (xnew-x)

x = xnow

Loop Ustil Er - Tol

MagBox FormatNumber(x, 3)

End Sub

Function rane (z)

fune- sqr(z (1/3) + 5* 23

End Function

Place your cursor somewhere within the Iteration sub and step through using FB. What do you notice when

you step through the "xnew=func(x)" Ine? Where does the cursorhighlight go to?

Let's make a few more (very cooll) final modifications to our Iteration code. Delete the func function and

modify iteration to be the following (okay, I admit, these are not minor but are somewhat major modifications):/nSubIteration()

Din xinit As Double, As Double

in E A Double, Tel As Double, now as Double

Dima As String, Here As String

on EXEGGUTO BOLO

- Inputic (anter a function

just as you would in Excel.VECELE(1)

InputBox("Please enter initial gas.")

xinit

x-xit

Tol-0.001

Evaluate Replace (Fm, "", 201

Ereb-x)

Loop Ostil Err-701

MB TOK TOCRatmusivΣ (Χ, 3)

"A GESr has occurred, please try again."

Place your cursor somewhere within the Iteration sub and step through using F8. When the first Input box

pops up, enter the following (note that we are using Excel syntax for the square root function, not VBA syntax):

Microsoft Excel

Enter a function 100 just as you would in Excel

(Fox)

x^(1/2)+3%)

OK

Cur

Then press OK and enter an Intial guess of 4. Continue stepping through (FB) and make sure everything is

working properly. You should get a result that is the same as what you got in the previous versions of neration.

Please consult your TA if you are having problems, or go back and follow these instructions carefully.

What does the function "Evaluate(Replace(Fxn, "x", x]" do?

You will have to use this type of equation entry in the next couple weeks so this will help you on your

homework!

At this point, it is worthwhile to explain a few lines in the code that you may be unfamiliar with. First, I have

Dimmed Fxn As String. The function that is input into the InputBox is stored as a String and used in the

Replace function (see above). Second, you may notice the statement "VbCrLf. This is essentially a

carriage return (Visual basic Carriage return Left formatted-from the old typewriter Ingol) and allows the

"(Form: x = f(x))"Ine to be on a new line. Third, you may have noticed the lines "On Error GoTo Here" and

"Here". We will now address these two statements and you will discover what they do!

Run your code again using F8 so you can step through the code. This time, enter the following into the input

DOK:/nCHEN 1310

Microsoft Excel

Interaction you wil

PINKS

Lab 805

Carca

Enter an initial guess of 4. Keep pressing F8 to cycle through iterations. Notice what is happening to the

value of x. What is happening to x?

Page 6

You will reach a point (If you press FB too quickly you will miss it!- this happens then you'll have to start

over) when the cursor/highlight skips to the Ine "MsgBox "An error has occurred, please try again.":

Exit Bub

Here:

MsgBox "An error has occurred, please try again."

End Sub

Make sure you save your file now.

What has happened here is that the solution is veraing (not converging like we'd want for iterative solving) A

point comes when x is just too large to be stored in VBA and an error occurs internally in the VBE. Based upon

the line "On Error GoTo Here", what is happening when there is an Internal error? What is the

Importance/significance of the line "Here:"7

Finally, you notice the line "Exit Sub" before the line "Here". What do you think this does and why is it needed?

(If you are unsure, temporarily delete or "comment out the line by using an apostrophe at the beginning of the

line to see what happens if Exit Sub is not present-you will want to use a function that converges.)

Submission Instructions: When you have completed this workshop, upload your completed (and saved) "Lab 5

Worksheet" and your file " Lab_08.xlsm. These should be submitted to Canvas in the

"Lab 5 module. For instructions on submitting assignments to Canvas, see the document How to Submit

Assignments to Canvas".

Leave Excel and return to Windows. Copy your workbook file to your USB drive or email to yourself. Log out.

End of Lab Workshop B

Fig: 1

Fig: 2

Fig: 3