usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Solving Cubic Equations and Animating along Polynomial Curves in C# | Senior Secondary Mathematics



Using Polynomial equations in C#

In this C# 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 C# Kids

A polynomial equation expresses a relationship involving powers of a variable. For a cubic curve, the general form is: $ y = ax^3 + bx^2 + 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 C#.

Graph of cubic equation and polynomial curve in C#
Figure: Graph of cubic equation and polynomial curve in C#

Deriving the Equation of a Cubic Curve | Maths Explanation for C# Kids

To generate a cubic equation, all we will need are the maximum and minimum points of the curve.

\[ \begin{equation} y = ax^3 + bx^2 + cx + d \tag{0} \end{equation} \]

By differentiating \( y = ax^3 + bx^2 + cx + d \), we get $$ \frac{dy}{dx} = 3ax^2 + 2bx + c. $$ Setting the derivative equal to zero at both the maximum and minimum points allows us to calculate $a$, $b$, $c$, and $d$.

\[ \frac{dy}{dx} = y^I = 3ax^2 + 2bx + c \]

At maximum point, \( y^I = 0 \)

\[ y^I|_{(x = x_{max})} = 0 \] \[ \begin{equation} 3ax_{max}^2 + 2bx_{max} + c = 0 \end{equation} \]

At minimum point, \( y^I = 0 \)

\[ y^I|_{(x = x_{min})} = 0 \] \[ \begin{equation} 3ax_{min}^2 + 2bx_{min} + c = 0 \end{equation} \]

Subtracting both derived equations

\[ y^I|_{(x = x_{max})} - y^I|_{(x = x_{min})} \] \[ \begin{align*} \Rightarrow 3a(x_{max}^2 - x_{min}^2) + 2b(x_{max} - x_{min}) &= 0 \\ 2b(x_{max} - x_{min}) &= -3a(x_{max}^2 - x_{min}^2) \\ b &= \frac{-3a(x_{max} - x_{min})(x_{max} + x_{min})}{2(x_{max} - x_{min})} \\ b &= -\frac{3}{2}a(x_{max} + x_{min}) \end{align*} \]

Substituting $b$ in (eqn 1)

\[ \begin{align*} 3ax_{max}^2 + 2bx_{max} + c &= 0 \\ 3ax_{max}^2 + 2(\frac{-3a}{2})(x_{max} + x_{min})x_{max} + c &= 0 \\ 3ax_{max}^2 - 3ax_{max}(x_{max} + x_{min}) + c &= 0 \\ 3ax_{max}^2 - 3ax_{max}^2 - 3ax_{max}x_{min} + c &= 0 \\ c &= 3ax_{max}x_{min} \end{align*} \]

From the general equation(eqn 0)

\[ \begin{align*} y &= ax^3 + bx^2 + cx + d \\ y_{max} &= ax_{max}^3 + bx_{max}^2 + cx_{max} + d \end{align*} \]

Substituting for $b$ & $c$

\[ \begin{align*} \Rightarrow y_{max} &= ax_{max}^3 - \frac{3}{2}a(x_{max} + x_{min})x_{max}^2 + 3ax_{max}x_{min}x_{max} + d \\ y_{max} &= ax_{max}^3 - \frac{3}{2}ax_{max}^3 - \frac{3}{2}ax_{max}^2x_{min} + 3ax_{max}^2x_{min} + d \\ y_{max} &= \frac{1}{2}[2ax_{max}^3 - 3ax_{max}^3 - 3ax_{max}^2x_min + 6ax_{max}^2x_{min} + 2d] \\ y_{max} &= \frac{1}{2}[-ax_{max}^3 + 3ax_{max}^2x_{min} + 2d] \\ 2y_{max} &= -a(x_{max} - 3ax_{min})x_{max}^2 + 2d \\ 2d &= 2y_{max} + a(x_{max} - 3ax_{min})x_{max}^2 \\ d &= y_{max} + \frac{a}{2}(x_{max} - 3ax_{min})x_{max}^2 \end{align*} \]

From the general equation (eqn 0)

