+ Reply to Thread
Results 1 to 23 of 23

Bisectional method using recursive Lambda

  1. #1
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Bisectional method using recursive Lambda

    This idea popped up in my head and after watching some YT about recursive lambda I played around with it for a few hours.
    The attached workbook shows a sloppy prototype that I 100% think should work. But it doesn't. What am I doing wrong?

    EDIT: There is a better workbook in post #4 and a solved one in post #15.
    Attached Files Attached Files
    Last edited by Jacc; 09-07-2023 at 04:38 AM.
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2407 (Windows 11 23H2 64-bit)
    Posts
    83,321

    Re: Bisectional method using recursive Lambda

    What is it supposed to be doing? Please explain the logic. Which are the expected results?
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2405-17628.20102
    Posts
    1,416

    Re: Bisectional method using recursive Lambda

    Without going into any detail, are you sure it will work to use RECSOLVE within the LAMBDA that is RECSOLVE. As the RECSOLVE is being created, it won't be able to calculate within itself.

    RECSOLVE Lambda:
    =LAMBDA(min,max,X,goal,IF(ABS(X*3-goal)<1,X,RECSOLVE(IF(X*3-goal>0,min,X),IF(X*3-goal<0,max,X), IF(X*3-goal>0,(min+X)/2,(X+max)/2))))

    For example, you get a result if it is created as below:
    =LAMBDA(min,max,X,goal,IF(ABS(X*3-goal)<1,X,IF(X*3-goal>0,min,IF(X*3-goal<0,max,IF(X*3-goal>0,(min+X)/2,(X+max)/2)))))
    Last edited by CheeseSandwich; 09-06-2023 at 08:02 AM.
    If things don't change they stay the same

  4. #4
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    Quote Originally Posted by AliGW View Post
    What is it supposed to be doing? Please explain the logic. Which are the expected results?
    Ok, that was a very sloppy workbook.
    I cleaned it up and added some text here.
    Attached Files Attached Files
    Last edited by Jacc; 09-06-2023 at 08:11 AM.

  5. #5
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    Quote Originally Posted by CheeseSandwich View Post
    Without going into any detail, are you sure it will work to use RECSOLVE within the LAMBDA that is RECSOLVE. As the RECSOLVE is being created, it won't be able to calculate within itself.
    Some people on internet claim it will work:
    Microsoft MVP Leila Gharani shows an example here:https://www.youtube.com/watch?v=L7s6Dni1dG8

    Mynda Treacy from MyOnlineTrainingHub shows an example here: https://www.youtube.com/watch?v=XAGdSKMV4wA

    Article from Ablebits.com that talks about recursive Lambda: https://www.ablebits.com/office-addi...unction-excel/

    Article about Lambda making Excel Turing complete and recursive: https://www.infoq.com/articles/excel...ring-complete/
    Last edited by AliGW; 09-06-2023 at 08:29 AM. Reason: Please do NOT quote unnecessarily!

  6. #6
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2405-17628.20102
    Posts
    1,416

    Re: Bisectional method using recursive Lambda

    To be honest recursive Lambda is something that is fairly new to me, it just didn't look right. I may well be wrong.

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2407 (Windows 11 23H2 64-bit)
    Posts
    83,321

    Re: Bisectional method using recursive Lambda

    I agree with Cheese Sandwich.

    Additionally you have multiple criteria that are the same:

    =LAMBDA(min,max,X,goal,IF(ABS(X*3-goal)<1,X,IF(X*3-goal>0,min,IF(X*3-goal<0,max,IF(X*3-goal>0,(min+X)/2,(X+max)/2)))))

    abd Excel will only resond to the first.

    There are other anomalies.

    What RESULT (value) would you expect the formula to return?

  8. #8
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    Quote Originally Posted by AliGW View Post
    What RESULT (value) would you expect the formula to return?
    The equation is 3*x=555 so result I expect is 555/3 which is 185.


    Quote Originally Posted by AliGW View Post

    Additionally you have multiple criteria that are the same:

    =LAMBDA(min,max,X,goal,IF(ABS(X*3-goal)<1,X,IF(X*3-goal>0,min,IF(X*3-goal<0,max,IF(X*3-goal>0,(min+X)/2,(X+max)/2)))))

    abd Excel will only resond to the first.
    Those are formulas for the different parameters for the next recursive step.
    It will for sure evaluate all of them regardless if they are similar or not.
    Last edited by Jacc; 09-07-2023 at 04:33 AM.

  9. #9
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,660

    Re: Bisectional method using recursive Lambda

    From the Help: If you call a LAMBDA function from within itself and the call is circular, Excel can return a #NUM! error if there are too many recursive calls.

  10. #10
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    Quote Originally Posted by zbor View Post
    From the Help: If you call a LAMBDA function from within itself and the call is circular, Excel can return a #NUM! error if there are too many recursive calls.
    Yeah, that is why I'm doing these dumb formula tests on the first sheets. As expected with a bisectional formula with an error in the range of 0.5%, it narrows it down in like 10 steps or something.

    The recursive lambda should be able to handle a lot more than that according to these articles:
    https://techcommunity.microsoft.com/...ns/m-p/2056256
    Here it mentions 1024 calls: https://www.sumproduct.com/news/arti...s-all-about-me

    This could ofcourse mean that my stop criteria is not working the way I see it in my head and the whole thing loops out of control and throws the #NUM!.
    Is my stop criteria wrong?
    Last edited by Jacc; 09-06-2023 at 09:02 AM.

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2407 (Windows 11 23H2 64-bit)
    Posts
    83,321

    Re: Bisectional method using recursive Lambda

    Those are formulas for the different parameters for the next recursive step.
    But they aren't parameters.

    I don't understand the mechanics of what you are trying to do well enough, but having dabbled with LAMBDA in named ranges myself, I know that the formulation you are using is not right, and that's why you get a #VALUE! error when the LAMBDA is tested correctly on the worksheet:

    =LAMBDA(min,max,X,goal,IF(ABS(X*3-goal)<1,X,RECSOLVE(IF(X*3-goal>0,min,X),IF(X*3-goal<0,max,X), IF(X*3-goal>0,(min+X)/2,(X+max)/2)))),(F15,G15,H15,I15)

    I don't have time to watch all the videos - sorry.

  12. #12
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    Quote Originally Posted by AliGW View Post
    But they aren't parameters.
    Yes they are. Are you familiar with the concept of recursive functions in general?

    Quote Originally Posted by AliGW View Post
    I don't understand the mechanics of what you are trying to do well enough, but having dabbled with LAMBDA in named ranges myself, I know that the formulation you are using is not right, and that's why you get a #VALUE! error when the LAMBDA is tested correctly on the worksheet:

    =LAMBDA(min,max,X,goal,IF(ABS(X*3-goal)<1,X,RECSOLVE(IF(X*3-goal>0,min,X),IF(X*3-goal<0,max,X), IF(X*3-goal>0,(min+X)/2,(X+max)/2)))),(F15,G15,H15,I15)

    I don't have time to watch all the videos - sorry.
    You cannot use that method of applying the parameters in the end on a recursive lambda function.
    Mynda mentions this in her video.

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,354

    Re: Bisectional method using recursive Lambda

    My work copy of 365 doesn't have LAMBDA yet, but it looks to me like you are just missing the final argument in your recursive call:

    =LAMBDA(min,max,X,goal,IF(ABS(X*3-goal)<1,X,RECSOLVE(IF(X*3-goal>0,min,X),IF(X*3-goal<0,max,X), IF(X*3-goal>0,(min+X)/2,(X+max)/2),goal)))
    Everyone who confuses correlation and causation ends up dead.

  14. #14
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    HA! It was that simple! You have good eyes Rory. I'm smashing that rep button like Ivan Lendl!
    Last edited by Jacc; 09-06-2023 at 11:28 AM.

  15. #15
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    Attached is the updated and working workbook.

    Lambda now looks like this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    and is implemented like this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files

  16. #16
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2407 (Windows 11 23H2 64-bit)
    Posts
    83,321

    Re: Bisectional method using recursive Lambda

    Are you familiar with the concept of recursive functions in general?
    Yes, and I shall turn a blind eye to the negative mplication of this question.

    I am glad you got it sorted. Well done.
    Last edited by AliGW; 09-06-2023 at 10:29 AM.

  17. #17
    Forum Expert CheeseSandwich's Avatar
    Join Date
    12-22-2021
    Location
    Kent, England
    MS-Off Ver
    365 - 2405-17628.20102
    Posts
    1,416

    Re: Bisectional method using recursive Lambda

    Looks like i need to learn about recursive Lambda!

    I too am glad you got there in the end, it's the best way to learn IMO.

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2407 (Windows 11 23H2 64-bit)
    Posts
    83,321

    Re: Bisectional method using recursive Lambda

    By the way, you need to mark the thread as SOLVED, please.

  19. #19
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,465

    Re: Bisectional method using recursive Lambda

    Please try
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    EDIT: Oh I see you found this 23 minutes ago also.
    I didn't see your post, beforeI added my solution, because it took me 40 minutes to find the issue.
    Attached Files Attached Files
    Last edited by HansDouwe; 09-06-2023 at 10:42 AM.

  20. #20
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    Yeah Hans I know, my stop condition is silly but that's on purpose. This workbook is just a proof of concept.
    I have a workbook at work in which I run through a table with a VBA bisection solver and I played around a lot with stop conditions there. There are multiple stop condition and I ended up putting them all together in one rather elegant formula with a general error percentage as input.
    As for the red larger than sign, isn't that the same as I have in my formula?

  21. #21
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,465

    Re: Bisectional method using recursive Lambda

    O yes the red larger sign was correct in your file also.
    Thanks for the nice question, now I learned a lot about recursive formulas in Excel.

  22. #22
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    Thanks for the positive feedback here people! It took me about a full day of only tinkering with this before I figured out the structure and got this far. Rory helped me stumble across the finishline.
    I'm really happy about this, it feels like I found myself a new toy to play with.
    Last edited by Jacc; 09-06-2023 at 11:19 AM.

  23. #23
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,844

    Re: Bisectional method using recursive Lambda

    I couldn't quite let it go, had to tidy it up a bit.:)
    This version has a separate lambda for the function you want to solve which is of course how it should be.
    And the solving accuracy is now a fraction of the goal and is an external parameter but this one could be either way I guess.

    My final question is: Is there a use for this recursive lambda function solver?
    Attached Files Attached Files
    Last edited by Jacc; 09-07-2023 at 05:21 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Maximum Drawdown and LAMBDA
    By andrewc in forum Excel General
    Replies: 5
    Last Post: 06-28-2023, 09:25 AM
  2. Spilling a BYCOL/LAMBDA function across rows
    By Emlyn Flint in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 06-23-2023, 09:39 AM
  3. [SOLVED] Replace Repetitive Formulas With Lambda?
    By helmanfrow in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 03-26-2023, 06:57 AM
  4. [SOLVED] BYROW(...LAMBDA(...) ) result in #CALC! error
    By dluhut in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-18-2022, 07:17 AM
  5. ARCANE: LAMBDA function calling XLM functions
    By hrlngrv in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-19-2022, 07:09 PM
  6. [SOLVED] LAMBDA iteration
    By Slabu in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 07-20-2021, 08:39 PM
  7. Lambda Expressions in Excel VBA
    By Gregor y in forum Tips and Tutorials
    Replies: 4
    Last Post: 10-17-2014, 06:59 PM

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