Hey, I've been having an issue. I need to set up a Newton Raphson method for approximating an answer to a complicated equation, line after line, for many conditions.
So for those who just want to skim this, my problem is that I need to set up a for loop inside of a sub that calls out functions inside said for loop, but I need the functions to have access to and change with the same changing variable set by the for loop.
Here's some context for the problem: I need to solve one cell based off of another similar to the way goalseek or solver would function, and loop down like 50 rows, referencing a table of values. The purpose of setting up this function using the Newton Raphson method has been that I am solving a complex math problem that has weird answers and I need to arrive at a real solution, and I need to do so more quickly than the solver or goalseek functions have been able to do for me. Which works! The newton raphson method is WAY faster for one line in my case, so that's not the issue.
So here's the part I need help with: I have one line/row of my spreadsheet completely solved and it solves the equation I need lightning fast. To do so, I have 2 functions set up.
My first function, f(x), is the equation I have simplified my problem to, and the second function, df(x), is its derivative. I use these to perform a taylor-series like iterative solution using a 3rd function, in which I call out those first two functions. Another subroutine calls for that 3rd function, and then outputs the value that the 3rd function spits out into a cell. All of this works phenomenally, for one line. My original plan was to solve for one line, and then put a big for loop around the whole procedure, referencing the changing variable in order to change the cells that the functions reference, and solve for many solutions in a vertical column that lines up with the rest of my data. Please let me know if that's not clear enough, I'll be happy to elaborate.
Setting up a for loop inside of each cell obviously won't work, and the variable I loop through, "j", can't be referenced within the other functions if I call it out within the sub, where it needs to be. I want to call out, for example,
b = Cells(j, "CJ") within the function that the sub calls for. I want to have the sub be a big for loop that changes j... and reference j within the functions as it changes.
I HAVE GOT AROUND my problem by making a for loop that inside my sub fills up a column in the spreadsheet with 1's, and in my functions I set j equal to a command that counts the number of cells filled in the column range, but since it has to output cells and count rows it takes too long for the size of data I need it for (like 50 lines.) Is there ANY way to reference a variable, WITHIN A CHANGING FOR LOOP, inside of a function that you call out? Will global variables work? (I've tried global variables but can't set one up that changes where I need it to.) PLEASE help me if you can, I'm going insane trying to find an answer, all I've found on most threads is basic for loop help and referencing things within subs, but nothing that quite helps with my super specific problem. Any help would be appreciated, this is my first time posting in a thread so any thread advice would also be appreciated. Thanks in advance!
I've provided a workbook set up the same way as my code. Thanks again, please let me know if I can clarify further!
Bookmarks