Turn imaginary numbers to complex

11 views (last 30 days)
Adam Holland
Adam Holland on 1 Apr 2016
Edited: Walter Roberson on 1 Apr 2016
I am trying to write a program to help me solve systems of differential equations which also includes systems with complex Eigen values and vectors. Everything is going pretty well however with some matrices the Eigen values don't have a real part to them, only imaginary. The problem with that is that the solution to the system of equations with complex Eigen values (lambda) takes the form y=C1*e^(alpha*t)(cos(beta*t)...... with plenty more to follow, where alpha is the real part of the complex value and beta is coefficient of the imaginary part (lambda=alpha+beta*i).
Here's an example:
clear,clc
syms l u1 u2 u3 u4
A=[-6 -4;10 6] %Coefficient matrix of system
sa=size(A);
I=eye(sa(1),sa(2)); %Creating an Identity matrix of the same size as A
A-l*I
Det=det(A-l*I)
l=solve(Det) %Finds the Eigen values of the system
With output:
A =
-6 -4
10 6
ans =
[ - l - 6, -4]
[ 10, 6 - l]
Det =
l^2 + 4
l =
-2i
2i
As you can see my Eigen values (l) are not of the form alpha+beta*i. Is there a way to have Matlab recognize if the values are complex, real, or imaginary, and convert to complex if they are not already so that when creating the solution I have a way of automatically creating an array of all values of alpha and all values of beta?

Answers (3)

Walter Roberson
Walter Roberson on 1 Apr 2016
Call real() and imag() to get the parts.
But your real issue is that the result of solve() is symbolic and you are thinking it is double precision. double(I) to get the form you are expecting.
Note: I advise against assigning to a symbolic variable after having used it in an expression. Doing so has a high chance of introducing bugs.

Roger Stafford
Roger Stafford on 1 Apr 2016
Your statement "my Eigen values (l) are not of the form alpha+beta*i" really isn't correct. They are both of that form where alpha in this case equals zero and beta is 2 or -2. Moreover if you ask for the real part using "real(l)" for alpha (where 'l' is a double,) you will get zero for an answer. You shouldn't be misled by the failure of matlab to specifically print the zero part of symbolic complex quantities which are pure imaginary.

Adam Holland
Adam Holland on 1 Apr 2016
Edited: Walter Roberson on 1 Apr 2016
Ok so I understand what you're saying.
And Roger, just to clarify yes you are right about my statement being technically incorrect but what I was trying to say but in the wrong choice of words is that my outputs are of the form -2i with no value shown for alpha rather than 0-2i which is what I am looking for.
So now what I have done is after assigning my values to l which will be symbolic, I am converting to double which using the same matrix and code with the only change being the addition of l=double(l) after solving Det, I get the output Im looking for. However when I try to get the real values using 'real' I get an error. Then just to see what would happen I tried getting the real values from 'U' and got the same message. And just to clarify U just another array used in later parts of the code that for this particular matrix has complex values however because it and the code that applies to it are later in the program and so far have no problems I am not including it at the moment.
My new code and outputs are given below:
clear,clc
syms l u1 u2 u3 u4
A=[-6 -4;10 6] %Coefficient matrix of system sa=size(A);
I=eye(sa(1),sa(2)); %Creating an Identity matrix of the same size as A
A-l*I
Det=det(A-l*I)
l=solve(Det)
l=double(l)
With output:
A =
-6 -4
10 6
ans =
[ - l - 6, -4]
[ 10, 6 - l]
Det =
l^2 + 4
l =
-2i
2i
l =
0.0000 - 2.0000i
0.0000 + 2.0000i
--and then using the command window--
>> real(l)
Subscript indices must either be real positive
integers or logicals.
What I not understanding or doing wrong?
  1 Comment
Steven Lord
Steven Lord on 1 Apr 2016
You have a variable in your workspace named real or you have created your own function named real that is taking precedence over the real function in MATLAB and is using its inputs as indices. In the first case assign its value to another variable if you want to keep that value then clear the variable. In the second case, rename or remove your version of real so MATLAB can use its real function.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!