Error : Matrix dimensions must agree

1 view (last 30 days)
Ahmad Sheikh
Ahmad Sheikh on 29 Sep 2015
Answered: Walter Roberson on 29 Sep 2015
I dont know why it's giving can someone help ?
function derivative_x = AIAA(t,x)
derivative_x=zeros(5,1);
A1 =[ -5.9690 -41.3275 1.3080 -1.0463 -0.4869
-0.0078 -2.1282 0.9507 -20.8661 -0.0053
2.0858 49.5871 -10.9028 1.2224 -0.1164
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A2 =[ -5.9690 -41.3275 1.3080 -1.0463 -0.4869
-0.0078 -2.1282 0.9507 -20.8661 -0.0053
2.0858 49.5871 -10.9028 1.2224 -0.1164
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A3 =[ -0.2132 -41.1168 0.9679 -1.0683 0.0097
0.0051 -2.1951 1.0124 -20.8428 -0.0003
0.4030 45.4138 -6.9930 2.6577 -0.0198
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A4 =[ -0.2142 -41.1032 1.0609 -1.0410 0.0365
-0.0040 -2.1002 0.9922 -20.9089 0.0013
-0.1534 51.2034 -8.2651 -1.3891 0.0643
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A5 =[ 46.4754 -41.1787 -45.6425 -1.9833 -1.3092
-0.2261 -2.1446 1.3133 -20.8828 -0.0024
-33.7711 48.5222 29.6778 0.6092 0.4142
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A6 =[ -6.6983 -41.3450 1.3548 -1.0467 -0.5485
-0.0106 -2.1288 0.9347 -20.8662 -0.0060
2.2284 49.5581 -13.6275 1.2201 -0.1330
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A7 =[ -0.2534 -41.1302 0.9724 -1.0674 0.0073
0.0071 -2.1944 1.0233 -20.8428 -0.0001
0.5412 45.4620 -8.0555 2.6541 -0.0108
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A8 =[ -6.7972 -37.7358 -4.5678 -8.3716 13.8487
-0.3353 -1.9333 0.6742 -21.2896 0.7127
-17.5613 59.9487 -26.9974 -21.4951 37.5793
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A9 =[ 54.4509 -25.8776 76.5925 -9.7710 16.3861
0.0523 -2.1433 1.0735 -20.8769 -0.0662
-20.1909 -63.2368 -35.7359 4.3123 -11.0777
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A10 =[ 53.5818 -27.3231 75.3401 -9.8364 16.6126
-0.3772 -2.1922 0.4696 -20.8736 0.2516
-46.0467 -65.6067 -72.0813 4.5409 8.2318
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A11 =[ 2.2818 -32.6618 3.1819 -9.5013 1.4445
-0.0878 -2.5814 0.9246 -20.4504 -0.0835
-6.3412 -87.0851 -13.3051 30.2435 -5.7179
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A12 =[ 2.5007 -29.9855 3.4502 -11.1737 1.6224
0.0743 -1.6826 1.0627 -21.2935 0.0767
3.4620 -33.3368 -4.9838 -20.5278 3.9878
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A13 =[ 67.0151 -24.5698 94.2833 -9.6220 19.9493
0.0621 -2.1426 1.0873 -20.8758 -0.0798
-24.9889 -63.7592 -44.2180 4.3141 -13.4387
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A14 =[ 65.9082 -26.2945 92.6619 -9.7749 20.3256
-0.4541 -2.1999 0.3613 -20.8809 0.3027
-56.0385 -66.5172 -87.8631 4.0703 9.7567
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A15 =[ 2.9873 -32.6748 4.1806 -9.4956 1.6515
-0.1195 -2.5805 0.8903 -20.4506 -0.0945
-8.5833 -87.0188 -17.5545 30.2301 -6.4803
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
A16 =[ 3.3293 -29.7764 4.6105 -11.1986 1.8517
0.1045 -1.6745 1.0962 -21.2946 0.0867
4.9538 -32.9345 -5.1669 -20.5833 4.4969
0 0 1.0000 0 0
0 -2.6000 0 2.6000 0];
derivative_x = A1.*x + A2.*x + A3.*x + A4.*x + A5.*x + A6.*x + A7.*x + A8.*x +A9.*x + A10.*x + A11.*x + A12.*x + A13.*x + A14.*x + A15.*x + A16.*x;
while running the above funtion in another matlab file i get this error
clc;
clf;
Initial_Time=0;
Final_Time=10;
[x1,x2] = meshgrid(-2:0.1:2);
[t,x] = ode45(@AIAA, [Initial_Time Final_Time], [0.5 1 0.5 1]);
figure(1);
%subplot(3,1,1);
plot(t,x(:,1));
%axis([0 10 -2 2]);
xlabel('time')
ylabel('state x1k')
figure(2);
%subplot(3,1,2);
plot(t,x(:,2));
xlabel('time')
ylabel('state x2k')
%axis([0 10 -3 3]);

Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2015
In order to be able to use .* between a 5 by 5 matrix and "x", such as A1 .* x, then x would have to be scalar or x would have to be a 5 x 5 matrix itself. As this is ode45, the size of the output has to be the same size as your "x" input, so if x was in fact a scalar then you would get an error because the 5 x 5 result of your calculation would not be the same size as x. Therefore your x would have to be a 5 x 5 matrix.
However, in ode45, the x input is taken as a column vector the same length as your x0. Your x0 is [0.5 1 0.5 1] which is length 4, so the parameter that your function will receive will be a 4 x 1 vector. That cannot possibly be .* with a 5 x 5 array.
If you were to pass in an x0 which was length 5 then you might possibly intend your code to be
derivative_x = A1*x + A2*x + A3*x + A4*x + A5*x + A6*x + A7*x + A8*x +A9*x + A10*x + A11*x + A12*x + A13*x + A14*x + A15*x + A16*x;
which is to say that you would use Matrix multiplication instead of element-by-element multiplication. A 5 x 5 array matrix-multiply a 5 x 1 vector would give a 5 x 1 result, which is exactly what you need.
Note that if you do mean matrix multiplication like that, then by the rules of matrix multiplication, A1*x + A2*x = (A2+A2)*x and so you might as well calculate the sum of your A1 to A16 and matrix-multiply the result by x.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!