Using Polynomial equations in Python
In this Python polynomial equation tutorial, you'll learn how to model and animate a moving object along a cubic curve using mathematical formulas. This hands-on project demonstrates how to solve polynomial equations and translate them into visual motion-ideal for senior secondary students learning both math and coding.
Understanding Polynomial and Cubic Equations | Maths Explanation for Python Kids
                        A polynomial equation expresses a relationship involving powers of a variable. 
                        For a cubic curve, the general form is:
                        y = ax3 + bx2 + cx + d;
                        Here, a, b, c, and d are constants. Every third-degree polynomial equation 
                        has both a maximum and a minimum point. 
                        These turning points are useful in generating smooth motion when graphing or animating curves with Python.
                    
Deriving the Equation of a Cubic Curve | Maths Explanation for Python Kids
To generate a cubic equation, all we will need are the maximum and minimum points of the curve.
                        y = ax3 + bx2 + cx + d  ----- (eqn 0)
                    
By differentiating y = ax³ + bx² + cx + d, we get dy/dx = 3ax² + 2bx + c. Setting the derivative equal to zero at both the maximum and minimum points allows us to calculate a, b, c, and d.
                            dy/dx = yI = 3ax2 + 2bx + c
                            At maximum point, yI = 0
                            yI|(x = xmax) = 0
                            3axmax2 + 2bxmax + c = 0  ----- (eqn 1)
                            At minimum point, yI = 0
                            yI|(x = xmin) = 0  ----- (eqn 2)
                            3axmin2 + 2bxmin + c = 0
                            Subtracting both derived equations
                            yI|(x = xmax) -
                            yI|(x = xmin)
                            ⇒
                            3a(xmax2 - xmin2) 
                            + 2b(xmax - xmin) = 0
                            2b(xmax - xmin) = 
                            -3a(xmax2 - xmin2)
                        
| b = | -3a(xmax - xmin)(xmax + xmin) | 
| 2(xmax - xmin) | 
                            b = -3/2a(xmax + xmin)
                            
                            Substituting b in (eqn 1)
                            3axmax2 + 2bxmax + c = 0
                            3axmax2 + 
                            2(-3a/2)(xmax + xmin)xmax 
                            + c = 0
                            3axmax2 - 
                            3axmax(xmax + xmin)
                            + c = 0
                            3axmax2 - 3axmax2
                            - 3axmaxxmin + c = 0
                            c = 3axmaxxmin
                            
                            From the general equation(eqn 0)
                            y = ax3 + bx2 + cx + d
                            ymax = axmax3 + 
                            bxmax2 + cxmax + d
                            
                            Substituting for b & c
                            ⇒ ymax = axmax3 - 
                            3/2a(xmax + xmin)xmax2
                            + 3axmaxxminxmax + d
                            
                            ymax = axmax3 - 
                            3/2axmax3 - 
                            3/2axmax2xmin
                            + 3axmax2xmin + d
                            
                            ymax = 1/2[2axmax3
                            - 3axmax3 - 3axmax2xmin
                            + 6axmax2xmin + 2d]
                            
                            ymax = 1/2[
                            -axmax3 +
                            3axmax2xmin + 2d]
                            
                            2ymax = -a(xmax - 
                            3axmin)xmax2 + 2d
                            
                            2d = 2ymax + a(xmax - 
                            3axmin)xmax2 
                            
                            d = ymax + a/2(xmax - 
                                3axmin)xmax2 
                            
                            
                            
                            From the general equation(eqn 0)
                            y = ax3 + bx2 + cx + d
                            
                            ymax = axmax3 + 
                            bxmax2 + cxmax + d
                            
                            ymin = axmin3 + 
                            bxmin2 + cxmin + d
                            
                            Subtracting both derived equations
                            ymax - ymin =
                            a(xmax3 - xmin3)
                            + b(xmax2 - xmin2)
                            + c(xmax - xmin)
                            
                            ymax - ymin = 
                            (xmax - xmin)[a(xmax2 
                            + xmaxxmin + xmin2)
                            + b(xmax + xmin) + c]
                            
                            Substituting for b & c
                            
                            ymax - ymin = 
                            (xmax - xmin)[a(xmax2 
                            + xmaxxmin + xmin2)
                            - 3a/2(xmax + xmin)2
                            + 3axmaxxmin]
                            
                            ymax - ymin = 
                            a(xmax - xmin)[xmax2 
                            + xmaxxmin + xmin2
                            - 3/2(xmax2 + 
                            2xmaxxmin + xmin2)
                            + 3xmaxxmin]
                            
                            2(ymax - ymin) = 
                            a(xmax - xmin)[2xmax2 
                            + 2xmaxxmin + 2xmin2
                            - 3(xmax2 + 2xmaxxmin 
                            + xmin2) + 6xmaxxmin]
                            
                            2(ymax - ymin) = 
                            a(xmax - xmin)(2xmax2 
                            + 2xmaxxmin + 2xmin2
                            - 3xmax2 - 6xmaxxmin 
                            - 6xmin2 + 6xmaxxmin)
                            
                            2(ymax - ymin) = 
                            a(xmax - xmin)(-xmax2 
                            + 2xmaxxmin - xmin2)
                            
                            2(ymax - ymin) = 
                            -a(xmax - xmin)(xmax2 
                            - 2xmaxxmin + xmin2)
                            
                            2(ymax - ymin) = 
                            -a(xmax - xmin)(xmax 
                            -  xmin)2
                            
                            2(ymax - ymin) = 
                            -a(xmax - xmin)3
                            
                        
