Matlab error: Inner matrix dimensions must agree

Click For Summary
SUMMARY

The forum discussion addresses a MATLAB error encountered when attempting to plot a function involving matrix exponentials. The user defines the z-Pauli matrix and identity matrix, then attempts to compute and plot the expression using the command expm(cos(t).*I + 1i.*cos(t).*s_3) * [1;2]. The error "Inner matrix dimensions must agree" arises due to incorrect element-wise multiplication of a vector with a matrix. The solution involves using proper matrix multiplication instead of element-wise operations.

PREREQUISITES
  • Understanding of MATLAB syntax and operations
  • Familiarity with matrix operations in MATLAB
  • Knowledge of matrix exponentials and their applications
  • Basic experience with plotting functions in MATLAB
NEXT STEPS
  • Learn about MATLAB matrix multiplication versus element-wise multiplication
  • Explore the use of the expm function in MATLAB for matrix exponentials
  • Investigate how to define and plot custom functions in MATLAB
  • Study error handling in MATLAB to troubleshoot common issues
USEFUL FOR

MATLAB beginners, students in engineering or physics, and anyone working with matrix computations and visualizations in MATLAB.

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

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K