+ Reply to Thread
Results 1 to 10 of 10

calling nth element in an array to be used in another code

  1. #1
    Registered User
    Join Date
    07-04-2013
    Location
    mauritius
    MS-Off Ver
    Excel 2010
    Posts
    15

    calling nth element in an array to be used in another code

    Hello,

    I need help with arrays. I'm trying to compute n-body problem in excel vba. I have succeeded in coding it. However, for nth number of bodies i have to write into my code n*4 1st order ODEs. For example, if i want 10 bodies, i would need to write 40 ODEs which is quite tedious. By the way i have already written it and it works fine. But i'm trying to make my life easier by writing a code that will go through the standard set of 4 equations several times over depending on the number of bodies. My code for calling the algorithm for gravitational computation is as follows, but it doesn't work:
    Function fu(t, x, m, dxdt)
    G = 1
    N = 12
    Dim a(), b() As Double
    ReDim a(1 To N / 4), b(1 To N / 4)


    For j = 1 To N / 4
    c = 0
    d = 0
    For i = 1 To N / 4

    If j <> i Then
    i = i
    Else
    i = i + 1
    End If


    r = (x(j) - x(4 * i - 3)) ^ 2 + (x(j + 1) - x(4 * i - 3 + 1)) ^ 2
    a(i) = ((G * m(i)) / (r ^ (3 / 2))) * (x(4 * i - 3) - x(j))
    b(i) = ((G * m(i)) / (r ^ (3 / 2))) * (x(4 * i - 3 + 1) - x(j + 1))


    c = c + a(i)
    d = d + b(i)


    Next

    dxdt(j * 4 - 3) = x((j * 4) - 1)
    dxdt(j * 4 - 2) = x(j * 4)
    dxdt(j * 4 - 1) = c
    dxdt(j * 4) = d
    Next


    fu = 0
    End Function

    Thanks

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    Can you add code tags when you post code?

    Oh, and a little explanation of ODEs and n-body problem might help
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-04-2013
    Location
    mauritius
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: calling nth element in an array to be used in another code

    Quote Originally Posted by Norie View Post
    Can you add code tags when you post code?

    Oh, and a little explanation of ODEs and n-body problem might help
    Hi
    Sorry, I'm new! I'll add code tags next time!

    ODEs are Ordinary Differential Equations. n-body problem is a problem in which we want to compute the gravitational effects each body has on the other bodies and hence calculate their orbits respectively through Newton's gravitational force equation which is F = GMm / r^2, where G is the gravitational constant, M and m are the masses of the bodies and r the distance separating their centers. Basically to plot the orbit using the force equation you will need x and y initial positions and vx and vy which are initial velocities.

    For example here are the 8 ODEs for two-body problem:

    Please Login or Register  to view this content.
    I hope this is clear enough. Thanks

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,528

    Re: calling nth element in an array to be used in another code

    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.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    07-04-2013
    Location
    mauritius
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: calling nth element in an array to be used in another code

    Quote Originally Posted by MrShorty View Post
    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:

    Please Login or Register  to view this content.
    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

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: calling nth element in an array to be used in another code

    You can't alter the limits of a For Next within the loop.

    So this won't do anything.
    Please Login or Register  to view this content.
    As for a syntax problem, unless you are getting syntax errors then I don't think there is one.

    There probably isn't a problem with the algorithim either.

    What could be the problem is how you've implememented that algorithim.

    Can you post the algorithim you are trying to implement?

  7. #7
    Registered User
    Join Date
    07-04-2013
    Location
    mauritius
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: calling nth element in an array to be used in another code

    Can i just send you the working excel sheet to get a hang of what it is doing? the excel sheet contains two functions: the fu function and the function for the numerical integrator.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: calling nth element in an array to be used in another code

    You have a sheet that works and givs you the results you want?

    If you do then you can attach it here.

    To upload an example workbook.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  9. #9
    Registered User
    Join Date
    07-04-2013
    Location
    mauritius
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: calling nth element in an array to be used in another code

    dopri 4 body.xlsm

    Hi here is my working excel sheet for a 4-body problem. THere is a chart showing you the 4 bodies in their orbits based on initial conditions set forth in sheet1. In the VBA section, you will find the fu function and a numerical integrator. Regarding the integrator, it is the DOPRI 8(7)13: Dormand Prince 8th order with embedded 7th order with 13 stages. I have written the integrator so that it can accept n variables as an array in the formula bar. see sheet1 formula for example. In this particular fu function the number of ODEs to compute are 16. So there are 16 1st order ODEs to compute.

  10. #10
    Registered User
    Join Date
    07-04-2013
    Location
    mauritius
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: calling nth element in an array to be used in another code

    I forgot to mention that the chart operates as follows: press clear button to clear all orbits. press start button and escape to run the simulation....ah yes...i forgot , there is a third function to run the plot simulation

+ 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