MATLAB: Matrix dimension must agree

matrix dimension

i want to calculate values for the vector v, where v=a+b, at time t=0s to t=40s increasing at 0.4 second intervals. b=[0,0,10-(t/4).^2] a=[20.*sin(t),20.*cos(t),0]. but when i populate these values and try and get values for v it keeps saying matrix dimensions must agree.
>> t=[0:0.4:40]
t =
Columns 1 through 12
0 0.4000 0.8000 1.2000 1.6000 2.0000 2.4000 2.8000 3.2000 3.6000 4.0000 4.4000
Columns 13 through 24
4.8000 5.2000 5.6000 6.0000 6.4000 6.8000 7.2000 7.6000 8.0000 8.4000 8.8000 9.2000
Columns 25 through 36
9.6000 10.0000 10.4000 10.8000 11.2000 11.6000 12.0000 12.4000 12.8000 13.2000 13.6000 14.0000
Columns 37 through 48
14.4000 14.8000 15.2000 15.6000 16.0000 16.4000 16.8000 17.2000 17.6000 18.0000 18.4000 18.8000
Columns 49 through 60
19.2000 19.6000 20.0000 20.4000 20.8000 21.2000 21.6000 22.0000 22.4000 22.8000 23.2000 23.6000
Columns 61 through 72
24.0000 24.4000 24.8000 25.2000 25.6000 26.0000 26.4000 26.8000 27.2000 27.6000 28.0000 28.4000
Columns 73 through 84
28.8000 29.2000 29.6000 30.0000 30.4000 30.8000 31.2000 31.6000 32.0000 32.4000 32.8000 33.2000
Columns 85 through 96
33.6000 34.0000 34.4000 34.8000 35.2000 35.6000 36.0000 36.4000 36.8000 37.2000 37.6000 38.0000
Columns 97 through 101
38.4000 38.8000 39.2000 39.6000 40.0000
>> b=[0,0,10-(t/4).^2]
b =
Columns 1 through 12
0 0 10.0000 9.9900 9.9600 9.9100 9.8400 9.7500 9.6400 9.5100 9.3600 9.1900
Columns 13 through 24
9.0000 8.7900 8.5600 8.3100 8.0400 7.7500 7.4400 7.1100 6.7600 6.3900 6.0000 5.5900
Columns 25 through 36
5.1600 4.7100 4.2400 3.7500 3.2400 2.7100 2.1600 1.5900 1.0000 0.3900 -0.2400 -0.8900
Columns 37 through 48
-1.5600 -2.2500 -2.9600 -3.6900 -4.4400 -5.2100 -6.0000 -6.8100 -7.6400 -8.4900 -9.3600 -10.2500
Columns 49 through 60
-11.1600 -12.0900 -13.0400 -14.0100 -15.0000 -16.0100 -17.0400 -18.0900 -19.1600 -20.2500 -21.3600 -22.4900
Columns 61 through 72
-23.6400 -24.8100 -26.0000 -27.2100 -28.4400 -29.6900 -30.9600 -32.2500 -33.5600 -34.8900 -36.2400 -37.6100
Columns 73 through 84
-39.0000 -40.4100 -41.8400 -43.2900 -44.7600 -46.2500 -47.7600 -49.2900 -50.8400 -52.4100 -54.0000 -55.6100
Columns 85 through 96
-57.2400 -58.8900 -60.5600 -62.2500 -63.9600 -65.6900 -67.4400 -69.2100 -71.0000 -72.8100 -74.6400 -76.4900
Columns 97 through 103
-78.3600 -80.2500 -82.1600 -84.0900 -86.0400 -88.0100 -90.0000
>> a=[20.*sin(t),20.*cos(t),0]
a =
Columns 1 through 12
0 7.7884 14.3471 18.6408 19.9915 18.1859 13.5093 6.6998 -1.1675 -8.8504 -15.1360 -19.0320
Columns 13 through 24
-19.9233 -17.6691 -12.6253 -5.5883 2.3310 9.8823 15.8734 19.3584 19.7872 17.0920 11.6983 4.4578
Columns 25 through 36
-3.4865 -10.8804 -16.5565 -19.6187 -19.5836 -16.4566 -10.7315 -3.3121 4.6302 11.8415 17.1832 19.8121
Columns 37 through 48
19.3132 15.7650 9.7280 2.1551 -5.7581 -12.7621 -17.7513 -19.9380 -18.9769 -15.0197 -8.6913 -0.9907
Columns 49 through 60
6.8663 13.6393 18.2589 19.9959 18.5759 14.2232 7.6250 -0.1770 -7.9511 -14.4699 -18.7042 -19.9855
Columns 61 through 72
-18.1116 -13.3782 -6.5327 1.3442 9.0088 15.2512 19.0857 19.9070 17.5855 12.4875 5.4181 -2.5067
Columns 73 through 84
-10.0358 -15.9804 -19.4021 -19.7606 -16.9994 -11.5543 -4.2851 3.6607 11.0285 16.6552 19.6524 19.5469
Columns 85 through 96
16.3553 10.5817 3.1374 -4.8022 -11.9837 -17.2731 -19.8356 -19.2664 -15.6555 -9.5729 -1.9790 5.9274
Columns 97 through 108
12.8979 17.8322 19.9511 18.9203 14.9023 20.0000 18.4212 13.9341 7.2472 -0.5840 -8.3229 -14.7479
Columns 109 through 120
-18.8444 -19.9659 -17.9352 -13.0729 -6.1467 1.7500 9.3703 15.5113 19.2034 19.8637 17.3879 12.1670
Columns 121 through 132
5.0252 -2.9100 -10.3858 -16.2219 -19.4969 -19.6938 -16.7814 -11.2197 -3.8866 4.0601 11.3658 16.8771
Columns 133 through 144
19.7238 19.4567 16.1177 10.2341 2.7347 -5.1963 -12.3070 -17.4747 -19.8836 -19.1532 -15.3990 -9.2136
Columns 145 through 156
-1.5736 6.3149 13.2063 18.0128 19.9754 18.7844 14.6277 8.1616 0.4070 -7.4119 -14.0606 -18.4894
Columns 157 through 168
-19.9992 -18.3516 -13.8066 -7.0819 0.7609 8.4836 14.8669 18.9030 19.9548 17.8561 12.9384 5.9780
Columns 169 through 180
-1.9263 -9.5264 -15.6225 -19.2521 -19.8423 -17.2998 -12.0260 -4.8537 3.0850 10.5367 16.3248 19.5356
Columns 181 through 192
19.6621 16.6845 11.0727 3.7128 -4.2333 -11.5110 -16.9714 -19.7524 -19.4149 -16.0122 -10.0816 -2.5593
Columns 193 through 203
5.3671 12.4461 17.5602 19.9018 19.1015 15.2854 9.0561 1.3970 -6.4826 -13.3388 0
>> v=a+b Matrix dimensions must agree.

Best Answer

Adding the ‘0’ elements in ‘a’ and ‘b’ are causing the problem, as well as horizontally concatenating the sin and cos terms in ‘b’.
Correcting those problems and vertically concatenating the sin and cos terms in ‘b’, and this works:
t = [0:0.4:40];
b = 10-(t/4).^2;
a = [20.*sin(t); 20.*cos(t)];
v = bsxfun(@plus, a, b);
NOTE With R2016b (if I remember correctly) and later, you will not need the bsxfun call. I include it here for comaptibility.