Extra value in my work

4 visualizaciones (últimos 30 días)
Yogesh Bhambhwani
Yogesh Bhambhwani el 17 de Dic. de 2020
Comentada: Dyuman Joshi el 20 de Dic. de 2020
function MyRoots = MyCubicRoot(a_3,a_2,a_1,a_0)
a = a_2/a_3;
b = a_1/a_3;
c = a_0/a_3;
Q = (a^2 - 3*b)/9;
R = (2*a^3 - 9*a*b +27*c)/54;
d = R^2 - Q^3;
if d < 0
t = acos(R/sqrt(Q^3));
x_1 = -(a/3)-2*sqrt(Q)*cos(t/3);
x_2 = -(a/3)-2*sqrt(Q)*cos((t+2*pi)/3);
x_3 = -(a/3)-2*sqrt(Q)*cos((t+4*pi)/3);
Order = [x_1,x_2,x_3];
MyRoots = sort(Order,'ascend');
disp('The polynomial 1x^3-6x^2+11x-6 has three real roots given by')
disp(x_1)
disp(x_2)
disp(x_3)
elseif abs(d) < 1e-6
x_1 = -(a/3) + 2*nthroot(-R,3);
x_2 = -(a/3) - nthroot(-R,3);
MyRoots = sort(x_1,x_2);
disp('The polynomial 2x^3+9.8x^2-4.9x-51.45has distinct real roots given by')
disp(x_1)
disp(x_2)
else
x_1 = -(a/3)+nthroot(-R+sqrt(d),3)+nthroot(-R-sqrt(d),3);
MyRoots = x_1;
disp('The polynomial x^3-7.8693x^2+13.377x-6.5354 has only real root given by')
disp(x_1)
end
end
The polynomial 1x^3-6x^2+11x-6 has three real roots given by
1
3
2
myRoots =
1 2 3
The polynomial 1x^3-6x^2+11x-6 has three real roots given by
-3.5000
2.1000
-3.5000
myRoots =
-3.5000 -3.5000 2.1000
The polynomial x^3-7.8693x^2+13.377x-6.5354 has only real root given by
5.7357
myRoots =
5.7357
I get an extra -3.5 how do I get rid of it
  5 comentarios
Yogesh Bhambhwani
Yogesh Bhambhwani el 17 de Dic. de 2020
I tried what you said and it didnt work
Dyuman Joshi
Dyuman Joshi el 20 de Dic. de 2020
Use this unique(roots(input))

Iniciar sesión para comentar.

Respuestas (1)

Jemima Pulipati
Jemima Pulipati el 19 de Dic. de 2020
Hello,
From my understanding, x_1 and x_3 are not exactly the same values.
You can verify this by changing the preferences of how the numeric format is displayed. Go to Preferences -> Command Window -> Set the 'Numeric Format' under 'Text Display' to 'long e'. Now if you try to print those values it does not show them to be the same. Since they have a slight difference, they are treated as different numbers and unique() is displaying all the three numbers in the answer.
If you still do not want to consider that minor difference and remove one of them, you may consider using uniquetol(). This searches for unique values within a given tolerance value.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by