\[ \begin{align*} y &= ax^3 + bx^2 + cx + d \\ y_{max} &= ax_{max}^3 + bx_{max}^2 + cx_{max} + d \\ y_{min} &= ax_{min}^3 + bx_min^2 + cx_{min} + d \end{align*} \]

Subtracting both derived equations

\[ \begin{align*} y_{max} - y_{min} &= a(x_{max}^3 - x_{min}^3) + b(x_{max}^2 - x_{min}^2) + c(x_{max} - x_{min}) \\ y_{max} - y_{min} &= (x_{max} - x_{min})[a(x_{max}^2 + x_{max}x_{min} + x_{min}^2) + b(x_{max} + x_{min}) + c] \end{align*} \]

Substituting for $b$ & $c$

\[ \begin{align*} y_{max} - y_{min} &= (x_{max} - x_{min})[a(x_{max}^2 + x_{max}x_{min} + x_{min}^2) - \frac{3a}{2}(x_{max} + x_{min})^2 + 3ax_{max}x_{min}] \\ y_{max} - y_{min} &= a(x_{max} - x_{min})[x_{max}^2 + x_{max}x_{min} + x_{min}^2 - \frac{3}{2}(x_{max}^2 + 2x_{max}x_{min} + x_{min}^2) + 3x_{max}x_{min}] \\ 2(y_{max} - y_{min}) &= a(x_{max} - x_{min})[2x_{max}^2 + 2x_{max}x_{min} + 2x_{min}^2 - 3(x_{max}^2 + 2x_{max}x_{min} + x_{min}^2) + 6x_{max}x_{min}] \\ 2(y_{max} - y_{min}) &= a(x_{max} - x_{min})(2x_{max}^2 + 2x_{max}x_{min} + 2x_{min}^2 - 3x_{max}^2 - 6x_{max}x_{min} - 6x_{min}^2 + 6x_{max}x_{min}) \\ 2(y_{max} - y_{min}) &= a(x_{max} - x_{min})(-x_{max}^2 + 2x_{max}x_{min} - x_{min}^2) \\ 2(y_{max} - y_{min}) &= -a(x_{max} - x_{min})(x_{max}^2 - 2x_{max}x_{min} + x_{min}^2) \\ 2(y_{max} - y_{min}) &= -a(x_{max} - x_{min})(x_{max} - x_{min})^2 \\ 2(y_{max} - y_{min}) &= -a(x_{max} - x_{min})^3 \end{align*} \]

Hence:

\[ \begin{align*} a &= \frac{-2(y_{max} - y_{min})}{(x_{max} - x_{min})^3} \\ b &= -\frac{3}{2}a(x_{max} + x_{min}) \\ c &= 3ax_{max}x_{min} \\ \& \\ d &= y_{max} + \frac{a}{2}(x_{max} - 3ax_{min})x_{max}^2 \end{align*} \]

These formulas form the mathematical basis of our C# polynomial solver.


Generating and Animating along a Cubic Polynomial Curve in C#

Once we determine the constants, we can implement a C# cubic equation solver to animate motion along the curve. The following example shows how to code a polynomial equation in C# using simple variables and C# windows form graphics.

To animate an object along a polynomial curve, increment x continuously and compute its corresponding y value using the cubic polynomial equation.

This C# code allows you to visualize the trajectory of a polynomial equation by plotting the curve dynamically on a C# windows form. The roots of the polynomial equation and the coefficients determine the shape and symmetry of the curve.

Create a new C# Windows Forms Application project ; call it Dymetric_CS.
Create 2 new C# classes;
Call them Dymetric and CubicPath.
Type out the adjoining C# code for animating an image body through the path of a cubic / polynomial curve.


Why Use C# to Teach Polynomial Equations?

Combining coding with mathematics helps students:

  • Visualize complex algebraic concepts
  • Strengthen problem-solving skills
  • Understand function behavior more deeply
  • Connect classroom theory with practical programming

Using C# to solve and animate cubic polynomial equations encourages active exploration rather than passive memorization.

