+ Reply to Thread
Results 1 to 9 of 9

Goal Seek & Newton-Raphson

  1. #1
    Registered User
    Join Date
    04-05-2013
    Location
    Around
    MS-Off Ver
    Excel 2007
    Posts
    4

    Goal Seek & Newton-Raphson

    Hi Guys,

    I am newbie to Excell/VBA programing and I am in need of help.

    Is there a way to make the Goal Seek function automatic, so a small change in one cell starts the calculation? If this is not possible, can someone help me the code below, an implemention of the Newton Raphson method, because I dont know how to program. I really appreacite your help.

    Function PriceChange(MA1, MA2, guess)
    Dim epsilon As Double, dVol As Double, P_1 As Double
    Dim i As Integer, maxIter As Integer, Value_1 As Double, P_2 As Double
    Dim Value_2 As Double, dx As Double

    dP = 0.00001
    epsilon = 0.00001
    maxIter = 100
    P_1 = guess
    i = 1
    Do
    Value_1 = MA1
    P_2 = P_1 - dP
    Value_2 = MA2
    dx = (Value_2 - Value_1) / dP
    If Abs(dx) < epsilon Or i = maxIter Then Exit Do
    P_1 = P_1 - (MA1 - Value_1) / dx
    i = i + 1
    Loop
    PriceChange = P_1
    End Function

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Goal Seek & Newton-Raphson

    For automating "Goal Seek" check out the "Worksheet_Change" sub i.e. something like

    Please Login or Register  to view this content.
    Then there is also the Excel add-in Solver that use the Newton Raphson metode (default setting) to solve linear and and non linera problem.

    Alf

  3. #3
    Registered User
    Join Date
    04-05-2013
    Location
    Around
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Goal Seek & Newton-Raphson

    Thanks, Alf. I have 400 cells that need to be calculate using Goal Seek, and the valeus for each of them are different. Will the same solutions work?

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Goal Seek & Newton-Raphson

    Please Login or Register  to view this content.
    That depends on the layout of your data. The macro above will fire if there is a change in any value in range B2 to B9 but the cell to change and the "Goal" will remain the same.

    Perhaps this link would be more usefull?

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=433

    Alf

  5. #5
    Registered User
    Join Date
    04-05-2013
    Location
    Around
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Goal Seek & Newton-Raphson

    Alf, thanks again.

    The problem is the following:

    A1: 10-X B1: 1 C1: -1. D:1 X. I want to change X until B1 = C1
    .
    .
    .
    A400

    So, it is a Goal Seek for each line, and has to take into consideration only 4 cells per line.

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Goal Seek & Newton-Raphson

    Perhaps like this then?

    Please Login or Register  to view this content.

    If I understood you problem description right. If this doesn’t work I suggest you upload a file describing the problem in more detail.

    Alf

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,827

    Re: Goal Seek & Newton-Raphson

    If you are still having trouble with this one, I might go back to the original program your own function for this.

    Newton's method is an algorithm for finding the roots or zeros of a function. Once we have a function we want to find the roots for, we give an initial guess for the desired root, then iterate using this basic equation:

    x(n+1)=x(n)-f(x(n))/f'(x(n))

    I cannot tell from your code what f(x) looks like in this case. I see what looks like an attempt to take a numerical derivative, but it doesn't look quite right. When I program a simple NR algorithm, the loop looks something like:
    Please Login or Register  to view this content.
    Assuming f is relatively simple, I prefer this to trying to code GoalSeek into event code, but that could just be that I am more comfortable with coding NR algorithms for simple f's than trying to figure out how to access GoalSeek/Solver from VBA.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  8. #8
    Registered User
    Join Date
    04-05-2013
    Location
    Around
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Goal Seek & Newton-Raphson

    I am uploading a spreadhseet with an example. I hope it makes the problem more clear. The values continue from row 30 to row 400, and ideally it should be automatic, meaning any change automatically finds the value on column D.
    Attached Files Attached Files

  9. #9
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Goal Seek & Newton-Raphson

    This macro will loop through rows 30 to 400 changing D value so BH value is equal to 1.

    Please Login or Register  to view this content.
    What I'm not sure about is
    and ideally it should be automatic, meaning any change automatically finds the value on column D
    You run the above macro and this changes all D values from D30 to D400 to "meet" specification i.e. BH30 to BH400 = 1.

    Do you wish the macro to "fire" if there is a changed value in the spreadsheet? If so add the "workings" of this macro i.e.
    Please Login or Register  to view this content.
    in the "Private Sub Workssheet_Change" macro (post #2) and adjust the "sensitive" area to suit your needs.

    Alf
    Last edited by Alf; 04-07-2013 at 01:52 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1