# Math Help - matlab error: Inner matrix dimensions must agree

1. ## matlab error: Inner matrix dimensions must agree

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

2. Originally Posted by Marine
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
Hi there. There are a number of issues with this piece of code.

The first, as you point out, is with MATLAB's inability to compute the product you are requestion. The reason for this is that cos(t) for t= 0:0.1:10 is a vector with 101 elements, whilst both I and s3 are 2x2 matrices, and so matlab cannot multiply them together. I can see that you'd tried to solve this problem by using .* instead of *, but .* means 'multiply each element of the first matrix with each element of the second', but that only works for matrices of the same size.

To solve this problem, I would use a for loop where the single value of t changes at every iteration.

The second problem I can see is that your equation gives a vector as an output, as you were expecting. However, you can't assign a vector value to an array variable.

By that I mean, if you say x(1) = [5;2];, matlab will give you an error, because you can only assign single values to x(1), e.g. x(1) = 5. So you have a problem in storing your solutions. I suggest storing them in cells instead of in an array, so you would simply use square brackets instead of normal brackets.

Finally, as x will be a variable containing 101 vector solutions, how do you expect matlab to plot it? The plot function takes a vector of single values and plots it against another vector of single values - it cannot deal with variables whose values are vectors, and so you will need to find another way to display your results, depending on precisely what it is that you want to display.

Given these problems, I suggest you write the code like so:

I = [1 0; 0 1];
s_1 = [0 1; 1 0];
s_2 = [0 -1i; 1i 0];
s_3 = [1 0; 0 -1];

x = cell(101,1); %this initialises the size of the cell, which saves time
i = 0; % this starts a counter, which will allow to store data at each iteration of the for loop
for t = 0:0.1:10; %a for loop where t changes upon every iteration
i = i+1; % add 1 to the counter
x{i} = (expm(cos(t).*I + 1i.*cos(t).*s_3) )*[1;2]; % store the vector-valued solution in cell 1 of the cell array x
end % end the for loop

If you explain to me what it is that you want to ploy, I might be able to help.

3. Hi, Phugoid!

Thanks a lot for your help!

I really don't have much knowledge about programming, but here's what I want matlab to do for me:

Consider some constant matrix A and a function w(t). Then the function expm^(w(t)*A) is another matrix /matrix exponential defined as usual via taylor series/ which is now t-dependent, i.e. a matrix-valued function of t. If I multiply this new matrix by a constant vector, I get my final vector-valued function f(t), both componnets of which I would like to plot.

e.g. f(t) = expm^(w(t)*A)*[1;2]

much like the plot for solving an ode numerically, where the commands

[t,z] = ode45('num',[0 10],[1;2])
plot(t,z)

compute the numerical solution to the ode given via the manually defined function num(t,z) and then plots both components of the vector-valued z

This type of calculation occurs when solving a system of linear ODE's

What I'm trying to do here is to compare my analytical solution to the numerical, but I'm not able to plot the analytical

4. Originally Posted by Marine
Hi, Phugoid!

Thanks a lot for your help!

I really don't have much knowledge about programming, but here's what I want matlab to do for me:

Consider some constant matrix A and a function w(t). Then the function expm^(w(t)*A) is another matrix /matrix exponential defined as usual via taylor series/ which is now t-dependent, i.e. a matrix-valued function of t. If I multiply this new matrix by a constant vector, I get my final vector-valued function f(t), both componnets of which I would like to plot.

e.g. f(t) = expm^(w(t)*A)*[1;2]

much like the plot for solving an ode numerically, where the commands

[t,z] = ode45('num',[0 10],[1;2])
plot(t,z)

compute the numerical solution to the ode given via the manually defined function num(t,z) and then plots both components of the vector-valued z

This type of calculation occurs when solving a system of linear ODE's

What I'm trying to do here is to compare my analytical solution to the numerical, but I'm not able to plot the analytical
In that case, I would suggest the code should look like this...

Code:
I = [1 0; 0 1];
s_1 = [0 1; 1 0];
s_2 = [0 -1i; 1i 0];
s_3 = [1 0; 0 -1];

i = 0; % this starts a counter, which will allow to store data at each iteration of the for loop
for t = 0:0.1:10; %a for loop where t changes upon every iteration
i = i+1; % add 1 to the counter
x = (expm(cos(t).*I + 1i.*cos(t).*s_3) )*[1;2]; % store the vector-valued solution in cell 1 of the cell array x
z(i,1) = x(1);
z(i,2) = x(2);
end % end the for loop

figure(1)
hold on
plot(real(z(:,1)),imag(z(:,1)),'r--')
plot(real(z(:,2)),imag(z(:,2)))
xlabel('Real Axis')
ylabel('Imaginary Axis')
Now, you are computing the vector valued function, x, at each time step. And then you are assigning each element of the vector to a particular column of the vector z.

Hence, at the end of the loop, your vector z will be a vector with 101 rows and 2 columns. The first column is one set of complex solutions at each time step, the second column is the other.

All that remains is to plot them, which is done as is shown in the code I've given.

If this is what you were looking for, let me know.

5. Thanks, man

It really works now

Are z(i,1) = x(1); z(i,2) = x(2); functions of t in this case? I mean can I plot the real part of each of them against the variable t in the real plane instead against one another in the complex plane?

plot(t,x(1)) doesn't really plot it

6. the previous post popped up twice

7. Originally Posted by Marine
Thanks, man

It really works now

Are z(i,1) = x(1); z(i,2) = x(2); functions of t in this case? I mean can I plot the real part of each of them against the variable t in the real plane instead against one another in the complex plane?

plot(t,x(1)) doesn't really plot it
The variable x is just a temporary variable within the loop... the values of z are not saved within it, they are only saved within the variable z.

At the end of that code you should write the following:

time = 0:0.1:10;
Realz1 = real(z(:,1));
Realz2 = real(z(:,2));
Imagz1 = imag(z(:,1));
Imagz2 = imag(z(:,2));

So now what I've done here is split the solution vector z into the real and imaginary parts of each column of the solution file.

So, if you wanted to plot the real part of both elements of the vector solution against time on the same graph, then you'd write, at the end...

hold on
plot(time,Realz1,'r')
plot(time,Realz2,'b')

They should show up as red and blue curves respectively.

8. now, this is what I need!

Thanks, once again!