1

I am trying to create a program that will work out how long it will take for something to hit the ground when dropped from a particular height and it uses the known quadratic formula. The program appears to be doing what I want of it until it reaches line 7 where there is a math issue I beleive involving sqrt. Can anybody help?

So far I have come up with...

v = float(input())
lowerSum = 2*(-4.9)
upperSum1 = -4*(-4.9)
upperSum2 = (upperSum1)*(11000)
upperSum3 = (v)**2 - (upperSum2)
from math import sqrt
upperSum4 = (v) - sqrt(upperSum3)
t = (upperSum4) / (lowerSum)
print (t)

When I run the program it states that there is a math domain error; I'm new to programming and I do not know what this means.

I am trying to print out the value of t.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2633836
  • 131
  • 3
  • 5
  • 13

4 Answers4

1

You misread the formula (bottom of page).

Specifically, you are applying the minus sign after v**2 twice:

First you apply it here (incorrectly, as if it was a negative sign):

upperSum1 = -4*(-4.9)

Then you apply it again here (correctly, as a minus symbol).

upperSum3 = (v)**2 - (upperSum2)

upperSum1 should be:

upperSum1 = 4*(-4.9)
Jon-Eric
  • 16,977
  • 9
  • 65
  • 97
0

The 'math domain error' is telling you that you've passed the sqrt function a value that is outside it's "domain". The domain of the sqrt function is the positive numbers. At some point upperSum3 becomes negative, which makes the sqrt rather unhappy, so it throws an error at you.

You could fix this by adding an if statement to change what your code does if upperSum3 is less than 0, for example:

if upperSum3 < 0:
    #do something other than take the sqrt
else:
    upperSum4 = (v) - sqrt(upperSum3)
Brionius
  • 13,858
  • 3
  • 38
  • 49
0

Just going a bit deeper into the problem by doing the math...

v = float(input())
lowerSum = -9.8
upperSum1 = 19.6
upperSum2 = 215600
upperSum3 = (v)**2 - (215600)
from math import sqrt
upperSum4 = (v) - sqrt(upperSum3)
t = (upperSum4) / (lowerSum)
print (t)

So you have to have the square of v in line 5 be more than 215600 for the equation to not return a negative number to sqrt(), which is what is causing the error.

V has to be greater than 465 for the upperSum3 to be positive, and by running various numbers, it never actually becomes positive, so there's definitely something wrong with the equation you are using, or the set-up itself.

Without knowing the formula myself (which I can't find) there's no way for me to tell which part of the code is actually wrong.

MisterRios
  • 315
  • 3
  • 7
  • The formula I have is as follows it is taken from http://cscircles.cemc.uwaterloo.ca/7b-math/ and is at the foot of the page. This is the only question on the page I am unable to solve... – user2633836 Aug 12 '13 at 14:25
0
import math
v=float(input())
t=float()
Result1=float(v-(math.sqrt(float(v**2)-float(4*(-4.9))*(11000))))
Result2=float(2*(-4.9))
t=float(Result1/Result2)
print(t)

This works. However, I'm new to python and was having trouble with my results converting to an integer. So.... if my use of "float" seems excessive, it's cause it is :-) For a good half an hour I was trying to perform all of the calculations on one line but gave up and broken it down. If anyone has a better solution that performs the calculation on one line I'm all ears.