| a = | -2(ymax - ymin) | 
| (xmax - xmin)3 | 
                        b = -3/2a(xmax + xmin)
                        
                        c = 3axmaxxmin
                        
        &
                        d = ymax + a/2(xmax - 
                            3axmin)xmax2
                    
These formulas form the mathematical basis of our Python polynomial solver.
Generating and Animating along a Cubic Polynomial Curve in Python
Once we determine the constants, we can implement a Python cubic equation solver to animate motion along the curve. The following example shows how to code a polynomial equation in Python using simple variables and Turtle canvas graphics.
To animate an object along a polynomial curve, increment x continuously and compute its corresponding y value using the cubic polynomial equation.
This Python code allows you to visualize the trajectory of a polynomial equation by plotting the curve dynamically on a Turtle canvas. The roots of the polynomial equation and the coefficients determine the shape and symmetry of the curve.
                        Create 2 new Python files; File, New File.
                        Call them Facet.py and CubicPath.py.
                        Type out the adjoining Python / Turtle code for animating an image body through
                        the path of a cubic / polynomial curve.
                    
Important: When trying to click on the button to get things started, you might need to click away from the button text.
Key Takeaways on Cubic Path Animation in Python
In this tutorial, you learned how to:
- Understand and derive cubic polynomial equations
 - Find coefficients from maximum and minimum points
 - Implement a polynomial equation solver using Python
 - Animate an object along a polynomial curve
 
By combining algebraic reasoning with code, senior secondary students can see how mathematics powers real-world applications like animation, computer graphics, and game design.
Applications of Polynomial Equations Python Programming and STEM Education
Polynomial equations are used in:
- Data modeling and curve fitting
 - Graphics programming for drawing smooth curves
 - Physics simulations and motion paths
 - Machine learning and optimization problems
 
Learning how to solve polynomial equations in Python provides a strong foundation for both mathematics and computational thinking.
Summary: Visualizing Polynomial Equations in Python
Polynomial equations are powerful tools for generating smooth, curved motion in graphics and animations. In this tutorial, you've learnt how to solve polynomial equations in Python, understand the mathematics of cubic curves, and create a simple animation that moves an image body along a polynomial equation path.
This interactive Python polynomial solver visually demonstrates how mathematical equations can be represented as real motion on a graph. It's a simple yet powerful example of combining coding and mathematics for educational purposes.
So! Python Fun Practice Exercise - Animate along Cubic Path
                        As a fun practice exercise, try modifying the values of xmax, xmin, ymax, 
                        and ymin to observe how they affect the polynomial equation graph. You can also:
                    
