Confusions about Multiplication of Complex Value in MatLab - matlab

I noticed a confused computation of complex valued multiplication in Matlab. One simple example is as below:
syms x1 x2 x3 x4
s=[x1 x2]*[x3 x4]'
And the return value of s is like:
s=x1*conj(x3) + x2*conj(x4)
In my opinion s should be equal to x1*x3+x2*x4. So, what's the problem here?
Then, how should I do if I want to get a multiplication of two complex vector?
update: I find out it will be solved through using .' rather than . like:
s=[x1 x2]*[x3 x4]

The operator ' is the also called Complex conjugate transpose in Matlab ctranspose, which basically means that it applies conj and and transpose functions. Note that this operator is call Hermitian operator in mathematics.
What you actually want is the operator transpose that is shortcut as .'
In order to get the expected output, and given that you just want to multiply without conjugating the second vector, you should do:
>> syms x1 x2 x3 x4
>> s = [x1 x2]*[x3 x4].'
so your output will be:
x1*x3 + x2*x4
For further information you can check help ., to see the list of operators, help transpose and help ctranspose

Note that the ' operator in Matlab is the conjugate transpose, i.e. it both transposes a matrix and takes the complex conjugate:
>> (1+1i)'
ans =
1.0000 - 1.0000i
If you want the matrix transpose then you should use the .' operator:
>> (1+1i).'
ans =
1.0000 + 1.0000i

Maybe this will help explain:
>> syms x1 x2 x3 x4
>> s=[x1 x2]*[x3 x4]'
s =
x1*conj(x3) + x2*conj(x4)
>> s=[x1 x2]*[x3; x4]
s =
x1*x3 + x2*x4
>> [x3 x4]'
ans =
conj(x3)
conj(x4)
The ' version of transpose isn't doing what you want. Use transpose instead:
>> transpose([x3 x4])
ans =
x3
x4

Related

Evaluate symbolic matrix in MATLAB

I defined 2 symbolic matrix in MATLAB, for example
w = sym('w',[10,10])
Then I do some operations on it and get function E dependent to symbolic matrix w and v. Now, I want to evaluate E numerically with numerical w and v.
How can I do this?
Simple example on R2017a:
>> syms E(v,w)
>> E(v,w) = v*w + v;
>> E(3,4)
ans =
15
On earlier versions, I believe symfun is the command to use.
We can use such code:
>> w=sym('w',[10 10]);
>> d=sym('d',[10 1]);
>> E=W*d + ...(some other operations)
>> define a numerical matrix f and vector x)
>> subs(subs(E,w,f),d,x)
This code performed in R2014a and had a correct answer.

writing function using varargin and varargout

