The function I need to plot is y = exp(-0.3*t)*(2*cos(2*t) + 4*sin(2*t)) for the range of values of t between 0 and 2*pi.
I entered the following commands on MATLAB:
>> t=linspace(0,2*pi,101);
>> y=exp(-0.3*t)*(2*cos(2*t) + 4*sin(2*t));
And I come up with the following error:
Error using *
Inner matrix dimensions must agree.
I don't know why. Can someone point out why and suggest the correct command line argument?
Thanks!
Your issue is in this term:
exp(-0.3*t) * (2*cos(2*t) + 4*sin(2*t));
You are multiplying 2 vectors. You want to be doing element-wise operations, i.e. each element of exp(-0.3*t) times each corresponding element of (2*cos(2*t) + 4*sin(2*t)), rather than the vector product of the two.
To achieve what you want, simply add a dot . before the multiplication *, like so
y = exp(-0.3*t) .* (2*cos(2*t) + 4*sin(2*t));
See this documentation for array vs. element-wise operations: http://uk.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
The "*" operator is a matrix-multiplication operator, like https://en.wikipedia.org/wiki/Matrix_multiplication
You need to use an ".*" operator which is a per-element operator. You must use it to match elements from one vector or matrix to the elements from the other vector or matrix one-to-one.
So you have to do
y=exp(-0.3*t).*(2*cos(2*t) + 4*sin(2*t));
Note that ".*" is not needed when multiplying by constant, because the effect is the same for matrix and per-element operation
Related
I have a Parameter matrix, C of size 2x2. It looks like this.
C= [2 4; 6 8]
I have a decision variable, X of size 2x2. It looks like this
[ X('S1', 'D1') X('S1', 'D2') ]
[ X('S2', 'D1') X('S2', 'D2') ]
I want to formulate my Objective Function as a series of double expression (Please refer the attachment),
Which shall look like the following after the expansion.
Z = 2*X('S1', 'D1') + 4*X('S1', 'D2') + 6*X('S2', 'D1') + 8*X('S2', 'D2')
I try the following.
Z = sum(C.*X,1);
But it creates An optimizationExpression of size 1x3, which is not desired.
What am I doing wrong? Is there any easier way to do so without using for loop. I have just started learning "Problem-Based Optimization" in MATLAB today. Any help will be greatly appreciated.
As C and X are 2x2 matrices, so C.*X gives a 2x2 matrix. With sum(C.*X,1);, summation is done along the first dimension (i.e. rows are added). But you want to sum all the elements. So if you convert your matrices into a vector and then multiply element-wise and then apply sum, it will add all the elements i.e.
Z = sum(C(:).*X(:));
Alternatively, you can first multiply and then convert the result into a vector before applying sum.
CX = C.*X;
Z = sum(CX(:));
or sum along all the dimensions one by one. But I'd go with the solution suggested in the beginning.
The answer to the question asked here Why is complex conjugate transpose the default in Matlab
says that for complex numbers we can use ' symbol to denote the transpose operation that is used for real numbers. Mathematically, the transpose operation that is done for real valued numbers is denoted by the symbol (.)^T . For the transpose of complex numbers the equivalent symbol is (.)^H. The way it is done is -- First we take the conjugate of the complex number and then take its transpose. This is the operation (.)^H.
I want to implement the operation (.)^{*T} = (.)^H for complex number. I have used the symbol apostrophe for this. Please correct me where wrong.
I wanted to confirm if my implementation of the concept is correct or not using Matlab. For example, for real valued vector A_r, I want to multiply it with its transpose multiply_r = A_r*A_r'
Replicating the same for complex valued vector A_c, this operation would become multiply_c = A_c * A_c'
A_r =[1,2,3]; %real valued vector
B_r = A_r'; %transpose of real valued vector
multiply_r =A_r*B_r;
A_c = [1 + sqrt(-1)*1, 2+sqrt(-1)*2, 3+sqrt(-1)*3]; %complex valued vector
B_c = A_c'; %transpose of complex valued vector
multiply_c = A_c*B_c;
Is this okay?
UPDATE : I am trying to take the normal transpose of this complex valued array, so that it is arranged in 3 rows and 1 column intead of 1 row and 3 columns. Using the operator .' I am getting weird values because the array is now increased in size !! What is the proper way?
h = [ -5.1053 + 3.6797i 1.3327 + 5.7339i 4.1302 -10.7521i].'
h =
-5.1053 + 3.6797i
1.3327 + 5.7339i
4.1302
0 -10.7521i
As you noted, Matlab has both matrix "transpose" ((.)^T) and "conjugate transpose" ((.)^H) defined.
For real-valued transpose, you have transpose, that can be expressed as an operator .' (note the '.' before the '):
aT = transpose(a);
isequal( aT, a.' ); % transpose() and .' are the same
For complex conjugate transpose you have ctranspose, that can be expressed as an operator ' (note there is no . before the '):
aH = ctranspose(a);
isequal( aH, a' ); % ctranspose and ' are the same
You can verify using conj:
isequal( a', conj(a).' );
I have a (transition) function defined by a matrix say P=[0.75,0.25;0.25,0.75] and I have a vector say X=[1,2,1] then i would like to find P(1,2)*P(2,1). How is the easiest way to generalise this? I tried creating a function handle for P(i,j) and then X_temp=[X(1:end-1);X(2:end)], using the function of each column and finally using the product function, but it seems a lot more comprehensive than it has to be.
The X i want to use is 1000 dimensional and P is 3x3 and I would have to repeat it a lot of times so speed I think will matter.
You can use sub2ind to get your relevant P values:
Ps = P(sub2ind(size(P), X(1:end-1), X(2:end)))
Now just multiply them all together:
prod(Ps)
EDIT:
For function handles you had the right idea, just make sure that you function itself handles vectors. For example lets say your function f(i,j) = i + j, I'm going to assume it's actually f(x) = x(1) + x(2) but I want it to handle many xs at once sof(x) = x(:,1) + x(:,2):
f = #(x)(x(:,1) + x(:,2))
f([X(1:end-1)', X(2:end)'])
OR
f = #(ii, jj)(ii + jj)
f(X(1:end-1)', X(2:end)') %//You don't actually need the transposes here anymore
just note that you need to use element wise operators such as .*, ./ and .^ etc instead of *, /,^...
When I try to plot a function h in MATLAB, using a variable omega which is defined as its own function, I get an Inner matrix dimensions must agree, error using _*_ response from the console.
The function works when I use a + between the seperate function-components of h; It does not work when I try multiplying the two inner functions in h, which is, from what I guess, what causes the matrix dim error.
function h = freqp(omega)
k = (1:1024-1);
hh = (1:1024-1);
omega = zeros(length(k),1);
omega = (k-1)*((2*pi)/1024);
hh = 2*exp((-3j)*omega)*cos(omega); % This works for ...omega) + cos(...
% but not for ...omega) * cos(, why?
y = fft(hh);
stem(real(y), omega);
How can I solve this? I read the info on mathworks but it only gives a solution for e.g. loading a file. Any help would be greatly appreciated!
Since Omega is a vector, the addition works. But multiplication of two vectors will result as a matrix. You can modify
hh = 2*exp((-3j)*omega)*cos(omega);
as
hh = 2*exp((-3j)*omega)*(cos(omega))';
or looking for element wise multiplication,
use
hh = 2*exp((-3j)*omega).*cos(omega);
The part exp((-3j)*omega worked fine because -3j is a complex scalar and omega a vector. Thus, MATLAB multiplies each element of omega with -3i. However, that result is a vector itself. Also cos(omega) is a vector, and both are row vectors.
In this case, with two vectors, the *-operator means dot product but that would be calculated between a column vector and a row vector, not two row vectors. So, [1 2 3] * [4 5 6] will raise the same error you are reporting, but [1 2 3] * [4 5 6]' yields 32.
From invoking fft on hh your code looks, however, as if you never intended to calculate a dot product (a scalar) but instead were looking for element-wise multiplication. The operator for element-wise multiplication is .*, such that your expression would be instead
hh = 2*exp((-3j)*omega).*cos(omega);
It's from this question。
Why the two solutions doesn't work, though it looks very valid for me:
>> t = -pi:0.1:pi;
>> r = ((sin(t)*sqrt(cos(t)))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2 ;
??? Error using ==> mtimes
Inner matrix dimensions must agree.
>> t = -pi:0.1:pi;
>> r = ((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2 ;
>> plot(r,t)
??? Error using ==> plot
Vectors must be the same lengths.
What's wrong with the above?
The * operator is the matrix multiplication operator, which requires its operands to have matching inner matrix dimensions. The .* operator is the element-wise multiplication operator, which requires its operands to have the same size (or for one to be a scalar) so it can perform multiplication on each matching pair of elements. See this link for more detail.
Also, I don't get the plotting error you do when I run the second solution. I just get this warning:
Warning: Imaginary parts of complex X and/or Y arguments ignored