We then create a corresponding array of y-values with y = a * x ** 2 + b * x + c. The code uses np.linspace() (from the numpy library) to create an array of values the x part of the coordinates to be plotted. # Create 1000 equally spaced points between -10 and 10Īx.set_title("Quadratic Equations with Python")Īx.hlines(y=0, xmin=min(x), xmax=max(x), colors='r', linestyles='-', lw=1) Once your graph is produced, you can zoom in to see the solutions if needed. For example we can use the matplotlib package (installation may be needed) to visualize a quadratic function and see where it intersects the x-axis (its real solutions). There are some awesome open-source tools available for working with mathematics in Python. Plotting a Quadratic Function with Python The equation has two solutions: 3.0 or 2.0 Sample output: Enter the coefficient a: 1 Print(f"The equation has one solution: ") Print("The equation has no real solutions") Input your values for a, b, and c from the equation in form ax² + bx + c = 0, and the (real) solutions will be displayed if there are any.Ī Python program to solve quadratic equations in the formĪ = int(input("Enter the coefficient a: "))ī = int(input("Enter the coefficient b: "))Ĭ = int(input("Enter the coefficient c: "))ĭ = b ** 2 - 4 * a * c # this part is called the discriminant The program below is based on the famous quadratic equation formula. Python Program for Solving Quadratic equations When it comes to using programming to enhance Maths learning though, Python is much more powerful and accessible. Things have come a long way since the days of BASIC, although messing around with it certainly can be educational. Using TI-BASIC to solve quadratic equations looked something like this: Some of you who are familiar with pseudocode from the various Computer Science exam syllabuses in the UK may notice a similarity between pseudocode and TI-BASIC (and other forms of BASIC – one of the most widely used languages when home computers first became “a thing”). One great feature of these calculators is that you can actually write programs on them, using a language called TI-BASIC. Many years ago I used to use TI-84 graphing calculators to teach Maths.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |