Jesus
Jesus

Reputation: 179

"Inner matrix dimensions must agree" MATLAB error

I am using the following equation in Matlab:

k=10e-10:0.01:1.5;
Ck2=(0.5*((i*k+0.135)*(i*k+0.651)))./((i*k+0.0965)*(i*k+0.4555));
plot(k,imag(Ck2));
plot(k,real(Ck2));

I did not define "i" so MATLAB assumes is an imaginary number in my equation as expected. I am trying to plot the real & imaginary parts of the equation against the range of k.

I am getting an error saying: Inner matrix dimensions must agree. I already tried to use the "." operator before the multiplication operator to multiply each element but I didn't succeed. Any help would be appreciated it.

Thank you in advanced.

Upvotes: 1

Views: 88

Answers (1)

fileyfood500
fileyfood500

Reputation: 1321

Since k is a vector, when you multiply k * k, you are multiply 2 vectors using matrix multiplication. With matrix multiplication, you multiply an j x k size matrix by a k x l size matrix, and get a j x l result.

But here you are multiplying 1 x 150 by 1 x 150, so the dimensions don't line up for proper matrix multiplication. Instead, using .* will perform pairwise multiplication between each of the elements.

Try this:

k = 10e-10:0.01:1.5;  % 1 x 150 length vector
Ck2= (0.5*((i*k+0.135) .* (i*k+0.651))) ./ ((i*k+0.0965) .* (i*k+0.4555));

Upvotes: 1

Related Questions