How to obtain a short-cut algorithm to compute higher degree polynomials?

Y
YUSUPHA JUWARA (450 points)
0 6 10
asked Oct 10, 2020 in Programming in Python by YUSUPHA JUWARA (450 points)
edited Oct 10, 2020 by andrea.sterbini

Hello Everyone,

I am really sorry, if I don't know yet what a "PSEUDO-CODE" is! It's my first semester, I am not acquinted with those TERMs yet. So, forwarding here my entire code in order to find a better algorithm, would be better since I am not acquinted with pseudo-code yet. The reason why I forward the code is to find a better algorithm than mine of how to compute polynomials; because, as you would see in the forwarded code below, I have spent a lot of time trying to figure out how to  get a short-cut algorithm to the last exercise(5), that is, polynomials of N-DEGREE (eg. power 5 or higher). @spognardi, @di_ciccio

The othe exercises were pretty easy, except the polynomial of 2nd-degree (it even get harder, when I try to compute the solution of higher degrees). The code is below:

"""1). Write a python script that takes in input a float number,
calculates its cube root and prints it on screen"""

def cube_root(c):
    d = c ** (1/3) # d is the cube root of c
    print(d, 'is the cube root of', c,': for exercise 1')
    
cube_root(64)
cube_root(27)
cube_root(8)
    
    
    
# 2). Write a python script that takes a person name (name)
# and the name of a fruit (fruit) and prints: "(name) loves to eat (fruit)

def eater(name, fruit):
    print(name, 'loves to eat', fruit, ': for exercise 2')
    
eater('YUSUPHA', 'MANGO')
eater('MATTEO', 'MELA')
eater('ELLA', 'BANANA')

    
# 3). Write a python script that takes in input two floats a and b and
# calculates and prints c, corresponding to the hypotenuse of the rectangle
# triangle having for cathets two sides of length a and b, respectively

def hypotenuse(a, b):
    c = (a ** 2 + b ** 2)
    c = c ** (1/2)
    print('the hypotenuse of a triangle of sides', a, ', ', b, 'is', c, ': for exercise 3')
    
hypotenuse(4, 5)
hypotenuse(3, 4)
hypotenuse(12, 5)

#4). Write a python script that takes in input three floats a, b, c,
# and calculates and prints the expression:
    
def expression(a, b, c):
    d = (b**3 * c**4) / (3 * a)
    y = a**2 + d -b + a * c # y is asighned with the original expression
    print(y, 'is the answer to the above expression: exercise 4')
    
expression(9, 5, 2)
expression(89, 1, 5)
expression(2, 4, 8)

#5). Write a python script that takes three floats a, b, c, and calculates
# and prints the two x roots of the quadratic equation:
    
def roots_of_quad(a, b, c):
    d = b**2 - 4 * a * c
    d = d ** (1/2)
    x1 = (-b + d) / (2 * a) # first or the positive(+) root
    x2 = (-b - d) / (2 * a) # second or the negative(-) root
    print(x1, 'is the positive root: for exercise 5')
    print(x2, 'is the negative root: for exercise 5')
    
roots_of_quad(1, 2, 1)
roots_of_quad(2, 5, 3)
roots_of_quad(4, -10, 6)
3.9999999999999996 is the cube root of 64 : for exercise 1
3.0 is the cube root of 27 : for exercise 1
2.0 is the cube root of 8 : for exercise 1
YUSUPHA loves to eat MANGO : for exercise 2
MATTEO loves to eat MELA : for exercise 2
ELLA loves to eat BANANA : for exercise 2
the hypotenuse of a triangle of sides 4 ,  5 is 6.4031242374328485 : for exercise 3
the hypotenuse of a triangle of sides 3 ,  4 is 5.0 : for exercise 3
the hypotenuse of a triangle of sides 12 ,  5 is 13.0 : for exercise 3
168.07407407407408 is the answer to the above expression: exercise 4
8367.340823970037 is the answer to the above expression: exercise 4
43706.666666666664 is the answer to the above expression: exercise 4
-1.0 is the positive root: for exercise 5
-1.0 is the negative root: for exercise 5
-1.0 is the positive root: for exercise 5
-1.5 is the negative root: for exercise 5
1.5 is the positive root: for exercise 5
1.0 is the negative root: for exercise 5