I have this function and I want to use varargin and varargout for all the inputs and the outputs and I don't know exactly how to do it.
Any suggestions? This is my function:
function [Output0,Output1] = myfunction(p0,p1,normal0,normal1,c0,c1)
t0 = sqrt((c0^2)/((normal0(1)^2) + (normal0(2)^2) + (normal0(3)^2)));
Output0= p0 + normal0*t0;
t1 = sqrt((c1^2)/((normal1(1)^2) + (normal1(2)^2) + (normal1(3)^2)));
Output1= p1 + normal1*t1;
Thanks in advance
I don't think this is an appropriate case for varargin or even for nargin. This is a case for vectorizing your function.
OK so lets say you have these inputs: xo, yo, zo, x1, y1, z1 (all scalar), normal0 (1x3), normal1 (1x3) and c0 and c1 both scalar.
Lets see if we can vectorize your function to calculate all the outputs in one shot. So first we'll rearrange your data:
P = [x0, x1;
y0, y1
z0, z1];
N = [normal0;
normal1]'; %better here to just make normal0 a (3x1) so no need to transpose
C = [c0, c1]
now lets look at how you got your first output:
t0 = sqrt((c0^2)/((normal0(1)^2) + (normal0(2)^2) + (normal0(3)^2)));
Output0= p0 + normal0*t0;
this can be simplified to
p0 + normal0 * sqrt(c0^2/sum(normal0.^2))
which can be generalized to
P + bsxfun(#times,N,sqrt(bsxfun(#rdivide,C.^2,sum(N.^2))))
So now you get any number of outputs in one shot! In one line too!
Just a quick explanation of where bsxfun came into it. So in your original calcs you sometimes multiply or add a scalar to a vector. Matlab allows this but it doesn't allow the higher dimensional case of say adding a vector to a 2D matrix. bsxfun does this for us. so where I have bsxfun(#times, N, B) above, it just takes the 3x1 B vector and does an elementwise multiplication (#times is the function handle to .*) of B on each column of the 3x2 N. But here it's fine for N to be 3xX i.e. have any number of columns i.e. any number of inputs.

Transpose 1D to 2D by Group in Matlab

I need to transpose a vector into a 2D matrix according to a group of values that are equal in another column of a matrix. For example:
1 x1
1 x2
1 x3
1 x4
2 x5
2 x6
2 x7
2 x8
Should look like:
x1 x2 x3 x4;
x5 x6 x7 x8;
This is the same procedure you would do in SAS using proc tabulate. Reshape didn't work for me because it doesn't transpose it, and tried permute with no luck either. Is there any built in command that does this besides having to program it in using find, transpose, and vertcat?
If for some reason you want to avoid reshape, although the solution in comments will work, you can use sub2ind to get the linear indices of the new matrix V, given that your first column will always provide the new line sub:
X = [[1,1,1,1,2,2,2,2]' (1:8)'];
subs = X(:,1);
M = length(unique(subs)); % count unique ids
N = length(X)./M; % Problem assumption: M sets of size N (MxN=length(X))
V = zeros(M, N);
i = sub2ind([M, N], subs, repmat(1:N,1,M)');
V(i) = X(:,2);
The above, according to your specs, will work as long as there is an equal number of unique elements in X, so you can form an MxN matrix.

how do you do symbolic differentiation on function handle?

Say if i define the following:
g = #(x) x/sqrt(x^2+1)
How do i get the derivative function for g, which i can then use to evaluate at different points?
I tried the symbolic math toolkit, and tried the following:
>> syms x
>> f = x/sqrt(x^2+1)
f =
x/(x^2 + 1)^(1/2)
>> diff(f)
ans =
1/(x^2 + 1)^(1/2) - x^2/(x^2 + 1)^(3/2)
However, i cannot figure out how to turn this into a function handle/evaluate at different points. However, i prefer doing differentiation on function_handle.
Thank you very much!
Jason
You can use matlabFunction to convert a symbolic equation to a function. For example:
syms x1 x2;
f1 = x1^2+x2^2;
Df1 = jacobian(f1, [x1 x2]);
Df1 = matlabFunction(Df1);
Then Df1(0, 0) returns [0 0] as expected.
The function matlabFunction was introduced in version 5.2 (R2009a) of the Symbolic Math Toolbox.

How do I solve a determinant in MATLAB?

As a simple example, let's say you have this matrix:
M = [omega 1;
2 omega];
and you need to solve for the values of omega that satisfy the condition det M = 0.
How do you do this in MATLAB?
It is surely something simple, but I haven't found the function yet.
For the general case where your matrix could be anything, you would want to create a symbolic representation of your matrix, compute the determinant, and solve for the variable of interest. You can do this using, respectively, the functions SYM, DET, and SOLVE from the Symbolic Math Toolbox:
>> A = sym('[w 1; 2 w]'); % Create symbolic matrix
>> solve(det(A),'w') % Solve the equation 'det(A) = 0' for 'w'
ans =
2^(1/2)
-2^(1/2)
>> double(ans) % Convert the symbolic expression to a double
ans =
1.4142
-1.4142
There are also different ways to create the initial matrix A. Above, I did it with one string expression. However, I could instead use SYMS to define w as a symbolic variable, then construct a matrix as you normally would in MATLAB:
syms w
A = [w 1; 2 w];
and now A is a symbolic matrix just as it was in the first example.
If you don't have the symbolic toolbox, then use the sympoly toolbox, found on the file exchange.
sympoly omega
roots(det([omega 1;2 omega]))
ans =
-1.4142
1.4142
Well the determinate is:
om * om - 1*2 = 0
So you would get: om*om = 2
The formal definition is: [a b ; c d] = ad - bc
I would look into simplifying the determinate, and finding a solver to solve for the unknowns.