HP Forums
Program for finding roots and selecting one - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Program for finding roots and selecting one (/thread-5842.html)



Program for finding roots and selecting one - Eugenio - 03-09-2016 01:40 AM

I am making a program an at some point i need the program to:

1) Find the roots of a cubic polinomi
2) The roots are always going to be 2 complex and 1 real ( cause of the physics behind). And i want the program to select the real one and define it as "Z"
3) Now that Z is define as one number ( the real root out of the three possible) it can continuo with my calculations.

I have the doubt that if I calculate the roots through Cas.function('expresion'), the three routs will come out as a vector, list, matrix or what?


RE: Program for finding roots and selecting one - SlideRule - 03-09-2016 02:43 AM

Is this a PRIME specific question? otherwise, HP calculator programs for the solution of cubic equations go back to at least the HP-65 with program #7 in MATH PAC 1.

Kahan has two source locations on the topic:
a. To Solve a Real Cubic Equation
found at Cubic
b. To Solve A Real Cubic Equation
found at AD a206859

as well as Analytic Solution of Quartic & Cubic Polynomials by A J Helou, BCE, M.Sc., Ph.D.

BEST!
SlideRule


RE: Program for finding roots and selecting one - Eugenio - 03-09-2016 04:26 AM

(03-09-2016 02:43 AM)SlideRule Wrote:  Is this a PRIME specific question? otherwise, HP calculator programs for the solution of cubic equations go back to at least the HP-65 with program #7 in MATH PAC 1.

Kahan has two source locations on the topic:
a. To Solve a Real Cubic Equation
found at Cubic
b. To Solve A Real Cubic Equation
found at AD a206859

as well as Analytic Solution of Quartic & Cubic Polynomials by A J Helou, BCE, M.Sc., Ph.D.

BEST!
SlideRule

Thank you very much, and yes, its for the HP PRIME.

I was thinking to use the CAS.function('expression') to find the roots instead of programming a cubic root finder. My real problem is which command should I use in the programm so that when the CAS.function gives the 3 roots, the programm choses the only real root out of the 3 roots ( 2 complex and 1 real.) and then define that real root as the valua of "Z" so it follows its calculations.


RE: Program for finding roots and selecting one - Didier Lachieze - 03-09-2016 08:38 AM

(03-09-2016 04:26 AM)Eugenio Wrote:  I was thinking to use the CAS.function('expression') to find the roots instead of programming a cubic root finder. My real problem is which command should I use in the programm so that when the CAS.function gives the 3 roots, the programm choses the only real root out of the 3 roots ( 2 complex and 1 real.) and then define that real root as the valua of "Z" so it follows its calculations.

This should work for you:
Code:
EXPORT RealRoot(poly)
BEGIN
  LOCAL s;
  s:=CAS.proot(poly);
  EXECON("IFTE(TYPE(&1),0,Z:=&1)",mat2list(s));
  RETURN Z;
END;
For example for $$2*x^3+x^2-2$$ RealRoot([2,1,0,-2]) returns 0.858094329497


RE: Program for finding roots and selecting one - Eugenio - 03-10-2016 04:57 AM

(03-09-2016 08:38 AM)Didier Lachieze Wrote:  
(03-09-2016 04:26 AM)Eugenio Wrote:  I was thinking to use the CAS.function('expression') to find the roots instead of programming a cubic root finder. My real problem is which command should I use in the programm so that when the CAS.function gives the 3 roots, the programm choses the only real root out of the 3 roots ( 2 complex and 1 real.) and then define that real root as the valua of "Z" so it follows its calculations.

This should work for you:
Code:
EXPORT RealRoot(poly)
BEGIN
  LOCAL s;
  s:=CAS.proot(poly);
  EXECON("IFTE(TYPE(&1),0,Z:=&1)",mat2list(s));
  RETURN Z;
END;
For example for $$2*x^3+x^2-2$$ RealRoot([2,1,0,-2]) returns 0.858094329497

Thank you so much for your help, it has been really helpfull. It works and suits perfectly