"Write a Python program to read in the three floating point coefficients a, b and c of the quadratic equation:\n",
"\n",
"$a x^2 + b x + c = 0$ \n",
"$$ a x^2 + b x + c = 0$$ \n",
"\n",
"then calculate and display the roots using the standard formula for roots of quadratic being,\n",
"\n",
"$r_i = \\frac{ −b± \\sqrt{b^2−4ac}}{2a}$\n",
"$$ r_i = \\frac{ −b± \\sqrt{b^2−4ac}}{2a} $$\n",
"\n",
"The program is expected to deal with conditions of\n",
"\n",
...
...
@@ -30,8 +30,7 @@
"2. two complex roots\n",
"3. single real root\n",
"\n",
"Your program must contain a function to calcuate the discriminant, d=$b^2$−4ac\n",
"and must have a main(): method. All variables must be declared inside a function or main(). \n",
"Your program must contain a function to calculate the discriminant, $d=b^2−4ac$ and must have a main(): method. All variables must be declared inside a function or main(). \n",
"\n",
"### Background\n",
"\n",
...
...
%% Cell type:markdown id:75c1fe44 tags:
## Checkpoint 2
### Aim
To write a Python program to read in the coefficients of a quadratic equation and print out the roots for all conditions.
This program demonstrates how to use a simple function, the use of conditional statements, mainly the `if : elif : else:` construct, and how to deal with the various input conditions.
### Task
Write a Python program to read in the three floating point coefficients a, b and c of the quadratic equation:
$a x^2 + b x + c = 0$
$$ a x^2 + b x + c = 0$$
then calculate and display the roots using the standard formula for roots of quadratic being,
$r_i = \frac{ −b± \sqrt{b^2−4ac}}{2a}$
$$ r_i = \frac{ −b± \sqrt{b^2−4ac}}{2a} $$
The program is expected to deal with conditions of
1. two real roots
2. two complex roots
3. single real root
Your program must contain a function to calcuate the discriminant, d=$b^2$−4ac
and must have a main(): method. All variables must be declared inside a function or main().
Your program must contain a function to calculate the discriminant, $d=b^2−4ac$ and must have a main(): method. All variables must be declared inside a function or main().
### Background
In addition to the sections for Checkpoint 1, you will need to have read and studied the following additional sections:
Note: the structure of this program is more complex than it initially looks; plan out what you are going to do before you start coding and also check it a bit at a time.
### Checkpoint
- Check your program with the following values:
- a=2.0, b=4.0 and c=−8.0
- a=2.0, b=−4.0 and c=8.0
- a=2.0, b=4.0 and c=2.0
- a=0.0, b=6.0 and c=8.0
Ensure you get sensible answers for the four cases. **Remember when a=0.0 you will get a linear equation with a single root. You should handle this correctly.**
> #### Note:
> You should think about using the `cmath.sqrt` and `math.sqrt` functions to deal explicitly with square roots of complex and real numbers respectively.
- Call a demonstrator, show them your code and run your program with the four sets of inputs above and any additional ones requested by the demonstrator.
- Ensure that the demonstrator finds you in Learn. Please check that your pass/fail (0/1) has been entered correctly (you should be able to see this in Learn).
### Assessment
To pass this checkpoint:
- The program must work correctly for at least three of the above four cases.
- The prompting is sensible.
- The code structure uses a function to calculate the discriminant and has a main(): function.
- The code uses conditional statements `(if : elif : else:)` to test for the various cases.
### Next steps
Once you have completed this checkpoint, please continue with the [Week2](../WeeklyTasks/Week2.ipynb) tasklist.