Matlab error: Inner matrix dimensions must agree

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a MATLAB error related to matrix dimensions when attempting to plot a function involving matrix exponentials and vector multiplications. Participants are exploring the implications of matrix operations and the correct usage of MATLAB syntax in the context of programming and plotting.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their attempt to plot a function involving the matrix exponential of a variable t and encounters an error regarding inner matrix dimensions.
  • Another participant suggests that the use of element-wise multiplication (.*) may be incorrect and that actual matrix multiplication should be used instead.
  • A different participant proposes using a loop to iterate over values of t, although they express uncertainty about the necessity of t being an integer.
  • A later reply indicates that a loop does not resolve the issue and raises a new example function that also produces a similar error, questioning why it can be evaluated at any point t but fails during plotting.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the cause of the error or the best approach to resolve it. There are competing views on whether the issue lies in the type of multiplication or the structure of the code.

Contextual Notes

Participants express uncertainty regarding the handling of matrix operations in MATLAB, particularly in relation to vector dimensions and the implications of using element-wise versus matrix multiplication. There are also unresolved questions about the behavior of functions defined within the context of plotting.

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
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K