MATLAB Matlab error: Inner matrix dimensions must agree

AI Thread Summary
The MATLAB error "Inner matrix dimensions must agree" arises when attempting to perform matrix multiplication with incompatible dimensions. The user is trying to compute a matrix exponential and multiply it by a vector, but the element-wise operation used with cos(t) results in a vector that cannot be multiplied by the 2x2 matrix. Suggestions include using a loop to iterate over values of t or ensuring proper matrix multiplication instead of element-wise operations. The user also seeks to apply a more complex function in future attempts, indicating a need for foundational understanding of matrix operations in MATLAB. Further clarification on matrix dimensions and operations is essential for resolving the issue.
Marin
Messages
192
Reaction score
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
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.
 
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)
 
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?
 

Similar threads

Back
Top