Categories
Python

Complex Solutions to Quadratic Equations

In my previous post I looked at finding real solutions to a quadratic equation. But what about if we wanted to find complex solutions as well.

Luckily it seems to be very easy we just need to import the complex math module using cmath. To be annoying Python uses j rather than i for \sqrt(-1), but I guess we can live with that.

So the code simply becomes:

import math, cmath
print('Quadratic Solver - ax\u00B2 + bx + c = 0')
a = float(input('a: '))
b = float(input('b: '))
c = float(input('c: '))
d = math.pow(b,2)-4*a*c
s1 = (-b+cmath.sqrt(d))/(2*a)
s2 = (-b-cmath.sqrt(d))/(2*a)
print(s1)
print(s2)

We can also give the solutions in modulus argument form for a bit of variation.

import math, cmath
print('Quadratic Solver - ax\u00B2 + bx + c = 0')
a = float(input('a: '))
b = float(input('b: '))
c = float(input('c: '))
d = math.pow(b,2)-4*a*c
s1 = (-b+cmath.sqrt(d))/(2*a)
s2 = (-b-cmath.sqrt(d))/(2*a)
print('Solution 1')
print('Rectangular Coordinates =', s1)
print('Modulus =', abs(s1))
print('Argument =', cmath.phase(s1))
print('Solution 2')
print('Rectangular Coordinates =', s2)
print('Modulus =', abs(s2))
print('Argument =', cmath.phase(s2))
Categories
Python

Quadratic Equation Solver

The first program I’m going to attempt is to find real solutions to quadratic equations.

This is my first attempt at a Python program and here is the code:

import math
print('Quadratic Solver - ax\u00B2 + bx + c = 0')
a = float(input('a: '))
b = float(input('b: '))
c = float(input('c: '))
d = math.pow(b,2)-4*a*c
if d>0:
    s1 = (-b+math.sqrt(d))/(2*a)
    s2 = (-b-math.sqrt(d))/(2*a)
    print(s1)
    print(s2)
else:
    print('No real Solutions')

To explain what is going on:

import math
print('Quadratic Solver - ax\u00B2 + bx + c = 0')

The first line is telling it that we want to use some maths functions in this program. Specifically powers and roots.

The second line is nothing more than an instruction to the user what the program is supposed to do and what a, b and c represent.

a = float(input('a: '))

The three lines like this allow the user to input a, b and c. The float bit makes the input a number as opposed to a string.

d = math.pow(b,2)-4*a*c

Next we work out the discriminant to see if there are any real solutions. The math.pow(b,2) means b² is this line.

Finally we have an if: else: statement so if there are any real solutions we find them, and then print them.

s1 = (-b+math.sqrt(d))/(2*a)
s2 = (-b-math.sqrt(d))/(2*a)
print(s1)
print(s2)

The math.sqrt(d) bit is the square root of the discriminant. If there are no real solutions then we say, no real solutions.

How would this work with students?

Now this really is the problem and the question I have not yet answered.

I don’t want to spend time teaching Python, there is not time in the course. But I feel that learning Python will make them better mathematicians and better placed for the future be it university or employment.

How can I ask them to write this program?
What do I need to provide them.

Categories
Python

First steps with Python

The first step is to install it. I’m not going to go into details here but it is quite straightforward. Just search for download Python and follow the instructions for whatever computer you have.

The Casio CG50 does have Python built in. It would be madness to try to type a program in using the calculator keys however but you can type it up on a PC and then transfer it using the USB cable.

It is important to realise at this stage that I am not an experienced Python programmer. I’m learning and writing this at the same time. I may not be doing things the best way.

I’ve no problem with that. It’s how I learn. I hope people might reply to these posts with suggestions of how it could be done better, alternative methods and approaches and ideas.

I’m also going to link what I’m doing to the A-Level Maths and Further Maths course. I am not going to spend time teaching Python to students but I think encouraging them to play and explore it will help.

Python programming, as far as I can see, is all about structure and thinking about the processes behind how things work. If students think about how things work then will have a better understand of the mathematics and hence be better at it.

That is the plan.

The second step is to write a program…

Categories
General

Welcome

This website is all about integrating technology into A-Level Mathematics and Further Mathematics.

In particular I plan on looking at:

  • Python
  • LaTeX
  • Excel
  • GeoGebra/Desmos
  • Casio ClassWiz and CG50

And I intend to think about how these can be used to enhance teaching and learning of maths at A-Level.