Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
identify real roots

Subject: identify real roots

From: Chris Sorrento

Date: 10 Jul, 2011 15:24:10

Message: 1 of 3

Hello,
I have very simple question (I think) but I am stuck. I have a polynomial that may have more than one real and complex roots. I then need to use only the real roots in further computations. The question I have is how do I extract only the real roots from the x = roots(polynomial) function? The code below constructs a matrix of zeros and real roots. But now I run into a problem of identifying the location of the all the real roots within this new matrix. Is there an easier way?

The following code solves polynomial of the form x^2 + x + constant = 0

pH = roots([1 (HCO30 + H) (HCO30 - CO2Produced)]);
NewpH = zeros(size(pH));
for k = 1:length(pH)
    if isreal(pH(k)) > 0
       NewpH(k) = pH(k);
    end
end

Thanks for you help. Chris.

Subject: identify real roots

From: Chris Sorrento

Date: 10 Jul, 2011 18:15:10

Message: 2 of 3

I found sort of found the answer. Use the 'find' function to locate the non zero entries in the matrix that results from isreal() test. Then use those indices to backtrack to the actual values...

Subject: identify real roots

From: Steven_Lord

Date: 11 Jul, 2011 01:23:36

Message: 3 of 3



"Chris Sorrento" <remove.this.string.agairan@hotmail.com> wrote in message
news:ivcg6p$7t0$1@newscl01ah.mathworks.com...
> Hello,
> I have very simple question (I think) but I am stuck. I have a polynomial
> that may have more than one real and complex roots. I then need to use
> only the real roots in further computations. The question I have is how do
> I extract only the real roots from the x = roots(polynomial) function? The
> code below constructs a matrix of zeros and real roots. But now I run into
> a problem of identifying the location of the all the real roots within
> this new matrix. Is there an easier way?

Select the roots for which the IMAGinary part is small.

> The following code solves polynomial of the form x^2 + x + constant = 0
>
> pH = roots([1 (HCO30 + H) (HCO30 - CO2Produced)]);
> NewpH = zeros(size(pH));

realroots = abs(imag(NewpH)) < tolerance;

for some value tolerance.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us