Post Reply 
Program for finding roots and selecting one
03-09-2016, 01:40 AM
Post: #1
Program for finding roots and selecting one
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?
Find all posts by this user
Quote this message in a reply
03-09-2016, 02:43 AM
Post: #2
RE: Program for finding roots and selecting one
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
Find all posts by this user
Quote this message in a reply
03-09-2016, 04:26 AM
Post: #3
RE: Program for finding roots and selecting one
(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.
Find all posts by this user
Quote this message in a reply
03-09-2016, 08:38 AM
Post: #4
RE: Program for finding roots and selecting one
(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
Find all posts by this user
Quote this message in a reply
03-10-2016, 04:57 AM
Post: #5
RE: Program for finding roots and selecting one
(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
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)