283 views

3 Answers

andrea.sterbini (172780 points)
514 935 1789
answered Oct 10, 2020 by andrea.sterbini (172,780 points)

PLEASE do not write code on Q2A, this is forbidden. (I have taken the liberty to hide it)
Pseudocode is a plain text description of what the algorithm/program does (see https://en.wikipedia.org/wiki/Pseudocode)
(always remember, Google and Wikipedia are your friends  laugh )

B
Barbara (270 points)
0 0 2
answered Oct 10, 2020 by Barbara (270 points)
edited Oct 11, 2020 by Barbara
Dear Yusupha,

I hope that writing you in "natural language" won't break any rule.

If you're refering to the exercise "Write a python script that takes three floats a, b, c, and calculates and prints the two x roots of the equation ax^2+bx+c=0",

read below, otherwise ignore this reply.

You can use the quadratic formula (if you need to review the quadratic formula I suggest you to watch this video on Khan Academy https://www.khanacademy.org/math/algebra/x2f8bb11595b61c86:quadratic-functions-equations/x2f8bb11595b61c86:quadratic-formula-a1/v/using-the-quadratic-formula );

if you want to solve higher degree polynomials, there is a formula for the third degree and the forth degree ones, about the fifth degree...you should do some reasearch on the web. I don't think one formula could work for polynomials of different degrees (one size fits all doesn't apply to math).

The aforesaid excercise, if I correctly understood it, just asked to use: 'a, b, c' as variables of your second degree polynomial and calculate the 2 x solutions with the quadratic formula. a, b, c are the COEFFICIENTS of the X not the EXPONENTIALS.

For these problems, I believe that low tech tools, like pencil and paper, are enough.

Have a nice week end.

Barbara
d
davide.marincione (1380 points)
0 4 14
commented Oct 10, 2020 by davide.marincione (1,380 points)

I think she misread and thought we had to write a generalized function that could solve any polynomial; that said there is a "brute-force" way to find the solutions to any polynomial of any degree- the Newton's (or Newton-Raphson) method, which is the method that many calculators use to find the zeroes of a function.

F
Fran.Cie. (210 points)
0 0 1
answered Oct 12, 2020 by Fran.Cie. (210 points)
Hi Yusupha,

As far as finding a roots of a polynomial is concerned, there is an algorithm called "bisection method" that can do that (not always).

It takes advantage of a property of continuous function:

If a function is continuous AND there are some two args(say: x1 and x2), which values are of the opposite sign (one is >0, second is <0), then a function has at least one root in between two arguments x1 and x2.

Bisection method algorithm takes two values as an input, which are the beginnig and the end of a range consisting a root.

It can be implemented both iteratively and recursively.

In every iteration it finds an argument that lies exaclty in the center of the searched range, and depending on the sign of a value of the center argument, one of the halves of a searched range is rejected, and the process repeats in the next iteration, but on a smaller range,

and so on, and so on... it could do it endlessly,

so you have to add some condition that end the process and gives an ultimate approximation as an output. And that would be the third input value of an algorithm that indicates the minimal size of a range.

The problem is, when the domain is infinite, you can be unable to choose the right borders of a range.

If you have a polynomial of a high grade, once you find one root, you can use the root to divide polynomial by a root, so that you obtain a new polynomial but of a grade diminished by 1,

Then you take a new polynomial, you find the next root, and so on.

I hope I helped at least a little.
Y
YUSUPHA JUWARA (450 points)
0 6 10
commented Oct 13, 2020 by YUSUPHA JUWARA (450 points)

hello Fran.Cie,

Well, this is a better responce to the question, and at the same time beyong my level. We haven't started ITERATION or RECURSIVE FUNCTIONS yet, as I am in my first Semester. However, I understand the explanation, and the polynomial division part. Unfortunately, I'll have to search for what you mean by "BISECTION"; but, if it means obtaining the farmula for quadratic equations, then I would say I know that ( as explained by @Barbara  in the ealier comment). I think, I might be able to implement your good answer only after having done ITERATIONs and RECURSIVE functions(but, I thoroughly understand the concepts). Thanks for the wonderfull reply!