Programming a Solar System Model: Overcoming Index Errors

In summary, I am trying to initialize a matrix with values from an array, however I am getting an error when I try to do it. I am not sure if the arrays are in the right order, or if I am doing something wrong. I would appreciate any help that is available.
  • #1
fasterthanjoao
731
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
  • #2
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.
 
  • #3
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.
 
  • #4
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) ];
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
Try a "clear all"... :confused:
 
  • #9
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.
 

1. What is a solar system model?

A solar system model is a simplified representation of our solar system, typically used for educational or scientific purposes. It includes the sun, planets, moons, and other objects orbiting around the sun in a scaled-down version of their actual distances and sizes.

2. Why is programming a solar system model difficult?

Programming a solar system model can be difficult due to the complexity of the solar system and the calculations involved. It also requires a good understanding of programming languages and techniques, as well as knowledge of astronomy and physics.

3. What are index errors in a solar system model?

Index errors in a solar system model are errors that occur when trying to access or manipulate data in an array or list. This can happen when the index used is out of range or when the data is not in the correct format, resulting in incorrect calculations or unexpected behavior.

4. How can index errors be overcome in a solar system model?

To overcome index errors in a solar system model, it is important to carefully check and validate all inputs and calculations. Using error handling techniques such as try-catch statements can also help to catch and handle any unexpected errors. It is also important to thoroughly test the program before using it for simulations or other purposes.

5. Are there any tools or resources available for programming a solar system model?

Yes, there are many tools and resources available for programming a solar system model. Some popular options include programming languages such as Python and Java, as well as specialized software such as Celestia and Solar System Simulator. There are also online tutorials and educational materials available to help with programming a solar system model.

Similar threads

  • Introductory Physics Homework Help
Replies
11
Views
1K
  • Sci-Fi Writing and World Building
Replies
21
Views
853
  • Programming and Computer Science
Replies
2
Views
2K
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Classical Physics
Replies
15
Views
2K
Replies
31
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • DIY Projects
Replies
3
Views
2K
Back
Top