How do I force real solutions using linsolve(B,x) or mldivide(B,x).

3 visualizzazioni (ultimi 30 giorni)
I am currently writing a function just for fun/to check some answers.
The aim is to input B, a basis in array format (which can take complex numbers), and T, as a transformation matrix, and find the matrix with respect to the basis. (B[T]B is common notation).
I currently have this code:
function A = BasisMatrix(T, B)
L = length(B);
for n = 1:L
x = T*B(:, n);
realx = real(x); % (1)
imagx = imag(x); % (2)
realB = real(B); % (3)
imagB = imag(B); % (4)
realA = linsolve(realB, realx) % (5)
imagA = linsolve(imagB; imagx) % (6)
% For (1) - (6) I have tried forcing real solutions by solving two separate real systems of equations
a = linsolve(B, x);
A(:, n) = a;
end
end
The error comes in at (6) where I have an unbalanced parentheses error for some reason. Could somebody explain why and how this could be ammended?
Thanks

Risposte (2)

Torsten
Torsten il 28 Nov 2014
Use
imagA = linsolve(imagB, imagx) % (6)
instead of
imagA = linsolve(imagB; imagx) % (6)
Best wishes
Torsten.

Matt J
Matt J il 28 Nov 2014
If a real solution X exists to P*X=Q where P and Q are given and (possibly) complex matrices, then it can be done in one line as follows
X=[real(P);imag(P)]\[real(Q);imag(Q)];

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by