Search code examples
pythonpendulum

Coding Exercise: Watch the Pendulum


In physics, for a pendulum with length L and initial angle A, its horizontal >displacement X(T) at time T is given by the formula X(T) = L × cos(A × cos(T × √9.8/L)) - L × cos(A)

Write a program which takes two lines of input; the first line is L and the >second line is A. The output should be ten lines, giving the values of X(0), >X(1), X(2), ..., X(9). For example, if the first line of input is 53.1 and the >second line of input is 0.8, then the first line of output is 0.0 and the second >line of output is 53.1*cos(0.8*cos(1*√9.8/53.1)) - 53.1*cos(0.8) ~ 2.6689.

import math 
L = float(input())
A = float(input())
for T in range (0,9):
print(L*math.cos(A*math.cos(T*math.sqrt(9.8/L))-L*math.cos(A)))

i have written this and i cant understand what am i doing wrong ?

input :

53.1
0.8

my output:

3.545012155898153
7.383727226708044
17.92714440725987
31.889478979714276
44.23118522394127
51.212404291669216
53.079364553814806
52.890770379027806
52.999922313121566

expected answer:

0.0
2.6689070487226805
9.021742145820763
14.794542557581206
15.73774678328343
11.124903835610114
4.423693604072537
0.27377375601245213
1.295906539090336
6.863309996333497

Solution

  • You have a bracket in the wrong place, change

    L*math.cos(A*math.cos(T*math.sqrt(9.8/L))-L*math.cos(A))
    

    to

    L*math.cos(A*math.cos(T*math.sqrt(9.8/L)))-L*math.cos(A)
    

    This gave the correct output on my pc

    Edit: also change range(0,9) to range(10)