Borrar filtros
Borrar filtros

Sorting an array for real only solutions or getting rid of complex results using embedded matlab function

1 visualización (últimos 30 días)
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.

Respuestas (2)

Honglei Chen
Honglei Chen el 5 de Feb. de 2013
Editada: Honglei Chen el 6 de Feb. de 2013
try
Sol = R(~imag(R))
and see if it is what you want.

Mahdi Saki
Mahdi Saki el 30 de Sept. de 2020
Sol(any(imag(Sol)~=0,2),:) = [];

Categorías

Más información sobre Programmatic Model Editing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by