- Write a function to calculate the roots of the polynomial.
 - Compare your results with a quadratic equation solver.
 - Build a reusable polynomial equation solver in Python.
 
Python Cubic Path Code for Turtle Template - Facet Class
class Template:
def __init__(self):
screen = turtle.getscreen()
screen.title("usingMaths.com")
screen.setup(width=0.9, height=0.9, startx=None, starty=None)
screen.colormode(255)
self.bgcolour = "#cccccc"
screen.bgcolor(self.bgcolour)
screen.delay(0)
self.screen = screen
self.wnd_width = self.screen.window_width()
self.wnd_height = self.screen.window_height()
def controlButtons(self):
self.button = turtle.clone()
turtle.speed(0)
turtle.penup()
turtle.setposition(-self.wnd_width/2, self.wnd_height/2)
turtle.color("black", "pink")
turtle.pendown()
# draw button region
turtle.begin_fill()
turtle.forward(self.wnd_width)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(self.wnd_width)
turtle.right(90)
turtle.forward(50)
turtle.end_fill()
self.button.speed(0)
self.button.penup()
# draw button
self.button.setposition(0, self.wnd_height/2 - 25)
self.button.shape("square")
self.button.resizemode("user")
self.button.shapesize(2, 8)
self.button.color("black", (255, 0, 255))
return turtle.getturtle()
Python Animation Code for Cubic Path File
import math
from Facet import Template
def prep():
global x_max, y_max, x_min, y_min, x_dot, y_dot, a, b, c, d, x_start, x_stop, restart
restart = False
# bind fun function
scene.button.onrelease(play)
screen.delay(20)
turtle.penup()
# button text
turtle.setposition(scene.button.xcor(), scene.button.ycor()-10)
turtle.pendown()
turtle.write("Move", align="center", font=("Arial",16,"bold"))
# dot parameters
diameter = 1
dot_colour = "#ffff00"
x_max = 20 - scene.wnd_width/4; y_max = scene.wnd_height/3
x_min = scene.wnd_width/4 - 20; y_min = -scene.wnd_height/3
# constants
a = -2*(y_max - y_min) / math.pow((x_max - x_min), 3)
b = -(3/2)*a*(x_max + x_min)
c = 3*a*x_max*x_min
d = y_max + (a/2)*(x_max - 3*x_min)*math.pow(x_max, 2)
x_start = 2*x_max; x_stop = -x_start
x_dot = x_start
y_dot = a*math.pow(x_dot, 3) + b*math.pow(x_dot, 2) + c*x_dot + d
# transform turtle into a dot
turtle.penup()
turtle.setposition(x_dot, y_dot)
turtle.setheading(0)
turtle.shape("arrow")
turtle.shapesize(diameter, diameter)
turtle.color(dot_colour, dot_colour)
# fun function when button is clicked
# just moves turtle until it hits the right boundary
def play(x, y):
global x_max, y_max, x_min, y_min, x_dot, y_dot, a, b, c, d, x_start, x_stop, restart
if restart:
turtle.clearstamps()
restart = False
if x_dot <= x_stop:
y_dot = a*math.pow(x_dot, 3) + b*math.pow(x_dot, 2) + c*x_dot + d
# change dot's coordinates
turtle.setposition(x_dot, y_dot)
turtle.stamp()
x_dot += 20
screen.ontimer(play(0,0), 500)
else:
x_dot = x_start
y_dot = a*math.pow(x_dot, 3) + b*math.pow(x_dot, 2) + c*x_dot + d
restart = True
screen.mainloop()
# Let fly
scene = Template();
turtle = scene.controlButtons()
screen = scene.screen
prep()