00:01
Okay, so in this question, we want to write a python function that solves for roots of quadratic equations.
00:11
So just i wrote a quick reminder on how to mathematically solve the equation.
00:18
So a times x square plus bx plus c equals zero.
00:25
So what we do first is we find delta, which is b square minus four times a c.
00:34
If delta is negative, then equation a has no real solution.
00:40
If delta is equal to zero, then equation a has one real solution, which is x minus x, which is x equals to minus b divided by 2a.
00:54
And if delta is greater than zero, then equation a has two real solutions, which are x1 and x2, so minus b plus or minus the square root of delta divided by 2a so those are the mathematical concept and we're going to translate them into code so here i'm going to write a polynomial so polynomio so i'm going to put a b and c so i'm going to put a b and c so i'm going to put uh one, four, and 21.
01:43
Those will be my a, b, and c.
01:45
It's a very basic representation of a polynomial.
01:50
And here i'm going to create a function called roots, which is going to take as an input polynomial.
02:05
Here i'm going to call it my polynomial, just to make it obvious that there are two different polynomial.
02:13
And here, for now, i'm simply going to return and i'm going to print roots of my polynomial.
02:28
So if i run this program, as you can see, this is way too big.
02:33
It prints zero, which is normal because my roots function only return zero.
02:40
But now i can call it, i know my polynomial is well -defined, i know my function can accept the polynomial.
02:48
So i can put more relevant things in it so first we need to find delta how do we how to find out well it's b square minus four times a times c so we're going to have that a is equal to polynomial the first the first coefficient so i can reuse a notation i will have a b and c to be equal to the coefficient i wrote in my polynomial i can simply reuse what i already have mathematically so when i write a comment here first we find delta so delta will be equal to b square so in python to write b square you write power of b and two so b to the power of b and two so b to the power that's how you square a number minus four times a times although you don't need to put them i will still put parentheses just to make it obvious that this is a separate calculation and here i'm gonna have my so i need data but i need to put my roots somewhere so i'm gonna put them in a list the same way i put my coefficient of polynomial and a list.
04:41
I'm going to have my roots and to begin the list is going to be empty.
04:50
And here using delta, we can find the roots...