Matrix dimensions must agree error
显示 更早的评论
Hello, I am completing a standard ballistic missile simulation in MATLAB. The code I wrote is the following, I get the error of matrix dimensions in Line 33 but I really don't know how to solve it. The function is also defined as follows:

function [a_x] = a_x(phi,psi,m,P,q,r)
C_a = 0.5;
S_m = 9;
C_alpha_y = 0.2;
C_beta_z = -0.2;
a_x = (1./m).*((cosd(phi).*cosd(psi)).*(P-C_a.*q.*S_m) - sind(phi).*C_alpha_y.*q.*S_m.*alpha.*57.3 + cosd(phi).*sind(psi).*C_beta_z.*q.*S_m.*beta.*57.3) + (mu/r.^3).*x;
end
%Constants
C_a = 0.5;
C_alpha_y = 0.2;
C_beta_z = -0.2;
S_m = 9;
mu = 3.986005e14;
R = 6371004;
m_dot = 1000;
I_sp = 2700;
S_theta = 3.2;
p_0 = 100000;
m_0 = 200000;
%Extract alpha and psi from table
phi = T.phi;
psi = T.psi;
%Get all values for density, pressure, geocentric distance and axial thrust for all registered heights
for h = 1:91*1000
[rho(h,:),p_h(h,:),a(h,:)]=atmosphere(h);
P(h,:) = I_sp*m_dot + S_theta*(p_0 - p_h(h,:));
r(h,:) = h + R;
q(h,:) = 0.5*rho(h)*a(h);
end
%Find m
for t = 1:152
m(t,:) = m_0 - m_dot*t;
end
x = 0; y = 0; z = 0;
%Find a_x,a_y and a_z
for t = 1:152
a_x(phi,psi,m,P,q,r) = a_x(phi,psi,m,P,q,r)*t;
end
7 个评论
Simon Chan
2022-1-13
What are the sizes of variable 'phi' and 'psi'?
Or could you attach the table with variable T here?
KSSV
2022-1-13
What are you trying to do here?
a_x(phi,psi,m,P,q,r) = a_x(phi,psi,m,P,q,r)*t;
We cannot help you unless the input is given.
Adnane Ait Zidane
2022-1-13
Torsten
2022-1-13
phi, psi, m, P, q and r must be of the same size for a_x to work properly.
This is not the case as you can see from the workspace listing.
Adnane Ait Zidane
2022-1-13
Ankit
2022-1-14
you can also convert all your variables to time dependent...
Samuel Clary
2022-1-14
编辑:Samuel Clary
2022-1-14
Edit: Although this does not solve your problem, I think it may cause errors as soon as the a_x function/inputs are corrected.
I may be missing something, but I believe your syntax is incorrect in this section:
for t = 1:152
a_x(phi,psi,m,P,q,r) = a_x(phi,psi,m,P,q,r)*t;
end
a_x is seen as a function by your code. I understand that in the function itself you have written:
function [a_x] = a_x(phi,psi,m,P,q,r)
But, this does not mean that in the previous section of code the variable a_x will be created by the function a_x. The first section of code should have a different output variable. Such as:
for t = 1:152
Varx(phi,psi,m,P,q,r) = a_x(phi,psi,m,P,q,r)*t;
end
This still is not ideal though because you are esentially overwritting the same variable 152 times. I have not taken the time to look into the output size of your variable from a_x, so my quick suggestion is to throw the answers into cells.
for t = 1:152
Varx{t} = a_x(phi,psi,m,P,q,r)*t;
end
This still is not going to be efficient, and I would recommened using the cell function in Matlab to create the correct number of cells before going into that for-loop.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
