Programming a Solar System Model: Overcoming Index Errors

Click For Summary

Discussion Overview

The discussion revolves around programming a solar system model in MATLAB, specifically addressing issues related to index errors when manipulating arrays and matrices. Participants explore the challenges of passing data structures between functions and the implications of using different array formats.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an index error occurring when trying to fill a matrix with values from an array, suggesting that the issue may stem from how the array is initialized or manipulated.
  • Another participant points out that using a semicolon in array definitions may cause MATLAB to interpret the data incorrectly, recommending the use of a comma instead.
  • Concerns are raised about whether MATLAB is interpreting the array dimensions correctly, with suggestions to check the size of the arrays involved.
  • A participant identifies that the problem may arise from the way data is passed between functions, noting that the ODE solver used requires a column vector, which conflicts with the desired array format.
  • There are suggestions to convert the column vector back to a row vector after using the ODE solver, although one participant expresses a desire to avoid this workaround.
  • Another participant humorously suggests rewriting the ODE solver to accommodate the desired array structure.
  • Later posts indicate that after resolving the initial index error, new issues arise related to tolerance settings in the ODE solver, prompting further exploration of potential causes.
  • A participant expresses a similar issue, seeking advice on how to address the problems encountered.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to handle the array formatting issues, and multiple competing views on how to resolve the index errors and ODE solver challenges remain present throughout the discussion.

Contextual Notes

Limitations include the dependency on MATLAB's requirements for array dimensions and the specific behavior of the ODE solver, which may not align with the participants' desired data structures.

Who May Find This Useful

Individuals working with MATLAB for numerical simulations, particularly those interested in modeling physical systems or solving differential equations, may find this discussion relevant.

fasterthanjoao
Messages
730
Reaction score
1
Ok, so semi-irrelevant information out of the way first - I'm writing a program that will give a basic model of the solar system - taking into account interactions from the planets on each other.

