KPBTO
KPBTO

Reputation: 29

Error of "TypeError: unsupported operand type(s) for /: 'list' and 'int'"

I have an error of the following below. I understand that python does not allow operands for different data types such as 'lists' and 'int' However my 'h' variable is not a list as i have simply initialized it as such:

import numpy as np
import matplotlib.pyplot as plt

v0 = 1                  # Initial velocity
theta = 45              # Launch angle
t0 = 0                  # Start time
tf = 100                # End time
h = 1                   # Steps

Uy = v0 * np.sin(theta*np.pi/180) # Initial velocity in y component
Ux = v0 * np.cos(theta*np.pi/180) # Initial velocity in x component
t = np.arange(t0,tf,h)            # Time points

v = [Uy,Ux]
s = [Uy,Ux]

def velocity(v,t):
    g  = 9.81               # Acceleration of free fall
    
    Vy = v[0]               # Array for initial value
    Vx = v[1]               # Array for initial value
    
    dVy_dt = -g
    dVx_dt = 0
    
    return [dVy_dt,dVx_dt]

def position(s,t):
    g  = 9.81               # Acceleration of free fall
    
    Sy = Uy               # Array for initial value
    Sx = Ux               # Array for initial value
    
    dSy_dt = Sy - (g*t)
    dSx_dt = Sx * t
    
    return [dSy_dt,dSx_dt]

def RK4(func,t,h,y0):
    nt = t.size
    y = np.zeros(nt)
    y[0] = y0
    for n in range(0,nt - 1):
        k1 = func(t[n],y[n])
        k2 = func(t[n] + h/2,y[n] + h*(k1/2))
        k3 = func(t[n] + h/2,y[n] + h*k2/2)
        k4 = func(t[n] + h,y[n] + h*k3)
        y[n+1] = y[n] + (h*k1/6 + h*k2/3 + h*k3/3 + h*k4/6)
        
    return y

Position_y = RK4(position,t,h,s[0])
Position_x = RK4(position,t,h,s[1])

TypeError                                 Traceback (most recent call last)
<ipython-input-21-8faeddf072bd> in <module>
     51     return y
     52 
---> 53 Position_y = RK4(position,t,h,s[0])
     54 Position_x = RK4(position,t,h,s[1])

<ipython-input-21-8faeddf072bd> in RK4(func, t, h, y0)
     44     for n in range(0,nt - 1):
     45         k1 = func(t[n],y[n])
---> 46         k2 = func(t[n] + h/2,y[n] + h*(k1/2))
     47         k3 = func(t[n] + h/2,y[n] + h*k2/2)
     48         k4 = func(t[n] + h,y[n] + h*k3)

TypeError: unsupported operand type(s) for /: 'list' and 'int'

Could it be the y variable giving me the error message?

Upvotes: 0

Views: 55

Answers (1)

JimmyCarlos
JimmyCarlos

Reputation: 1952

When running your code:

k1 = [-6.2296107422534828, 0.5]

Which causes the error on the next line.

Upvotes: 1

Related Questions