0

I have the following function to be solved by ODE45 solver of Matlab:

function f = odefun(t, y)
global mu ft
% y = [a, h, k, p, q, L, lla, lh, lk, lp, lq, lL];

a = y(1);
h = y(2);
k = y(3);
p = y(4);
q = y(5);
L = y(6);
lla = y(7);
lh = y(8);
lk = y(9);
lp = y(10);
lq = y(11);
lL = y(12);

n = sqrt(mu./a.^3);  % mean motion
G = sqrt(1-h.^2-k.^2);
K = 1+p.^2+q.^2;
sL = sin(L);
cL = cos(L);
r = (a.*G.^2)./(1+h.*sL+k.*cL);
% components of B Matrix
B11 = 2.*(n.^-1).*(G.^-1).*(k.*sL-h.*cL);
B12 = 2.*(n.^-1).*a.*(r.^-1).*G;
B13 = 0;

B21 = -(n.^-1).*(a.^-1).*G.*cL;
B22 = (n.^-1).*(a^-2).*r.*(G.^-1).*(h+sL)+(n.^-1).*(a.^-1).*G.*sL;
B23 = -(n.^-1).*(a.^-2).*r.*(G.^-1).*k.*(p.*cL-q.*sL);

B31 = (n.^-1).*(a.^-1).*G.*sL;
B32 = (n.^-1).*(a.^-2).*r.*(G.^-1).*(k+cL)+(n.^-1).*(a.^-1).*G.*cL;
B33 = (n.^-1).*(a.^-2).*r.*(G.^-1).*h.*(p.*cL-q.*sL);

B41 = 0;
B42 = 0;
B43 = 0.5.*(n.^-1).*(a.^-2).*r.*(G.^-1).*K.*sL;

B51 = 0;
B52 = 0;
B53 = 0.5.*(n.^-1).*(a.^-2).*r.*(G.^-1).*K.*cL;

B61 = 0;
B62 = 0;
B63 = n.*(a.^2).*(r.^-2).*G+(n.^-1).*(a.^-2).*r.*(G.^-1).*(q.*sL-p.*cL);

B = [B11 B12 B13;
     B21 B22 B23;
     B31 B32 B33;
     B41 B42 B43;
     B51 B52 B53;
     B61 B62 B63];
% costate vector
lambdaVec = [lla lh lk lp lq lL];
% control law vector
uvec = lambdaVec * B;
unorm = norm(uvec);
u = uvec./unorm;
u = u';
z_ = (B*u).*ft;
C = [0; 0; 0; 0; 0; n.*(a.^2)*(r.^-2).*G];
zdot = z_+C;

which should solve a system of 12 ODEs with 12 initial conditions, however when i run the code i get the following error:

Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Error in odefun (line 49)
B = [B11 B12 B13;

which refers to matrix B. what possibly goes wrong here?

B11, B12, ... are the components of the matrix which are defined based on function variables e.g. "y" vector.

Esi
  • 13
  • 6
  • 1
    maybe I'm missing it but where does B11 to B63 come from in the code? I don't see where they are defined. Also you have 12 variables but the B matrix has 18. – Hojo.Timberwolf Sep 19 '18 at 10:09
  • I didn't add the components of B matrix to the post cause the post would be so long! B11, B12, ... so on are the defined based on % a, h, k, p, q, L, lla, lh, lk, lp, lq, lL. for example: n = sqrt(mu/a^3); % mean motion G = sqrt(1-h^2-k^2); K = 1+p^2+q^2; sL = sin(L); cL = cos(L); r = (a*G^2)/(1+h*sL+k*cL); % components of B Matrix B11 = 2*(n^-1)*(G^-1)*(k*sL-h*cL); B12 = 2*(n^-1)*a*(r^-1)*G; B13 = 0; – Esi Sep 19 '18 at 10:11
  • yeah but that's the only part that matters. "Error using vertcat" tell you that the line of your matrix B don't always have the same dimension. So of course an error will occur. – obchardon Sep 19 '18 at 10:16
  • i updated the code – Esi Sep 19 '18 at 10:16
  • 1
    check that all your Bxx array have the same dimension. – obchardon Sep 19 '18 at 10:19
  • the error is still persistent ! – Esi Sep 19 '18 at 10:44
  • 1
    You should click on the dash of line 49 in your editor window and then run your code (it looks like `49 - B = [B11 B12 B13`, click on the -). If you run your code now, it will halt when it reaches line 49 and all the variables will appear in your workspace. Most likely, your B11, B12 and B13 will not have the same number of rows, which is why it won't let you concatenate them. – Floris Sep 19 '18 at 11:26
  • I would try and find the exact error for you, but I would have to know the sizes of `mu` and `ft` (I assume they are not scalars). You are always using element-wise operators even where not necessary, so that makes it hard for me to infer this information. – Floris Sep 19 '18 at 11:29
  • Assuming mu and ft are scalars (=simple numbers, not a matrix/vector), the code doesn't throw errors. I am nearly certain that because you made those variables global, something in any other code is setting them to a vector/matrix. Lesson = don't ever make stuff global if you can avoid it. Make a function `mu` that gives you the constant. 2nd thing = you use `.` excessively, making the code unreadable. Only use it when you really want element-wise multiplication of matrices/vectors. – Zizy Archer Sep 19 '18 at 11:42
  • @FlorisSA **ft** and **mu** are scalars – Esi Sep 19 '18 at 13:23
  • 1
    Is it possible for you to show how you call this function? Otherwise we will be taking shots in the dark. Is `y` a vector, or is it perhaps a cell array (such that `y(1)` can be a vector)? Either way, I still think the best way to solve this issue is to run the code in debug mode and evaluate the workspace (see my first comment) – Floris Sep 19 '18 at 14:14

0 Answers0