+ Reply to Thread
Results 1 to 11 of 11

Optimization of newton-raphson iterative solution with excel solver vba

  1. #1
    Registered User
    Join Date
    10-02-2023
    Location
    Aktau
    MS-Off Ver
    365
    Posts
    7

    Optimization of newton-raphson iterative solution with excel solver vba

    Cross-posted at https://www.mrexcel.com/board/thread...ation.1246158/

    I have an excel worksheet that solves system of PDEs using newton-raphson method. The solution obviously depends on some variables that I input. There is a VBA macro (lets call it VBA1) that is used to solve the equations, after solving the equations the macro inputs the solution as values. Basically I can input variables, run macro VBA1 and it gives results as values.

    The VBA1 macro gives me modeled results, which depend on variables. I also have some real data from my experiments that I want to fit to the model. The problem can be solved by comparing these two data sets, defining SSE and minimizing it by varying input parameters. I guess it can be done by some optimization algorithm, but in excel we only have solver.

    Now the problem is that solver won’t accept the data from the model as it is pasted as values (I guess solver needs explicit solution with formulas), because there is no analytical solution, and as I mentioned earlier, I used newton-raphson iterative method for solution.

    I tried to write a macro that utilizes solver for optimization, it calls VBA1 and tries to minimize SSE, but nothing really happens. VBA1 inputs the solution as values, solver tries to change variables but nothing happens. I am not sure why it behaves like this.

    Is it possible to write a VBA utilizing solver so it can optimize variables and minimize SSE in this problem? If not possible to utilize solver then why?
    Last edited by 6StringJazzer; 10-02-2023 at 01:15 PM. Reason: added xpost link

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    You need some sort of calculation (formula) for solver to solve. Be it newton-raphson iterative method or others.

    Otherwise, there is nothing for Solver to solve

    Have a look at MrShorty's post below.
    https://www.excelforum.com/tips-and-...ind-roots.html
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    10-02-2023
    Location
    Aktau
    MS-Off Ver
    365
    Posts
    7

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    Ok that makes sense.
    Are there any other optimization techniques that can solve my problem?
    Last edited by nurlanj; 10-02-2023 at 01:13 PM.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not told us about this. You are required to do so. Cross-posts are allowed but you must provide a link to your posts on other sites.

    Please see Forum Rule #7 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important: https://excelguru.ca/a-message-to-forum-cross-posters/

    I have added the link to the OP because new are still new.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

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

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    I'm not sure how much you expect from us. As one who frequently does curve fitting/regression on PDE type equations that require a numerical method to solve, I note that I use the strategies in my thread that CK76 linked to to solve the PDE, then Solver has little difficulty with the regression part of the problem.

    You say that you already have a VBA procedure that solves the PDE and writes the result to the spreadsheet. Would you be able to restructure that procedure so it is a Function procedure and can be called as a UDF from the spreadsheet? That's one strategy I use frequently and with good success. My tutorial thread included an example of writing a simple NR algorithm as a UDF and calling it from the spreadsheet, if that will help.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  6. #6
    Registered User
    Join Date
    10-02-2023
    Location
    Aktau
    MS-Off Ver
    365
    Posts
    7

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    Unfortunately I have no idea how to convert my VBA to UDF, since it is complex. The vba itself generates 2d data, obtained from solving Jacobian matrix, and each of the rows of this 2d dataset is the result of newton-Raphson iterations. And the objective function to minimize is the sum over this 2d dataset.

    I read somewhere about solver option called StepThru, would it be able to solve my problem?
    I mean mean objective cell already contains formula (for calculation of SSE), would it be enough for solver to assume there is some calculation going on?
    Last edited by nurlanj; 10-03-2023 at 07:03 AM.

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

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    I have not heard of a StepThru option as part of Excel's Solver. A quick internet search returns no meaningful hits, so I have no idea what capabilities this option adds.

    Here's a complete list of the Excel programming strategies I could foresee using for this.

    1) Convert VBA1 to UDF to solve the PDE. Solver to perform the regression. You've already said you have no idea how to make VBA1 into a UDF, so maybe this option is off the table.
    2) Convert VBA1 to circular reference spreadsheet formulas (iteration enabled) to solve the PDE. Solver to perform the regression. I, personally, have had minimal difficulty using Jacobian matrices in circular reference type NR formulas. I'm not sure if you feel up to programming this approach, though. Do you think you could convert VBA1 to circular reference spreadsheet formulas?
    3) On occasion I have tried setting up Solver to simultaneously solve the PDE and regress the parameters, with mixed success. This can work, but it seems a bit less numerically stable than the previous two strategies.
    4) I have never really tried it, but I believe Solver executes Calculate event procedures as part of each calculate event that it executes, so it can include those results in its algorithms. It might be worth the effort to move the VBA1 code into a calculate event procedure (or call VBA1 from a calculate event procedure) and see if Solver can handle the regression while the Calculate event procedure solves the PDE.

    Which of those strategies do you feel like trying?
    Last edited by MrShorty; 10-03-2023 at 12:57 PM.

  8. #8
    Registered User
    Join Date
    10-02-2023
    Location
    Aktau
    MS-Off Ver
    365
    Posts
    7

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    Thanks for your reply MrShorty,

    1) I have no idea how to do it
    2) I don’t think circular references are an option either. At least for my spreadsheet. My Jacobian matrix, which acts as an “engine” of all calculations in my workbook, consists of 80x80 cells. In order to do circular reference in my calculations I need as many Jacobian matrices as time steps in my calculations, and I make over 100 timesteps. I could program Jacobian calculations in VBA, but again I don’t know how solver works with vba.
    3) This is also not an option, since I have over 80 equations to be solved for each time step.
    4) The only option is to try to setup the solver so that it can optimize the problem. I think I will focus on this.

    Also, now I am thinking of writing another optimization routine such as PSO in my spreadsheet.

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

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    You say that you have no idea how to write a VBA UDF for this, but you also say that you would have no trouble programming the Jacobian calculations in VBA. It seems to me that programming the Jacobian calculations is the hard part of writing a UDF. If you can write the Jacobian calculations in VBA, it seems to me that you should also be able to put it in a VBA UDF. Perhaps you are just unfamiliar with the basics of writing UDFs? I have a tutorial for that here: https://www.excelforum.com/tips-and-...uild-udfs.html

    I don't want to push you in a direction you don't want to go, but it seems to me that writing a VBA UDF to solve the PDE would be well within your skillset. Maybe I'm misunderstanding what you are saying.

    Edit to add: Here's my strategy when I need a UDF to return an array/matrix/multiple values: https://www.excelforum.com/excel-pro...ml#post4071488
    Last edited by MrShorty; 10-03-2023 at 01:49 PM.

  10. #10
    Registered User
    Join Date
    10-02-2023
    Location
    Aktau
    MS-Off Ver
    365
    Posts
    7

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    MrShorty,

    Thank for your tutorials. I will definitely try these strategies if the solver fails. You’re right I know about UDFs not much.
    I will try to program Jacobian calculation in VBA, but after your words how difficult it is, I think I could be overestimating my skills)
    I will update you on my progress.

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

    Re: Optimization of newton-raphson iterative solution with excel solver vba

    Let us know how it goes, and if there are any specific areas we can help.

+ 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. Replies: 9
    Last Post: 06-10-2020, 11:35 AM
  2. Logistic optimization using Excel solver
    By ElBeginnerDon in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-27-2020, 11:43 AM
  3. Need newton raphson method to converge to only positive numbers
    By jacktbg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-18-2017, 12:19 PM
  4. Replies: 7
    Last Post: 12-31-2016, 12:29 PM
  5. Vba code for newton raphson
    By albeezy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-20-2014, 09:02 AM
  6. Goal Seek & Newton-Raphson
    By posto5 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-07-2013, 01:49 AM
  7. request newton raphson code
    By hamidciv in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-02-2012, 03:34 PM

Tags for this Thread

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