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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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>
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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
Back
Top