Matlab error: Inner matrix dimensions must agree

In summary, the programmer is trying toplot a function of a matrix exponential of a variable t. He has defined A, epsilon, and Omega, and calculated s, but when he tries to use it with t as the argument, he gets an error.
  • #1
Marin
193
0
Hi all!

I am having great trouble in the last days trying to make MATLAB plot for me a function of a matrix exponential of a variable t.

Here's my attempt:

first, I define the z-Pauli matrix s_3 and the identity matrix I by:

%define the pauli matrices and the identity I%
I = [1 0; 0 1];
s_1 = [0 1; 1 0];
s_2 = [0 -1i; 1i 0];
s_3 = [1 0; 0 -1];

Then I want to plot the (real part) of both components of the vector-valued function

x = (expm(cos(t).*I + 1i.*cos(t).*s_3) )*[1;2]

using the commands:

t = 0:0.1:10;
x = (expm(cos(t).*I + 1i.*cos(t).*s_3) )*[1;2];
plot(t,x(t))

Unfortunately I got the following error:

? Error using ==> mtimes
Inner matrix dimensions must agree.

I suspect that this is because t is interpreted as a vector, say 100 components, and cos(t) is taken componentwise, so that cos(t) is also a vector of the same number of components and the error occurs when this vector is multiplied by a 2 by 2 matrix from the left.

However, I'd like MATLAB to compute the matrix exponential, multiply it by the given vector, and then plug in the t, to avoid the above error.

Does anyone have any ideas on how to make this work?

I also want to use a more complicated function defined by me instead of the sin(t) in the above expression, but I guess, if I find how to do it with a simple one like sin(t), it'll work also with manually defined functions as well


PS: I'm new to MATLAB and programming and unfortunately don't have much experience with it


Thanks a lot for the help!

With regards,
marin
 
Physics news on Phys.org
  • #2
I notice that you're using the element-wise operation .* when I think you probably want to do actual matrix multiplication.

For instance:
>>A=[0,0;1;0]
>>B=[1,0;0,0]

A.*B gives you a 2x2 zero matrix. A*B gives you back A.

So when you have t (a 1x2 vector) and then element-wise multiply it by a 2x2 matrix, you get an error.
 
  • #3
How about using loop instead?

for t = 0:0.1:10

...
end
t = 0:0.1:10
plot(t,x(t)) ? I thought t must be integer positive in x(t)
 
  • #4
Hi all!

Thanks for your replies!

matematikawan, I guess it doesn't work with a loop, since it gives the error message:

Error: Expression or statement is incomplete or incorrect.

MATLABdude, I think the problem is not in the type of multiplication, here's an example which cannot be plotted, however it can indeed be evaluated at any point t - how can this happen?!?

function s = s(t)
%define the constants A, epsilon and Omega, initial conditions [x_0;y_0]%
A = 0.5;
W = 2;
e = 1;
x_0 = 1;
y_0 = 2;
%define the identity matrix, sigma_z and R%
I = [1 0; 0 1];
s_3 = [1 0; 0 -1];
R = [-1 1; -1 1];
%define functions beta(r), rho (r), tau (d)%
b = A/W*sin(W*t);
d = -A/W^2*cos(W*t);
r = A^2/(2*W^3)*(W*t -1/2*sin(2*W*t));
%define the second part of the particular solution z_0 and its initial position z_1%
z_0 = -1i*e*(t*s_3 + 1i*d*(s_3*R - R*s_3) + r*R*s_3*R )*[x_0; y_0];
z_1 = -1i*e*( -1i*A/W^2*(s_3*R - R*s_3) )*[x_0; y_0];

s = (I + 1i*b*R)*([x_0; y_0] - z_1 + z_0);

and the error to it:

plot(t,s(t))
? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> s at 17
z_0 = -1i*e*(t*s_3 + 1i*d*(s_3*R - R*s_3) + r*R*s_3*R )*[x_0; y_0];

any further ideas?
 
  • #5


Hi Marin,

It seems like the error you are getting is due to the mismatch in dimensions between the matrix exponential and the vector you are trying to multiply it with. To avoid this error, you can use the element-wise multiplication operator '.*' instead of the regular multiplication operator '*'. This will ensure that the dimensions of the matrix exponential and the vector are the same, and the multiplication can be performed.

So your code would look like this:

t = 0:0.1:10;
x = (expm(cos(t).*I + 1i.*cos(t).*s_3) ).*[1;2];
plot(t,x(t))

By using the element-wise multiplication, you are essentially multiplying each element of the matrix exponential by the corresponding element in the vector, resulting in a vector of the same dimensions as the original vector.

I hope this helps! Good luck with your MATLAB programming and feel free to reach out if you have any further questions.
 

1. Why do I get the error "Inner matrix dimensions must agree" in Matlab?

This error occurs when trying to perform an operation between two matrices, but their inner dimensions (number of columns in the first matrix and number of rows in the second matrix) do not match.

2. How can I fix the "Inner matrix dimensions must agree" error in Matlab?

To fix this error, you can either adjust the dimensions of your matrices so that they match, or use a different operation that is compatible with your matrices' dimensions.

3. Can this error occur with vectors in Matlab?

Yes, this error can also occur when trying to perform an operation on two vectors with different lengths. In this case, the inner dimensions refer to the lengths of the vectors.

4. Is there a way to avoid getting the "Inner matrix dimensions must agree" error in Matlab?

To avoid this error, it is important to carefully check the dimensions of your matrices before performing any operations. You can also use the built-in functions "size" or "length" to double check the dimensions of your matrices.

5. Are there any other common errors that are related to "Inner matrix dimensions must agree" in Matlab?

Yes, other common errors that are related to this one include "Matrix dimensions must agree" and "Array dimensions must match for binary array opertators". These all indicate a mismatch in the dimensions of matrices or vectors being used in an operation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
246
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
985
Back
Top