Visualize cubic functions with interactive demo and VB code examples
This program enables users to input the coefficients of a cubic function and visualize its graph. The cubic function takes the form: f(x) = ax3 + bx2 + cx + d.
In Visual Basic, we need to handle coordinate transformations since computer screen coordinates start from the top left. To center the graph, we use transformations like (5 - x) to start plotting from the center.
The graph is drawn using Visual Basic's built-in PSet
method, which plots individual points to form the curve.
Enter coefficients below to visualize the cubic function in real-time:
This VB6 implementation creates a cubic graph plotter with a simple interface:
Dim a, b, c, d As Integer Dim x As Double Private Function f(x As Variant) ' Transform coordinates to start from center f = a * (5 - x) ^ 3 + b * (5 - x) ^ 2 + c * (5 - x) + d End Function Private Sub cmd_Clear_Click() pic_graph.Cls txt_a.Text = "" txt_b.Text = "" txt_c.Text = "" txt_d.Text = "" End Sub Private Sub cmd_draw_Click() Dim y As Double Dim w As Double a = Val(txt_a.Text) b = Val(txt_b.Text) c = Val(txt_c.Text) d = Val(txt_d.Text) ' Using a scale of 0.5 cm to represent 1 unit ' Transform coordinates as VB starts from top left For w = 0 To 10 Step 0.0001 y = f(w) pic_graph.PSet (w, 5 - y) Next w End Sub
This modern VB.NET implementation uses GDI+ for more sophisticated graphing:
Private a, b, c, d As Double Private Function f(x As Double) As Double ' Calculate cubic function value Return a * Math.Pow(x, 3) + b * Math.Pow(x, 2) + c * x + d End Function Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click ' Parse input values If Not Double.TryParse(txtA.Text, a) OrElse Not Double.TryParse(txtB.Text, b) _ OrElse Not Double.TryParse(txtC.Text, c) OrElse Not Double.TryParse(txtD.Text, d) Then MessageBox.Show("Please enter valid numeric coefficients") Return End If ' Create bitmap and graphics object Dim bmp As New Bitmap(picGraph.Width, picGraph.Height) Using g As Graphics = Graphics.FromImage(bmp) g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias g.Clear(Color.White) ' Draw axes Dim penAxis As New Pen(Color.Black, 1) Dim centerX As Integer = picGraph.Width \ 2 Dim centerY As Integer = picGraph.Height \ 2 g.DrawLine(penAxis, 0, centerY, picGraph.Width, centerY) ' X-axis g.DrawLine(penAxis, centerX, 0, centerX, picGraph.Height) ' Y-axis ' Draw graph Dim penGraph As New Pen(Color.Blue, 2) Dim scale As Double = 50 ' Pixels per unit Dim prevPoint As PointF? = Nothing ' Plot the function For x As Double = -5 To 5 Step 0.01 Dim y As Double = f(x) Dim px As Single = centerX + x * scale Dim py As Single = centerY - y * scale If prevPoint IsNot Nothing Then g.DrawLine(penGraph, prevPoint.Value.X, prevPoint.Value.Y, px, py) End If prevPoint = New PointF(px, py) Next End Using ' Display the graph picGraph.Image = bmp End Sub
Unlock the power of Visual Basic with this comprehensive guide that bridges the gap between classic VB6 and modern VB.NET programming. Learn to create mathematical visualization tools like this cubic graph plotter and many other practical applications.
Test your Visual Basic skills with these graphing exercises:
Modify the cubic graph plotter to include function transformations:
Extend the program to plot multiple functions simultaneously:
Dr. Liew Voon Kiong, the founder and webmaster of VBTutor.net, holds a Bachelor's Degree in Mathematics, a Master's Degree in Management, and a Doctorate in Business Administration from the University of South Australia.
With decades of experience in software development and education, Dr. Liew created the renowned Visual Basic Tutorial website in 1996, which has become a top resource for VB learners worldwide. He has authored multiple best-selling books on Visual Basic programming.