Home > Enterprise >  float() argument must be a string or a number, not 'function'. What is wrong with this cod
float() argument must be a string or a number, not 'function'. What is wrong with this cod

Time:10-24

I am trying to make a function call from a choice of two functions based on the variable 't' value. But "float() argument must be a string or a number, not 'function'" comes up. Please help.

import numpy as np
import sympy as sym
import matplotlib.pyplot as plt

n = 9
t = np.linspace(0,10,n)
u = np.linspace(0.0,n)
v = np.linspace(0.0,n)
a = np.linspace(0.0,n)

def T_td(t,utd,vtd):
    t = sym.symbols('t')
    y = utd*sym.sin(5*t)   vtd*sym.cos(5*t)
    yp = y.diff(t)
    ypp = yp.diff(t)
    j = sym.lambdify(t,y)
    k = sym.lambdify(t,yp)
    l = sym.lambdify(t,ypp)
    return j,k,l

def td_T(t):
    t = sym.symbols('t')
    y = sym.sin(5*t)   sym.cos(5*t)
    yp = y.diff(t)
    ypp = yp.diff(t)
    j = sym.lambdify(t,y)
    k = sym.lambdify(t,yp)
    l = sym.lambdify(t,ypp)
    return j,k,l

def func(t,utd,vtd):
    if t < 5:
        u,v,a = td_T(t)
        utd = 0
        vtd = 0
    elif t == 5:
        u,v,a = td_T(t)
        utd = u
        vtd = v
    else:
        u,v,a = T_td(t,utd,vtd)
    return u,v,a,utd,vtd


#print(t)
for i in range(0,n,1):
    u[i],v[i],a[i],u_td,v_td = func(t[i],0,0)

CodePudding user response:

The first 3 values in the tuple returned by func() are of the type <function _lambdifygenerated at 0x1282cd090>

The target is a numpy array of floats.

Hence the error

CodePudding user response:

Well, there are many probable errors in this code. The linspace arguments. The useless "t" arguments to td_T and T_td (since the first thing you do in it, is overwrite t with a symbolic value), the apparently useless u_dt and v_td in the main loop.

But the one causing your error, is the fact that u, v and a are numpy arrays of floats. And you are trying to force feed them with functions.

3 1st values returned by func are the values returned by td_T and T_td functions. Which are all the result of sym.lambdify. And as its name suggest, sym.lambdify returns a function, not a float. You are supposed to call those functions with some parameters. And since I've no idea what you are trying to do, I've no idea neither about which would be those parameters. But there have to be some.

Otherwise, it is like you where trying to do

u[i]=sin
v[i]=cos
a[i]=len

sin, cos or len are functions. sin(0), cos(0) and len([]) are numbers.

Likewise, the j, k, l your td_T and T_td functions returns are functions. j(0), k(1), l(2) would be numbers suited to be stored in u[i] and its kind.

  • Related