Quadratic Functions in Python part 2: Coefficients
Overview
In the first article of this series I wrote a class to represent a quadratic function, evaluate it over a given interval, and print and plot the results. Please read Part 1 before proceeding to this article.
This is the general form of the function implemented by the class.
In the demo code for Part 1 I left the coefficients a, b and c at their defaults of 1, 0 and 0 respectively. These values give a basic parabola but other values can be used to translate and/or transform the parabola, ie move it around or stretch/squash it. The coefficients therefore enable us to use quadratic functions to model real-world behaviour in many fields.
In this article I will create a selection of functions with non-default coefficients and of course plot the results with Matplotlib.
The Project
This project consists of the following files:
qf1.py
qfplotter.py
qfdemo2.py
which you can clone or download from the GitHub repository The depository will be used for all files for this series of articles and already contains qfdemo1.py from Part 1.
The code for this article uses the NumPy and Matplotlib libraries. If you are not familiar with these you might like to read my reference articles on them to get you up ‘n’ running quickly.
The Code
The QuadraticFunction class in qf1.py was listed and described in Part 1 so I won’t repeat it here. The class included a method to create a single plot but the following code, qfplotter.py, allows for the plotting of multiple parabolas passed in a list of QuadraticFunction objects.
After the imports the plot function iterates the list of QuadraticFunctions, passing each in turn to Matplotlib’s plot method. Note that the label is set to equation_str to show in the legend the function used for each plot.
The qfdemo2.py file contains code to use a variety of coefficients and plot the result.
After importing qf1 and qfplotter the main function first creates a trio of variables for the x values and interval, and then an empty list for QuadraticFunction objects.
The next four chunks of code create sets of coefficient for us to evaluate and plot - you might like to study the comments or you might prefer to look at the plots.
After the coefficients we have a for/in loop to iterate them and use them as arguments to new QuadraticFunction objects. For each evaluate is called, and it is then appended to the list. Finally we just pass the list to the qfplotter’s plot function.
Note that there are four for/in loops so you can just uncomment the one you want to run.
Running the Program
That’s the coding done so run the program with this command:
python3 qfdemo2.py
This is the first plot - a basic parabola.
If you uncomment the for/in line with coefficients_a the plot looks like this. The default coefficients are plotted here in orange and as you can see the other values stretch the parabola up and down, including through the horizontal to an inverted shape. Note however that the vertex is always unchanged at (0,0).
Now run the for/in loop with coefficients_b. The result might be a bit confusing at first so it’s worth studying to get you head round what is happening. Probably the easiest way to comprehend the behaviour is to imagine the vertex tracing out the path of the brown curve as the value of the b coefficient changes.
Now run the code with coefficients_c uncommented. This time it’s nice and simple: the parabola just moves up or down.
For the sake of demonstrating the a, b and c coefficients I have only changed one at a time from the defaults. However, you can of course change two or all three for an infinite variety of parabolas. In a future article I’ll use Matplotlib sliders so we can do just that.
No AI was used in the creation of the code, text or images in this article.










