print 5*0.6 - 0.5*9.81*0.6**2 print 5*0.6 - 0.5*9.81*0.6**2 from numpy import * def integrate(f, a, b, n=100): """ Integrate f from a to b, using the Trapezoidal rule with n intervals. """ x = linspace(a, b, n+1) # Coordinates of the intervals h = x[1] - x[0] # Interval spacing I = h*(sum(f(x)) - 0.5*(f(a) + f(b))) return I # Define my special integrand def my_function(x): return exp(-x**2) minus_infinity = -20 # Approximation of minus infinity I = integrate(my_function, minus_infinity, 1, n=1000) print 'Value of integral:', I v0 = 5 g = 9.81 t = 0.6 y = v0*t - 0.5*g*t**2 print y initial_velocity = 5 accel_of_gravity = 9.81 TIME = 0.6 VerticalPositionOfBall = initial_velocity*TIME - \ 0.5*accel_of_gravity*TIME**2 print VerticalPositionOfBall # program for computing the height of a ball # in vertical motion v0 = 5 # initial velocity g = 9.81 # acceleration of gravity t = 0.6 # time y = v0*t - 0.5*g*t**2 # vertical position print y t = 0.6; y = 1.2342 print 'At t=%g s, y is %.2f m.' % (t, y) v0 = 5 g = 9.81 t = 0.6 y = v0*t - 0.5*g*t**2 print """ At t=%f s, a ball with initial velocity v0=%.3E m/s is located at the height %.2f m. """ % (t, v0, y) a = 1 # 1st statement (assignment statement) b = 2 # 2nd statement (assignment statement) c = a + b # 3rd statement (assignment statement) print c # 4th statement (print statement) a = 1; b = 2; c = a + b; print c myvar = 10 myvar = 3*myvar # = 30 myvar myvar = 5.2 prinnt Myvar v0=3 v0 = 3 v0= 3 v0 = 3 counter = 1 while counter <= 4: counter = counter + 1 # correct (4 leading blanks) while counter <= 4: counter = counter + 1 # invalid syntax v0 = 3; g = 9.81; t = 0.6 position = v0*t - 0.5*g*t*t velocity = v0 - g*t print 'position:', position, 'velocity:', velocity C = 21 F = (9/5)*C + 32 print F C = 21 F = (9.0/5)*C + 32 print F a = 5 # a refers to an integer (int) object b = 9 # b refers to an integer (int) object c = 9.0 # c refers to a real number (float) object d = b/a # d refers to an int/int => int object e = c/a # e refers to float/int => float object s = 'b/a=%g' % (b/a) # s is a string/text (str) object print d, e, s a = 3 # a is int b = float(a) # b is float 3.0 c = 3.9 # c is float d = int(c) # d is int 3 d = round(c) # d is float 4.0 d = int(round(c)) # d is int 4 d = str(c) # d is str '3.9' e = '-4.2' # e is str f = float(e) # f is float -4.2 import math r = math.sqrt(2) # or from math import sqrt r = sqrt(2) # or from math import * # import everything in math r = sqrt(2) from math import sin, cos, log x = 1.2 Q = sin(x)*cos(x) + 4*log(x) # log is ln (base e) print Q v1 = 1/49.0*49 v2 = 1/51.0*51 print '%.16f %.16f' % (v1, v2) a = 1; b = 2; computed = a + b expected = 3 correct = computed == expected print 'Correct:', correct a = 0.1; b = 0.2; expected = 0.3 a + b == expected print '%.17f\n%.17f\n%.17f\n%.17f' % (0.1, 0.2, 0.1 + 0.2, 0.3) from math import sinh, exp, e, pi x = 2*pi r1 = sinh(x) r2 = 0.5*(exp(x) - exp(-x)) r3 = 0.5*(e**x - e**(-x)) print '%.16f %.16f %.16f' % (r1,r2,r3) C = 41 F = (9.0/5)*C + 32 print F F a = -2 b = 0.5 s = complex(a, b) # make complex from variables s s*w # complex*complex s/w # complex/complex s.real s.imag from sympy import * t, v0, g = symbols('t v0 g') y = v0*t - Rational(1,2)*g*t**2 dydt = diff(y, t) # 1st derivative dydt print 'acceleration:', diff(y, t, t) # 2nd derivative y2 = integrate(dydt, t) y2 y = v0*t - Rational(1,2)*g*t**2 roots = solve(y, t) # solve y=0 wrt t roots x, y = symbols('x y') f = -sin(x)*sin(y) + cos(x)*cos(y) simplify(f) expand(sin(x+y), trig=True) # requires a trigonometric hint from math import sin, log x = 5 r = sin(3*log(10*x)) g = 9.81 # m/s**2 v0 = 15 # km/h theta = 60 # degrees x = 0.5 # m y0 = 1 # m print """v0 = %.1f km/h theta = %d degrees y0 = %.1f m x = %.1f m""" % (v0, theta, y0, x) # convert v0 to m/s and theta to radians: v0 = v0/3.6 from math import pi, tan, cos theta = theta*pi/180 y = x*tan(theta) - 1/(2*v0)*g*x**2/((cos(theta))**2) + y0 print 'y = %.1f m' % y