Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

anyone know the command in IDLE to solve: Positive root of the following equation: 34*x^2 + 68*x - 510

OpenStudy (anonymous):

There is no single command to solve the quadratic function. You will need to define a function.

OpenStudy (microbot):

plus in python ^ is **

OpenStudy (anonymous):

Hi @Monti, I am too a beginner. I wrote this rather naive script which solves any quadratic equation. Returns both positive and negative roots. Try it and tell me if it is ok. In your question, a=34, b= 68 and c =-510 >>> def roots(a,b,c): ... import math ... root1 = (-b + math.sqrt(b**2-4*a*c))/(2*a) ... root2 = (-b - math.sqrt(b**2-4*a*c))/(2*a) ... result = print("First Root is",root1,"and Second Root is",root2) ... return result

OpenStudy (anonymous):

thanks @MicroBot for my first medal. :-)))))

OpenStudy (anonymous):

What if it has a single solution or no solution at all?

OpenStudy (anonymous):

Just asking :)

OpenStudy (anonymous):

@Chris2332, you are right. The script will have to be modified to include a error message.

OpenStudy (anonymous):

Why error? One solution or no solution is a valid outcome of a quadratic function.

OpenStudy (anonymous):

ya. It should return "No solution to this quadratic equation" (or something like that.)

OpenStudy (microbot):

bah i cant give both a medal xD

OpenStudy (anonymous):

Hehehehh

OpenStudy (microbot):

but w8 chris has too many xD sry chris xD

OpenStudy (anonymous):

Hahaha :)

OpenStudy (anonymous):

@Chris2332 and@MicroBot, Both of you have already started to think like a Computer Scientist, so I will be bothering you frequently. Here is my modified program for @Monti: Do tell me what u think. def roots (a,b,c): #Solving A Quadratic Equation import math if (b**2-4*a*c)<0: print 'Sorry, this equation has no solution, dude.' elif (b**2-4*a*c)==0: x1 = (-b + math.sqrt(b**2 - 4*a*c))/(2*a) print 'This equation has only one root:',x1 else: x1 = (-b + math.sqrt(b**2 - 4*a*c))/(2*a) x2 = (-b - math.sqrt(b**2 - 4*a*c))/(2*a) print 'First root of the equation is:',x1 print ' and the second root is:',x2 print 'Here is the sanity check for you:' print ' a*x*x+b*x+c=',a*x1*x1+b*x1+c, 'and', a*x2*x2+b*x2+c print ' which are equal to zero (or almost zero), dude.'

OpenStudy (anonymous):

Looking good. Two minor points: 1) Just change your calculation for the one real root to: x1 = -b/(2*a) 2) When the discriminant is less than zero then the quadratic equation does not have a REAL root. It does have two distinct (non-real) complex roots: \[\frac{ -b }{ 2a }+i \frac{ \sqrt{-\Delta} }{ 2a }\] and \[\frac{ -b }{ 2a }-i \frac{ \sqrt{-\Delta} }{ 2a }\]

OpenStudy (anonymous):

Just saying :)

OpenStudy (anonymous):

Here's a version so you don't calculate the discriminant constantly: def roots (a,b,c): #Solving A Quadratic Equation import math discriminant = b**2-4*a*c if discriminant < 0: print 'Sorry, this equation has no solution, dude.' elif discriminant == 0: x1 = -b/(2*a) print 'This equation has only one root:',x1 else: x1 = (-b + math.sqrt(discriminant))/(2*a) x2 = (-b - math.sqrt(discriminant))/(2*a) print 'First root of the equation is:',x1 print ' and the second root is:',x2 print 'Here is the sanity check for you:' print ' a*x*x+b*x+c=',a*x1*x1+b*x1+c, 'and', a*x2*x2+b*x2+c print ' which are equal to zero (or almost zero), dude.'

OpenStudy (microbot):

about non real roots: so @Dileep_6.00 has to edit print 'Sorry, this equation has no solution, dude.' as print 'Sorry, this equation has no real roots, dude.'? or he has to calculate the unreal ones too?

OpenStudy (anonymous):

Up to the requirements of the exercise! :)

OpenStudy (microbot):

says "Positive root of the following equation: "

OpenStudy (anonymous):

No idea what that means... cause if we take it literally, then any real negative roots shouldn't be displayed either!

OpenStudy (anonymous):

@Monti, @MicroBot,@Chris2332. Hi, Thanks to you all. It is amazing indeed how fast we learn when we study together in this gr8 forum. Never thought, i will be able to do something like this. Here is my latest version for Quadratic roots: def roots(a,b,c): #Roots of A Quadratic Equation import math if (b**2-4*a*c)< 0: x1 = (-b)/(2*a) x2 = (math.sqrt(-b**2+4*a*c))/(2*a) print 'This equation has imaginary roots:' print x1,'+ i*',x2,' and ',x1,'- i*',x2, elif (b**2-4*a*c)==0: x3 = (-b)/(2*a) print 'This eqution has only one root:',x3 print ' Sanity check: ax^2+bx+c =',(a*x3*x3+b*x3+c) else: x4 = (-b + math.sqrt(b**2-4*a*c))/(2*a) x5 = (-b - math.sqrt(b**2-4*a*c))/(2*a) print 'The roots are:',x4,'and',x5 print 'Sanity check: ax^2+bx+c =',(a*x4*x4+b*x4+c),'and',(a*x5*x5+b*x5+c) print '...which are equal to zero (..or almost zero).' print 'Hey, it works, dude.'

OpenStudy (microbot):

k i am not an expert so it takes me some time to read a code written by others...but wouldnt it be better if u called ur x1 and x2 somthing else? like r for real part u for unreal part:P....i mean i was thinking u were trying to calculate the roots and was like : "what is he doing there?" but about the rest , to my unexperiensed eyes it seems a great job.

OpenStudy (microbot):

if (b**2-4*a*c)< 0: r = (-b)/(2*a) u = (math.sqrt(-b**2+4*a*c))/(2*a) print 'This equation has imaginary roots:' print 'x1=',r,'+ i*',u,' and x2=',r,'- i*',u,

OpenStudy (anonymous):

@MicroBot Yep, we can use something better than x1 and x2 to make it more clear to readers. As Chris has suggested, we can use 'Discriminant' to avoid duplication. In fact, every programmer will use something different of his choice. That is the beauty of the whole thing. The entire script can be much shorter when we learn many more keywords.

OpenStudy (anonymous):

Where do you get the four from? Sorry, I was reading your posts and got really confused.

OpenStudy (microbot):

@M4ernay please be more specific. do u mean the 4 in : b**2-4ac?

OpenStudy (anonymous):

@M4emay , Hi, If you are referring to my post, please google search for 'Solving Quadratic Equations'. The derivation of the roots are explained there.

OpenStudy (anonymous):

@Microbot, Yes I am, did you make that up, or you Google the equation?

OpenStudy (anonymous):

@M4emay, Hi, this is a well known high school math solution for quadratic equations: x= (-b+/-sqrt(b^2-4ac))/2a. If u want to know how it is derived, search for it. btw, i had to recall it when i did 6.002x in June2012. The problem is that if b^2-4ac is <=0, then python returns a math domain error, hence those additional lines in the script. Have you done exercise 1 by now?

OpenStudy (microbot):

as Dileep says.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!