Removing Complex Roots and Solutions

8 visualizaciones (últimos 30 días)
john barnes
john barnes el 17 de Abr. de 2015
Comentada: Star Strider el 17 de Abr. de 2015
I do not know how to remove complex roots from an equation (or even if it is possible.) The problem reads, "The product of three integers with spacing of 5 between them (e.g., 9, 14, 19) is 10,098. Using Matlab's built-in function for operations with polynomials, determine the three integers."
The three integers are 17, 22, and 27. My prof wants us to use the conv() and roots() command. He also said "we are ONLY concerned with real integer solutions." I would imagine he wants the output to be 17, 22 and 27 only. I need a way to either remove the complex roots, or tweak my code to only give the three real integer solutions. Below is my code. Below that are the solutions given my Matlab. Any help would be great.
CODE:
a=[1 0] b=[1 5] c=[1 10]
p=conv(a,b)
f=conv(p,c)
d=f+[0 0 0 -10098]
r=roots(d) %first root
r+5 %second root
r+10 %third root
MATLAB OUTPUT:
r =
-16.0000 +18.3848i
-16.0000 -18.3848i
17.0000 + 0.0000i
ans =
-11.0000 +18.3848i
-11.0000 -18.3848i
22.0000 + 0.0000i
ans =
-6.0000 +18.3848i
-6.0000 -18.3848i
27.0000 + 0.0000i

Respuesta aceptada

Star Strider
Star Strider el 17 de Abr. de 2015
You’re almost there, so I’ll show you how I would find the other integers:
r = roots(d); % Find Roots
r(1) = r(imag(r)==0); % Isolate Real Root
q = r(1)*(r(1)+5)*(r(1)+10) - 10098; % Test Product
if q < 1E-8
r(2) = r(1)+5;
r(3) = r(2)+5;
end
fprintf(1, '\n\tThe integers are: %3d, %3d, %3d\n',fix(r))
The ‘Test Product’ assignment and following if block are to be certain the first root is the lowest of the series. It isn’t necessary, but unless you already know that 17 is the lowest value, you would then have to iteratively test to see if it is the highest, middle, or lowest value.
The fix call in fprintf is necessary to eliminate the small residual inaccuracies that are the inevitable result of floating point operations.
  2 comentarios
john barnes
john barnes el 17 de Abr. de 2015
Thank you. Great explanation.
Star Strider
Star Strider el 17 de Abr. de 2015
My pleasure. Thank you.
Expanding on that, the exact test, ‘imag(r) == 0’, worked here, but in practice, it is best to use a tolerance, such as I used in the if block. It’s best not to entirely trust floating-point calculations, because decimal-to-binary conversion and representation is not always exact, the same reason I used fix in fprintf. I thought I’d mention that while I’m thinking of it.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 17 de Abr. de 2015
Use imag(r) to find out when the imaginary part is 0 or less than some very small number like 1E-6 or something.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by