简体   繁体   中英

What is this error trying to tell me and how can I fix it?

I am building a double pendulum in python and have a built a doublePen class where, and I am a little new to python classes and I can not figure this error out

TypeError: can only concatenate tuple (not "int") to tuple

            """
            theta1 = state[0]
            theta2 = state[2]
            intVM1 = state[1]
            intVM2 = state[3]
            """
            #makes a empty array of zeros
            dydx = np.zeros_like(state)
            #sets the dydx[1] equal to angular velocity of mass 1
            dydx[0] = state[1]

            del_ = state[2] - state[0]
            den1 = (self.M1 + self.M2)*self.L1 - self.M2*self.L1*np.self.cos(del_)*np.self.cos(del_)
            dydx[1] = (self.M2*self.L1*state[1]*state[1]*np.self.sin(del_)*np.self.cos(del_) +
                       self.M2*self.G*np.self.sin(state[2])*np.self.cos(del_) +
                       self.M2*self.L1*state[3]*state[3]*np.self.sin(del_) -
                       (self.M1 + self.M2)*self.G*np.self.sin(state[0]))/den1
            #derivative of dydx
            dydx[2] = state[3]

            den2 = (self.L1/self.L1)*den1
            dydx[3] = (-self.M2*self.L1*state[3]*state[3]*np.self.sin(del_)*np.self.cos(del_) +
                       (self.M1 + self.M2)*self.G*np.self.sin(state[0])*np.self.cos(del_) -
                       (self.M1 + self.M2)*self.L1*state[1]*state[1]*np.self.sin(del_) -
                       (self.M1 + self.M2)*self.G*np.self.sin(state[2]))/den2

            return dydx

all other varibles like M1, M2, etc are defined as well as cos(x) and sin(x) [edit] here is a github gist link to look at all other code if you need to https://gist.github.com/Jackbaude/b30cb6d83972d76c1949aeec8cd94869 Thank you for any help

the + operator requires compatible operands. for example two integers (1 + 2).

it will also accept other operands, such as two tuples (eg (1,2) + (3,4) == (1,2,3,4)), but not operands of different types (eg number + tuple).

I have checked your gist and it seems your issue is a typo. You have a comma after M1 initialization which makes it a tuple, then you do (M1 + M2) where M1 is a tuple and M2 is an int. all you need to do is to remove the comma from this line in doublePen class in the __init__ function:

self.M1 = mass1,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM