Need help in vba

ivance

New Member
Joined
Jun 22, 2012
Messages
2
I am trying to come up with a program to impliment the secant method in Excel VBA. I have tried multiple ways of doing this. If statements, for-next, and do while loops. I believe the do while loops was the closest I got to completing it. For the program someone should be able to enter two initial conditions and their desired tolerence level. I have two main issues. The current code shown here will give an overflow error and not stop iterating the function. However; I believe that the do while condition should be bound>tolerence. I want it to iterate untile the bound is less than the tolerence and then msgbox that answer. Please help, I have only been doing excel for a week so I have alot to learn

Sub dwl()

xold = InputBox("Please input x-1 value")
x = InputBox("please input a x initial value")
tolerence = InputBox("Please input the tolerence,the difference between x initial and x-1, you wish to use")

e = 2.71828182845905

bound = Abs((x - xold))

Do While bound < tolerence
fxc = (3 * x) - (2 * x ^ 2) + (e ^ x) - 10
fxp = (3 * xold) - (2 * xold ^ 2) + (e ^ xold) - 10
nxt = x - ((fxc * (xold - x)) / (fxp - fxc))
xold = x
x = nxt
bound = Abs((x - xold))
Loop
MsgBox (nxt)
End Sub
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

delaneyjm

Well-known Member
Joined
Apr 22, 2009
Messages
624
My trigonometry skills are very rusty...but could you not do this?

Code:
Public Function MySecant(ByVal MyAngle As Double) As Double
    'MyAngle should be in radians.
    MySecant = 1 / Cos(MyAngle)
End Function</pre>
 

Watch MrExcel Video

Forum statistics

Threads
1,123,256
Messages
5,600,556
Members
414,388
Latest member
Pkmep4

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top