I'm trying to create a table of values based upon tanh(x) (nevermind the cubic function for the moment):

#!/usr/bin/python

import math

def cubic(y0, y1, y2, y3, mu):
    a0 = y3 - y2 - y0 + y1
    a1 = y0 - y1 - a0
    a2 = y2 - y0
    a3 = y1
    mu2 = mu*mu
    return (a0*mu*mu2+a1*mu2+a2*mu+a3)

for i in range(0,1001):
    x = (i * .004) - 2
    y = math.tanh(x)
    table[i] = y

But when I run it, I get the following error:

Traceback (most recent call last):
File "./cubictable.py", line 16, in <module>
table = y
NameError: name 'table' is not defined

How am I supposed to pre-define the list table[]? Thanks!

Recommended Answers

All 2 Replies

like this

table = []
for i in range(0,1001):
    x = (i * .004) - 2
    y = math.tanh(x)
    table.append(y)

for example :)

Ah, the one way I didn't try. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.