I can get my program running when containing the individual starting points of each of the planets in a massive row vector (say, X(1:12) but would rather (for simplicity, efficiency and making the next stage much easier..) have X(1,1:3) and X(2,1:3) so i can have a separate area for each planet.

Hopefully i won't have to provide much code - essentially when i transfer into arrays I'm getting the following problem when run:
"

? Index exceeds matrix dimensions.

Error in ==> forcesar at 8

DX(1,1:6) = [X(1,4:6); -X(1,1:3)*GM(1)/norm(X(1,1:3))^3];

"
forcesar being a function that adjusts the data for other planetary effects - previous to the above line I've initialised DX as a zeroes(3,6) matrix so i don't see what the problem with filling it up is? (as far as I'm aware the error is essentially telling me I'm trying to put too many values into a smaller matrix) clearly I'm doing something wrong, either that or it's just not possible to do it this way.

as always, help much appreciated.

-fasterthanjoao
 
Physics news on Phys.org
the semicolon in your array statement puts the last sub-array on the next row. Use a comma instead and you should be fine. Also Matlab requires you spell it "zeros" for some reason; make sure you are really initializing DX.
 
thanks for the reply. I made those changes to no avail, still having the same error. I don't suppose that when i have:

DX(1,1:6) = [X(1,4:6), -X(1,1:3)*GM(1)/norm(X(1,1:3))^3];

matlab may feel that I'm trying to fill the first space in DX with the 4:6 from X? (obviously not possible) then the second space with the first three values? when obviously what i want is the last three in DX row 1 to be filled with the last three from X and the first three to be filled with the adjusted first three from X.

it'll all fall in eventually. thanks.
 
fasterthanjoao said:
DX(1,1:6) = [X(1,4:6), -X(1,1:3)*GM(1)/norm(X(1,1:3))^3];
when obviously what i want is the last three in DX row 1 to be filled with the last three from X and the first three to be filled with the adjusted first three from X.

If you want X(1,4:6) to go in DX(1,4:6) and the other stuff to go in DX(1,1:3), just swap the order of the things on the right side
DX(1,1:6) = [-X(1,1:3)*GM(1)/norm(X(1,1:3))^3 , X(1,4:6) ];
 
again, thanks. unfortunately it's still coming up with this same error.

I've tried everything i can think of - if, even, i enter the values individually and perform the operation (instead of passing defining X and operting on it to form DX inside a function) manually i.e. entering;

X(1,1) = -1.756895992827094E-01;
X(1,2) = 9.659716383076408E-01;
X(1,3) = 2.050240276128469E-04;
X(1,4) = -1.722463621150023E-02;
X(1,5) = -3.020684839068507E-03;
X(1,6) = -7.003389133678563E-08;

DX(1,1:6)=[-X(1,1:3)*GM(1)/norm(X(1,1:3))^3 , X(1,4:6) ]

matlab responds sensibly (as in, exactly as expected, which is good) but when I define X in the main part of my program then pass it to a function that forms DX, i keep getting this error. After clearing fully then running (obviously the error appears), I'm finding that MATLAB happily displays any part of X without problem, so I've no problem there.

I've even tried, in the function, setting instead

DX(1,1:6)=X(1,1:6) (seems sensible)

but this doesn't work either. leading me to think there's maybe a problem passing X as an array properly; this is how i do it:

"function DX = forcesar(t,X,GM)"

t,GM obviously other things that needed passing. Either way, I don't understand why it worked fine before setting the info in a single vector as oppose to an array! much appreciated replies anyway, i suppose this is all part of the process.
 
fasterthanjoao said:
DX(1,1:6)=X(1,1:6) (seems sensible)
Have you you rows and columns the right way round.

If you do what I've quoted with a single column of X (6 rows), it won't work.

However,

DX(1:6,1)=X(1:6,1) will.

If something goes wrong, it's worth checking the size of your arrays, ie.

size(X)=
6 1

shows X has 6 rows, in a single column.
 
X is formed individually as X(1,1) then X(1,2) ... X(1,6). then passed on. DX is initialized as DX=zeros(1,6) in the function and then DX(1,1:6)=X(1,1:6) follows.

"? Index exceeds matrix dimensions" is the result. meh.
 
Try a "clear all"... :confused:
 
appreciate the suggestions J77, still in the pickle.

waiting for some resident MATLAB experts in our department to become available next week, it's just one of those annoying things that looks like its going to eat away at me until then!
 
  • #10
Got me stumped - and I use Matlab for most of the day :confused:

Going back to the original error - stick the command keyboard just before where it breaks down in your function.

When it reaches this point you'll get a debugging prompt K>>, then you can check on what X looks like at that point - you'll see if it's been read in as you want then...

type K>>return to pass through the keyboard point.
 
  • #11
Could you post the entire function that this troublesome segment is from?
 
  • #12
mm.. thanks for the help everyone, I've noticed what the problem is now- something i didn't actually mention before it seems to don't be too disheartened that you didn't figure it out.

anyway, using a debug point helped me out in that I'm finding in the function X is coming through as a column vector (even though i defined it as an array) - the reason for this being that the data in the function comes from another routine i set up - one which uses ode113 to solve.

Reading up in MATLAB help, ode113 requires a column vector for whatever reason (so ode113 is taking my nice array and changing it for me..); unfortunately, clearly, i don't want to use a column vector - is there any way round this?

obviously i could use a different ode function, but I'm not really sure which one would be appropriate. Suggested to me so far were both ode113 and ode45, so if anyone knows which function can perform the same operations (if possible) but with an array then i'd be super greatful.

I've thought about taking the column that comes from ODE and changing it back to an array, but i'd like to consider anything less tedious (or inefficient..)

thanks.
 
  • #13
The easiest solution is to just change the column array back to a row array, nothing tedious or inefficient about that.

just one short statement that's all:
Code:
X = X';
 
  • #14
the problem is i have a (10,6) array, and the ode solver changes it to a size 60 column vector; and if possible, i want to have the solver deal with my array so that i don't have to use a single column or row vector.
 
  • #15
:wink: ok your program appears to be correct... sooo how about you rewrite the ode solver?? This seems to be the most simple and effective way of resolving this crisis!

Glad to have been of help! :approve:
-G & N :zzz:
 
  • #16
..anyway, got the problem sorted through hours of fiddling around - changing the sizes and converting. The result: expectedly, another error. This time I'm the program runs as expected at pretty much every part i can see except for within the ODE solver -

when solving, i get an error at a certain time - where it breaks down and says it can't hold my defined tolerances. I realize to get this problem sorted i'd probably have to paste the entirity of my code - instead could someone maybe propose some potential problems that would cause this error? It is getting through the first few hundred cycles, i thought at first the problem might be that my tolerances weren't assigned properly, but that seems to be ok.

thanks.
 
  • #17
Solution?

Hiya, I'm having exactly the same problem, I was just wondering if you could give me some pointers about where to fiddle about.

Kind Regards

Geraint
 
  • #18
which one of those problems is it you´re having ? heh. If it´s with the ODE solver demanding a column vector, I stumbled upon ¨X=reshape(n,m)¨ which happily makes MATLAB change the array to any size (number of entries must be the same) you want. Couldn´t believe I just wasn´t aware of that.
 

Similar threads

Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K