Question
Description This project is intended to familiarize you with Input-Output and basic functions. Some conditional statements may also be used. The project is designed to
Description This project is intended to familiarize you with Input-Output and basic functions. Some conditional statements may also be used. The project is designed to be incremental, you can then debug, test and run your code after each new task is presented. Tasks 1-4 must be completed one after another, Tasks 5-9 (more difficult) can be completed in any order.
Grading Proposal This project will be graded out of 100 points:
1. Your program should implement all basic functionality/Tasks and run correctly. Tasks 1-4 (70 points), Tasks 5-8 (20 points), Task 9 (5 points bonus)
2. Overall programming style: program should have proper identification, and comments. (10 points).
How to start
Only one .py file is needed for this project. Use your preferred IDE to write and save your file (any name is fine). Functions should go at the top of the file. Do not forget to comment your code. Make sure you obtain the exact same output for the exact same input for the examples below (this includes syntax, blank spaces, and skipping blank lines). Your program will also be tested with different inputs by the graders.
Task-1- [20pts]
By first executing your code you should obtain the following: Welcome to Quadratic Solver for f(x)=ax**2+bx+c Enter value for a: 2 Enter value for b: 3 Enter value for c: 1 Equation is: f(x)=2.0x**2+3.0x+1.0 Press Enter to continue... By order, (1) the code is printing the welcome statement Welcome to Quadratic Solver for f(x)=ax**2+bx+c, (2) it is then skipping a line, (3) ask the user to enter a value for a, for b and for c (we use 2, 3, 1, respectively, for this example), (4) skip a line again, (5) return the expression of the quadratic function f(x), (6) skip a line, (7) wait for the user to press Enter/Return before to process further. 1 What you need to implement: 1. A function my quad equation that should accept a,b,c and should display or return the equation (String). The input coefficients a,b,c are considered float and could be converted to string before display. 2. the Press Enter to continue... wait instruction (including line skipping), could simply be achieved by adding this line to the program temp=input(" Press Enter to continue...") # wait where temp is just a temporary unused variable.
Task-2- [15pts] Let us continue with the execution of the program: Welcome to Quadratic Solver for f(x)=ax**2+bx+c Enter value for a: 2 Enter value for b: 3 Enter value for c: 1 Equation is: f(x)=2.0x**2+3.0x+1.0 Press Enter to continue... Enter value for x: 3 f(3.0)= 28.0 Press Enter to continue... The program is then asking the user to enter a value x (we choose 3 here), and it will return the value of the quadratic function for this specific x (i.e. f(x)). We then skip a line and wait again for the user to press Enter/Return before to process further. What you need to implement: 1. a new function evaluate quad equation that should accept the coefficients a,b,c and the variable x as arguments and return the value of f(x).
Task-3- [15pts]
Let us continue then with the program execution: Welcome to Quadratic Solver for f(x)=ax**2+bx+c Enter value for a: 2 Enter value for b: 3 Enter value for c: 1 Equation is: f(x)=2.0x**2+3.0x+1.0 Press Enter to continue... Enter value for x: 3.0 2 f(3.0)= 28.0 Press Enter to continue... f(x) has a minimum at x0=-0.75 with value f(x0)=-0.125 Press Enter to continue... A quadratic equation is mainly a parabola which goes through an extremum that could be a minimum if a>0 or a maximum if a<0...just to convince yourself, you can plot the function using your preferred software or just click here https://www.wolframalpha.com/input/?i=plot+2*x%5E2%2B3*x%2B1 the program is then computing the x = x0 position of the extremum, and return the value of the function at the extremum f(x0) (as you could see in the plot of the function for our particular example). An extremum of a function is obtained when f 0 (x) = 0 (derivative), and basic math should give you x = b/(2a) that we call x0 here. What you need to implement: 1. Just compute the extremum x0 and return its value 2. Use the function evaluate quad equation written in Task-2 to return the value of f(x0) 3. The code is displaying a summary sentence that provides the values of x0 and f(x0) and must also specifies if it is a minimum (like in this example) or a maximum (if you try your code with a value of a<0).
Task-4-[20pts]
Let us continue with the execution. Welcome to Quadratic Solver for f(x)=ax**2+bx+c Enter value for a: 2 Enter value for b: 3 Enter value for c: 1 Equation is: f(x)=2.0x**2+3.0x+1.0 Press Enter to continue... Enter value for x: 3.0 f(3.0)= 28.0 Press Enter to continue... f(x) has a minimum at x0=-0.75 with value f(x0)=-0.125 Press Enter to continue... Solving for f(x)=0 Discriminant is 1.0 Two real solutions: -0.5 and -1.0 Thanks for using Quadratic solver!, come back soon After this execution, the program must end by skipping a line and displaying Thanks for using Quadratic solver!, come back soon. 3 By steps, the program must: (1) simply display what this execution is doing, mainly solving for f(x) = 0, (2) compute and display the discriminant (value of d = b 2 4ac), (3) return the values of the solutions. If d > 0 like in this case, the program will return two real solutions x1 = b + d 2a , and x2 = b d 2a If d = 0 the program should return only one real solution x0 = b/(2a) (which happened to be the extremum already calculated in Task-3). If you choose the inputs a=1, b=2, c=1, the output (at this stage of the execution) should look like: Press Enter to continue... Solving for f(x)=0 Discriminant is 0.0 One real solution: -1.0 Thanks for using Quadratic solver!, come back soon If d < 0 there is no real solutions, using the inputs a=1, b=1, c=2 (for example), the program should then return (at this stage of the execution): Press Enter to continue... Solving for f(x)=0 Discriminant is -7.0 No real solutions Thanks for using Quadratic solver!, come back soon What you need to implement: 1. a new function called compute discriminant that will take a,b,c as inputs and return the value of d. 2. you will need to use some conditional statements to differentiate between the cases d = 0.0, d > 0.0 and d < 0.0. 3. To compute the solutions you need to use the square root function x known as sqrt(x). Since Python does not provide a built-in sqrt function, we will import it from the Math module. At the root (top) of your file add the following instruction import math you will then be able to call the square root function of x (for example) as follows: math.sqrt(x)
Task-5- [5pts]
If you made it that far it means that your program is executing correctly. Now, we aim at handling some special cases (corner cases). If you decide to enter as input a=1, b=1.0, c=2.5 (for example), most likely your output for the equation would currently look like: Equation is: f(x)=1.0x**2+1.0x+2.5 However, when the coefficients a and b are equal to 1.0, then there is no need to include them in the expression. What we would like to get is: 4 Equation is: f(x)=x**2+x+2.5 You need to change the code (function my quad equation) appropriately to make this happen.
Task-6- [5pts]
If you decide to enter as input a=-3.5, b=-2, c=-3 (for example), most likely your output for the equation would currently look like: Equation is: f(x)=-3.5x**2+-2.0x+-3.0 The (+-) is kind of ugly. We need to see the following instead: Equation is: f(x)=-3.5x**2-2.0x-3.0 You need to change the code (function my quad equation) appropriately to make this happen. Hint: we note that x = |x| if x is negative, and x = |x| if x is positive. The absolute value function is a Python built-in function that can be called using abs(x). In the cases where a=-1 and b=-1, we should also get (here c=3.0): Equation is: f(x)=-x**2-x+3.0
Task-7-[5pts]
Once the program tells you that one or two solutions are obtained, you could display just after the factorized form of f(x). For example if x1and x2 are solutions, the factorized form is f(x) = (x x1)(x x2), if only x0 is solution then it is f(x) = (x x0) 2 . Example of execution using 2,1,-1 for a,b,c gives: Press Enter to continue... Solving for f(x)=0 Discriminant is 9.0 Two real solutions: 0.5 and -1.0 Factorize form is: f(x)=(x-0.5)(x+1.0) Thanks for using Quadratic solver!, come back soon Using 1,2,1 for a,b,c we should get: 5 Press Enter to continue... Solving for f(x)=0 Discriminant is 0.0 One real solution: -1.0 Factorize form is: f(x)=(x+1.0)**2 Thanks for using Quadratic solver!, come back soon You could define a new function factorize form to accomplish this. As you can see, the factorize form can manage the correct sign nicely in the expression.
Task-8-[5pts]
If d < 0 there is no real solutions but there are two solutions in the complex plane. The formula to compute these solutions is the same than the real formula above but now we need to evaluate the square root of a negative number. This can easily be done using the regular square root function since, if x is negative then j p |x| is its square root (j stands for imaginary number). You can modify your program to display the complex solutions as follows for the case of a=1, b=1, c=2 Press Enter to continue... Solving for f(x)=0 Discriminant is -7.0 No real solutions Complex solutions are (-0.5+1.3228756555322954j) and (-0.5-1.3228756555322954j) Thanks for using Quadratic solver!, come back soon
task-9-[Bonus 5pts]
In your program you may use a conditional statement for comparing the value of d with 0.0. It is always difficult to compare float numbers. For example by using a=2.5, b=3.2, c=1.024, it is easy to show that d = b 2 4ac should be equal to 0. However, with Python you will most likely obtain something like d = 1.7763568394002505e 15 that makes it difficult to use a conditional statement while comparing with the value 0.0. Find a way to solve this problem. Note that no TAs help will be provided for bonus question.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started