Turn imaginary numbers to complex
11 views (last 30 days)
Show older comments
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?
0 Comments
Answers (3)
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.
0 Comments
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.
0 Comments
Adam Holland
on 1 Apr 2016
Edited: Walter Roberson
on 1 Apr 2016
1 Comment
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.
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!