# Matlab problem.

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

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
marcusl
Gold Member
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.

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 theres 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.

J77
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.

J77
Try a "clear all"... 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!

J77
Got me stumped - and I use Matlab for most of the day 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...

Could you post the entire function that this troublesome segment is from?

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.

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';

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. 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!! -G & N :zzz:

..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 realise 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.

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

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.