MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 20th, 2002, 03:12 PM   #1
jacamar21
New Member
 
Join Date: May 2002
Posts: 2
Default

Looking for a code to solve the secant method.

xnew = xold1-(f(xold1)*(xold2-xold1))
-------------------------
(f(xold2)-f(xold1))

xnew needs to replace xold1, xold1 needs to replace xold2, and xold2 is discarded. The iterations are limited by a tolerance and a maximum variable. Any help on this would be very much appreciated. thank you
jacamar21 is offline   Reply With Quote
Old May 21st, 2002, 01:07 PM   #2
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi jacamar21,

Here is a VBA user-defined function (UDF) that implements the Secant method:

Function Secant(X0 As Double, X1 As Double) As Double

' Returns the root of a function of the form F(x) = 0
' using the Secant method.

' X1 is a first guess at the value of x that solves the equation
' X0 is a "previous" value not equal to X1.
' This function assumes there is an external function named FS that
' represents the function whose root is to be solved

Dim X As Double 'the current guess for root being sought
Dim Xold As Double 'previous guess for root being sought
Dim DeltaX As Double
Dim Iter As Integer 'iteration counter
Const Tol = 0.00000001 'convergence tolerance

Xold = X0
X = X1

'permit a maximum of 100 iterations
For Iter = 1 To 100
DeltaX = (X - Xold) / (1 - FS(Xold) / FS(X))
X = X - DeltaX
If Abs(DeltaX) < Tol Then GoTo Solution
Next Iter

MsgBox "No root found", vbExclamation, "Secant result"

Solution:
Secant = X
End Function

As you can see, you must provide a function FS that is the function you desire the root of. I used the following function for my test of Secant:

Function FS(X As Double) As Double
'Example function cubic equation
FS = X^3 - X - 1
End Function

and I used Secant to solve this by entering it into a cell like this:

=Secant(1.4,1.3)

where 1.4 and 1.3 are the two "previous" (guess) values for x. I got a result of 1.324718, which is correct.
__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other life: http://damonostrander.com )
Damon Ostrander is offline   Reply With Quote
Old May 21st, 2002, 02:26 PM   #3
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-05-21 12:07, Damon Ostrander wrote:
Hi jacamar21,

Here is a VBA user-defined function (UDF) that implements the Secant method:

Function Secant(X0 As Double, X1 As Double) As Double

' Returns the root of a function of the form F(x) = 0
' using the Secant method.

' X1 is a first guess at the value of x that solves the equation
' X0 is a "previous" value not equal to X1.
' This function assumes there is an external function named FS that
' represents the function whose root is to be solved

Dim X As Double 'the current guess for root being sought
Dim Xold As Double 'previous guess for root being sought
Dim DeltaX As Double
Dim Iter As Integer 'iteration counter
Const Tol = 0.00000001 'convergence tolerance

Xold = X0
X = X1

'permit a maximum of 100 iterations
For Iter = 1 To 100
DeltaX = (X - Xold) / (1 - FS(Xold) / FS(X))
X = X - DeltaX
If Abs(DeltaX) < Tol Then GoTo Solution
Next Iter

MsgBox "No root found", vbExclamation, "Secant result"

Solution:
Secant = X
End Function

As you can see, you must provide a function FS that is the function you desire the root of. I used the following function for my test of Secant:

Function FS(X As Double) As Double
'Example function cubic equation
FS = X^3 - X - 1
End Function

and I used Secant to solve this by entering it into a cell like this:

=Secant(1.4,1.3)

where 1.4 and 1.3 are the two "previous" (guess) values for x. I got a result of 1.324718, which is correct.
Hi Damon,

Nice job on this one.

Can you suggest a way to identify when a sequence starts diverging? It usually will be identifiable immediately, and if not stopped, will go to overflow or undeflow quickly.

What I have done in the past is store the prior result (DeltaXold, for instance) and if DeltaX is greater than DeltaXold, then go back to DeltaXold and "nudge" it a bit before resuming.

Any specific guidance to offer? I have a number of posted examples where this is done, but am not satified with the results thus far.

Thanks,
Jay
Jay Petrulis is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 06:15 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes