Using Polynomial equations in VB.Net
In this VB.Net 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 VB.Net 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 VB.Net.
Deriving the Equation of a Cubic Curve | Maths Explanation for VB.Net 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 VB.Net polynomial solver.
Generating and Animating along a Cubic Polynomial Curve in VB.Net
Once we determine the constants, we can implement a VB.Net cubic equation solver to animate motion along the curve. The following example shows how to code a polynomial equation in VB.Net using simple variables and VB.Net 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 VB.Net code allows you to visualize the trajectory of a polynomial equation by plotting the curve dynamically on a VB.Net windows form. The roots of the polynomial equation and the coefficients determine the shape and symmetry of the curve.
Create a new Visual Basic Windows Forms Application
project
;
call it Dymetric_VB.
Create 3 new VB.Net classes;
Call them Facet, Dymetric and CubicPath.
Type out the adjoining VB.Net code for animating an image body through
the path of a cubic / polynomial curve.
Key Takeaways on Cubic Path Animation in VB.Net
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 VB.Net
- 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 VB.Net 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 VB.Net provides a strong foundation for both mathematics and computational thinking.
Summary: Visualizing Polynomial Equations in VB.Net
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 VB.Net, understand the mathematics of cubic curves, and create a simple animation that moves an image body along a polynomial equation path.
This interactive VB.Net 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! VB.Net 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 VB.Net.
VB.Net Cubic Path Window Display Code Stub
Private form_details As New Facet
Private action_class As New Dymetric
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Fill in Form - Put button on form
form_details.formFeatures(sender)
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
' Colour button area
form_details.decorateButtonArea(sender, e)
' Call MovingBody class into action
action_class.decideAction(sender, Me.CreateGraphics(), form_details.CLICK_OCCURRED)
' Reset click variable
form_details.CLICK_OCCURRED = False
End Sub
End Class
VB.Net Cubic Path Facet Window Code Stub
Dim screen_rect As Rectangle
Public CLICK_OCCURRED As Boolean = False
Public Sub formFeatures(sender As Object)
'Set window position, width and height
screen_rect = Screen.PrimaryScreen.Bounds
sender.SetDesktopBounds(0, 0, screen_rect.Width, screen_rect.Height)
' Set a display text
sender.Text = "useOfMaths.com"
' Set a background colour
sender.BackColor = System.Drawing.Color.LightGray
' Set an icon image
Dim path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
path = New Uri(path).LocalPath
Try
sender.Icon = New Icon(path & "\useOfMaths.ico")
Catch ex As System.IO.FileNotFoundException
' Well, just go on and use default pic
End Try
'
'create a button - response_btn
'
Dim response_btn As New Button()
response_btn.BackColor = System.Drawing.Color.Magenta
response_btn.ForeColor = System.Drawing.Color.Blue
response_btn.Name = "response_btn"
response_btn.SetBounds(CInt(Math.Round(screen_rect.Width / 2)) - 50, 5, 100, 40)
response_btn.Text = "Move"
sender.Controls.Add(response_btn)
AddHandler response_btn.Click, AddressOf response_btn_Click
End Sub
Public Sub decorateButtonArea(sender As Object, e As PaintEventArgs)
' Draw a dotted line
Dim pencil As New System.Drawing.Pen(System.Drawing.Color.Black)
pencil.DashStyle = Drawing2D.DashStyle.DashDot
pencil.Width = 5
e.Graphics.DrawLine(pencil, 0, 50, sender.Width, 50)
pencil.Dispose()
' Colour region
Dim paint_brush As New System.Drawing.SolidBrush(System.Drawing.Color.Pink)
e.Graphics.FillRectangle(paint_brush, 0, 0, sender.Width, 50)
paint_brush.Dispose()
End Sub
Public Sub response_btn_Click(sender As Object, e As EventArgs)
' turn this on on every button click
CLICK_OCCURRED = True
sender.Refresh()
End Sub
End Class
VB.Net Cubic Path Code for Dymetric Class
Private cube_curve As New CubicPath
Private do_simulation = False
' decide what course of action to take
Public Sub decideAction(sender As Object, g As Graphics, click_check As Boolean)
If do_simulation And click_check Then
' do animation
cube_curve.play(sender, g)
do_simulation = False
Else
' Put ball on screen
cube_curve.prep(sender, g)
do_simulation = True
End If
End Sub
End Class
VB.Net Animation Code for Cubic Path Class
Private x_max, y_max, x_min, y_min, x, y As Integer
Private a, b, c, d As Double
Private Const dotDIAMETER = 10
Dim dot_colour As New System.Drawing.SolidBrush(System.Drawing.Color.Yellow)
Dim bg_colour As New System.Drawing.SolidBrush(System.Drawing.Color.LightGray)
' draw first appearance of dot on the screen
Public Sub prep(sender As Object, g As Graphics)
x = 20
x_max = Math.Round(sender.Width / 4) + 10
y_max = 70
x_min = Math.Round(3 * sender.Width / 4) - 10
y_min = sender.Height - 70
' 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)
y = CInt(Math.Round(a * Math.Pow(x, 3) + b * Math.Pow(x, 2) + c * x + d))
' clear entire used canvas area
g.FillRectangle(bg_colour, 0, 60, sender.Width, sender.Height)
' draw dot
g.FillEllipse(dot_colour, x, y, dotDIAMETER, dotDIAMETER)
End Sub
' repetitively clear and draw dot on the screen - Simulate motion
Public Sub play(sender As Object, g As Graphics)
' condition for continuing motion
Do While x < sender.Width - dotDIAMETER And y >= y_max
' redraw dot
g.FillEllipse(dot_colour, x, y, dotDIAMETER, dotDIAMETER)
x += 20
y = CInt(Math.Round(a * Math.Pow(x, 3) + b * Math.Pow(x, 2) + c * x + d))
' take a time pause
Threading.Thread.Sleep(50)
Loop
End Sub
End Class