Applications of Polynomial Equations C# 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 C# provides a strong foundation for both mathematics and computational thinking.


Key Takeaways on Cubic Path Animation in C#

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 C#
  • 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.


Summary: Visualizing Polynomial Equations in C#

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 C#, understand the mathematics of cubic curves, and create a simple animation that moves an image body along a polynomial equation path.

This interactive C# 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! C# Fun Practice Exercise - Animate along Cubic Path

As a fun practice exercise, try modifying the values of $x_{max}$, $x_{min}$, $y_{max}$, and $y_{min}$ 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 C#.









C# Cubic Path Window Display Code Stub


C# Cubic Path Code for Dymetric Class

using System.Windows.Forms;

namespace Dymetric
{
    class Dymetric
    {
        private CubicPath cube_curve;
        private bool do_simulation;

        public Dymetric(int screen_width, int screen_height)
        {
            cube_curve = new CubicPath(screen_width, screen_height);
            do_simulation = false;
        }

        // decide what course of action to take
        public void decideAction(PaintEventArgs e, bool click_check)
        {
            if (do_simulation && click_check)
            {
                // do animation
                cube_curve.inPlay(e);
                do_simulation = false;
            }
            else
            {
                // Put ball on screen
                cube_curve.clearAndDraw(e);
                do_simulation = true;
            }
        }
    }
}

C# Animation Code for Cubic Path Class

using System;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;

namespace Dymetric
{
    class CubicPath
    {
        private int x_start, x_max, y_max, x_min, y_min, x, y;
        private double a, b, c, d;
        private const int dotDIAMETER = 10;

        // we'll be drawing to and from a bitmap image
        private Bitmap offscreen_bitmap;
        Graphics offscreen_g;

        private Brush dot_colour, bg_colour;

        public CubicPath(int screen_width, int screen_height)
        {
            dot_colour = new SolidBrush(Color.Yellow);
            bg_colour = new SolidBrush(Color.LightGray);

            // Create bitmap image
            offscreen_bitmap = new Bitmap(screen_width, screen_height - 55,
                System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            // point graphic object to bitmap image
            offscreen_g = Graphics.FromImage(offscreen_bitmap);

            // Set background of bitmap graphic
            offscreen_g.Clear(Color.LightGray);

            x_start = x = 20;
            x_max = offscreen_bitmap.Width / 4 + 10;
            y_max = 20;
            x_min = 3 * offscreen_bitmap.Width / 4 - 10;
            y_min = offscreen_bitmap.Height - 70;

            // constants
            a = (-2 * (y_max - y_min)) / Math.Pow((x_max - x_min), 3);
            b = -((double)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);

            y = (int)Math.Round(a * Math.Pow(x, 3) + b * Math.Pow(x, 2) + c * x + d);
        }

        // draw first appearance of dot on the screen
        public void clearAndDraw(PaintEventArgs e)
        {
            /*
             * draw to offscreen bitmap
             */

            // clear entire bitmap
            offscreen_g.Clear(Color.LightGray);
            // draw dot
            offscreen_g.FillEllipse(dot_colour, x, y, dotDIAMETER, dotDIAMETER);

            // draw to screen
            Graphics gr = e.Graphics;
            gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);
        }

        // repetitively clear and draw dot on the screen - Simulate motion
        public void inPlay(PaintEventArgs e)
        {
            Graphics gr = e.Graphics;
            // condition for continuing motion
            while (x < offscreen_bitmap.Width - dotDIAMETER && y >= y_max)
            {
                // redraw dot
                offscreen_g.FillEllipse(dot_colour, x, y, dotDIAMETER, dotDIAMETER);
                // draw to screen
                gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);

                x += 20;
                y = (int)Math.Round(a * Math.Pow(x, 3) + b * Math.Pow(x, 2) + c * x + d);
                // take a time pause
                Thread.Sleep(50);
            }
            x = x_start;
            y = (int)Math.Round(a * Math.Pow(x, 3) + b * Math.Pow(x, 2) + c * x + d);
        }
    }
}






<< PreviousNext >>