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

3 ビュー (過去 30 日間)
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.

回答 (2 件)

Honglei Chen
Honglei Chen 2013 年 2 月 5 日
編集済み: Honglei Chen 2013 年 2 月 6 日
try
Sol = R(~imag(R))
and see if it is what you want.
  2 件のコメント
Amir Khan
Amir Khan 2013 年 2 月 5 日
Thanks Chen, but it did not work
Honglei Chen
Honglei Chen 2013 年 2 月 6 日
編集済み: Honglei Chen 2013 年 2 月 6 日
My bad, isreal doesn't do element-wise operation, try the following instead. I've also updated the answer
Sol = R(~imag(R))

サインインしてコメントする。


Mahdi Saki
Mahdi Saki 2020 年 9 月 30 日
Sol(any(imag(Sol)~=0,2),:) = [];

カテゴリ

Help Center および File ExchangeArduino Hardware についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by