Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does my code not work?
#1
Hi!

I'm trying to make a code that solves my equations with newtons method, but python prints "TypeError: 'float' object is not callable". I have tried different numbers of parentheses and to use Decimal insted, but nothing works.

I would appreciate it if someone could help me. Thanks! :-)

from  math import pi, sin, cos
from decimal import Decimal

m = 3
g = 9.8
R = 2
I = 12
x = 3*pi/4
eps = 0.000000001

def dadx(x):
    return (-m*g*R/(I))*cos(x)

def d2adx2(x):
    return (m*g*R/(I)*sin(x))

def newtonsb(dadx,d2adx2,x,eps):
    while abs(dadx(x))>eps:
        x - float(dadx(x))/d2adx2(x)
    return x

print "Maximum angular acceleration for releasing point x is at angle", newtonsb(dadx,d2adx2(x),x,eps)
Reply
#2
Probably what you want to do is this

from  math import pi, sin, cos
from decimal import Decimal
 
m = 3
g = 9.8
R = 2
I = 12
x = 3*pi/4
eps = 0.000000001
 
def dadx(x):
    return (-m*g*R/(I))*cos(x)
 
def d2adx2(x):
    return (m*g*R/(I)*sin(x))
 
def newtonsb(x):
    while abs(dadx(x))>eps:
        x =- float(dadx(x))/d2adx2(x)
        print x
    return x
 
print "Maximum angular acceleration for releasing point x is at angle", newtonsb(x)
That is assuming m, g, R, I, eps are global constants.
Alo on line 19 - I guess you want to change x value. The line in your code would do nothing (i.e. will not change x) and it will be infinite loop. I added print on line 20, just to see x change, you can remove it.
Finally, it looks you are using python2. You really should be using python3, official support for python2 ends 2020
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you so much! Now it works. The error in line 19 was just something that went wrong when I tried to fix the code.

The reason I'm using Python 2 is that my professor prefers this version and all the lecture notes are made in Python 2.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 772 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 675 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Beginner: Code not work when longer list raiviscoding 2 816 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,775 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,427 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 883 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,231 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  cannot get code to work Led_Zeppelin 10 2,427 Jun-30-2022, 06:28 PM
Last Post: deanhystad
  How does this code work? pd_minh12 3 1,335 Apr-15-2022, 02:50 AM
Last Post: Pedroski55
  What should i do, for this code to work -> description hamad 2 1,457 Nov-18-2021, 01:22 PM
Last Post: ghoul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020