Sorting an array for real only solutions or getting rid of complex results using embedded matlab function
3 views (last 30 days)
Show older comments
Hi,
I am using embedded matlab function to find roots of a quartic equation. Following is the code that Im using
* function Sol=Quartic(C1, C2, C3, C4, C5)
* R=roots([C1 C2 C3 C4 C5]);
* z=zeros(1,4);
* Sol=zeros(1,4);
* z=real(R);
* Sol(:)=z(:);
This Sol is an array of n rows by 4 columns. Two of the columns contains the pure real solutions and the other two contains the real part of the complex solutions. All the input coefficients (C1 to C4) are real for my simulation, which means that the solution should contain two pure real and one conjugate pair of complex results (having the same real parts but different imaginary parts).
The problem is that the Sol array, sorts the real parts of the conjugate complex results randomly. For example, for these four solutions, -1.1314 0.0095 0.8816 0.0095, the first and 3rd colum are the pure real solutions but they are not arranged according to decreasing or increasing value. Sometimes for a certain set of quartic coefficients, the first and fourth column contain the pure real, and the 2nd and 3rd column contains the real part of the complex conjugate results.
Ideally, I would like to totally remove the the complex results from the roots, but I couldn't do it being novice at programming. The second thing that I thought is to force the pure real solutions to always appear in the first two columns. This could solve my problem in getting rid of the other ones. But I cant think of any way to do it. Can somebody help me with a code to do it please? I'm stuck badly at this. Will really appreciate any help.
0 Comments
Answers (2)
Honglei Chen
on 5 Feb 2013
Edited: Honglei Chen
on 6 Feb 2013
try
Sol = R(~imag(R))
and see if it is what you want.
2 Comments
Honglei Chen
on 6 Feb 2013
Edited: Honglei Chen
on 6 Feb 2013
My bad, isreal doesn't do element-wise operation, try the following instead. I've also updated the answer
Sol = R(~imag(R))
See Also
Categories
Find more on Arduino Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!