
Originally Posted by
MrShorty
I realize debugging something like this is tedious and painful. Debugging is also an essential skill (as you may already know).
1) Per forum rules, put your code inside of code tags (see forum rules link for instructions). This makes it easier for us to read your code.
2) Stating that the function "doesn't work" is not very helpful. What does "doesn't work" mean? It fails to compile? If it fails to compile, does it give an error message? If so what is the error message? Is it a runtime error? If so, what is the error message? Is it flagging specific statements for these errors? If so what statements is it indicating have the problem? Or maybe it compiles and runs without error, but the result being returned is obviously incorrect? If so, what is the function returning and what do you expect it to return? Stepping through the function is often useful at this point so you can monitor variables and, by comparing expected results at each step with what the value of each variable at each step, you can hopefully identify where in the code the calculations are going wrong.
All that said, a couple of things I note:
1) you have a block If inside of the For i..Next i loop that modifies i. I'm not 100% familiar with the ways this goes wrong, but most programmers that I see say not to modify the index variable inside of a For..Next loop. I would suggest you revisit what this part of the code is doing and see if there is a better way to structure it so that you don't modify i inside of its loop.
2) The final assignment statement is fu=0. So, after going through all of those calculations, fu is returning 0. Sometimes while developing/debugging a function, I will use a similar strategy to debug issues with the return value. On the other hand, for a calculation that is at astage where it is expected to work, I would expect the final assignment statement to have something to do with the calculations that preceeded it. Perhaps you need to think about what value you need to return and figure out how to get that value into fu.
Hi,
Well sorry my fault. I didn't give much info regarding what the code is supposed to do. This preliminary algorithm i posted at the start returned a "VALUE" error.
The if is supposed to say that j cannot equal to i based on the formula for n-body problem on page 2 equation 3:
http://www.cs.hut.fi/~ctl/NBody.pdf
The fu function is callable in another function where i'm using a numerical ODE to compute. If i had my ODEs written down in the normal way and used fu=0 at the end of the fu function, the computation will give correct results. Basically the fu function should return the dxdt values as per my code above after having ran through the numerical ODE.
My problem is, and this is where i think the code is faltering, trying to specify which dxdt element to have which calculations:
Is that correct in the excel vba syntax? I'm sure something is wrong here, or i may be wrong as well in my suspicion as i'm no VBA expert. As to get into the code to debug each part, this will amount to rewriting the whole code altogether because the fu function in itself will never return anything useful since it is used by another function to do the actual computing. Basically i'm trying to make an algorithm that go through a certain set of equation a number of times and give the results in as many dxdt as is needed as per the number of bodies. So here i think i'm faced with a syntax problem and not a function or algorithm problem. Thanks
Bookmarks