Trying to build a specific function

I have a problem. I am trying to build a function for different values d. I defined das:

d = np.arange(0.0, 100.0, 0.01)

But I still get the same error:

TypeError: only length-1 arrays can be converted to Python scanners

This is my script:

import pylab
import numpy as np
import scipy
import matplotlib.pyplot as plt
import math
from scipy.optimize import curve_fit
import numpy

def teo_function(d):
    return 2*math.pi*math.sqrt(((1**2)/(12+d**2))/9.81*d)

d = np.arange(0.0, 100.0, 0.01)
T = teo_function(d)
pylab.plot (d,teo_function(d), 'bo', d, teo_function(d), 'k')

pylab.show()

Thank you all for your help.

+4
source share
3 answers

You must digitize your function teo_functionto work with the array:

import numpy as np
import matplotlib.pyplot as plt
import math

def teo_function(d):
    return 2*math.pi*math.sqrt(((1**2)/(12+d**2))/9.81*d)
vecfunc = np.vectorize(teo_function)

d = np.arange(0.0, 100.0, 0.01)
T = vecfunc(d)
plt.plot (d, T, 'bo', d, T, 'k')
plt.show()

enter image description here

+3
source

teo_function math.sqrt, , . - numpy, , numpy, . numpy.sqrt. Numpy , math.module, numpy.

Numpy , , .

:

def teo_function(d):
    return 2*np.pi*np.sqrt(((1**2)/(12+d**2))/9.81*d)
+2

Another way. If you want your function to return a list

def teo_function(ds):
    return [2*math.pi*math.sqrt(((1**2)/(12+d**2))/9.81*d) for d in ds]
+1
source

All Articles