0

I looked up similar questions but I still don't understand how to call values from string to propagate. When I try to run propagate I get an error that c isn't defined. I am assuming the same is true for delta t and x it's just that the error triggers on the first undefined variable it comes across.

def string(T, mu, length):

#legnth is just the number of elements in the array that represents the 
#string, the strings length is 1m
delta_x = 1 / length
#c is the speed of the wave in the string
c = (T / mu) ** (0.5)
delta_t = delta_x / c

string_v = np.zeros((3,length))
return(string_v, delta_x, delta_t, c)

What do I have to do to allow the variables to be called in the next function?

def propagate(A, omega):

t = 0
r = c * delta_t / delta_x
string_v[0,0] = string_v[1,0] = A*np.sin(omega*t)
while t < 100:

    for i in range(len(length)):
        string_v[2,i] = 2 *(1 - r**2)*string_v[1,i] - string_v[0,i] + r**2(string_v[1,i+1] + string_v[1,i-1])

    for i in range(len(length)):
        string_v[1,i] = string_v[0,i]
        string_v[2,i] = string_v[1,i]

    t = t + delta_t
return(string_v)
  • 2
    can't you pass the variables into the function that need those variables? – chickity china chinese chicken May 10 '17 at 01:10
  • I am pretty dumb when it comes to programming but are you saying that I feed the variables into the function call? – user7988953 May 10 '17 at 01:22
  • 1
    No worries, yes exactly that's what I mean. Provide the variables both to the function call, and the function definition as more parameters, just as you had done with `A`, and `omega` – chickity china chinese chicken May 10 '17 at 01:25
  • I suppose I could. The purpose of having the two functions is so that I don't always have to enter all those variables every time that I want to run the calculation on my string. Propagate will be used many times and it's much easier to not have to initialize the strings conditions each time I want to run the calculation for the strings position. – user7988953 May 10 '17 at 01:32
  • That's what using parameters as `var1=None` is for, when you don't want to require that parameter – chickity china chinese chicken May 10 '17 at 01:40
  • 1
    Then perhaps you really want to write a `class` which has those values as properties. – deceze May 10 '17 at 01:41
  • I know nothing about class, so I guess I should read about that. I will probably fail to comprehend how to implement it, but I can try. I am not trying to put down any ideas but I feel like it should be easy to just call values from a different function and that I am just having a hard time understanding how. I think I am just going to implement the string outside of a function as to avoid this problem. Thank you for the help though. – user7988953 May 10 '17 at 01:55
  • To be honest, you should first read about variable scope in Python. Briefly, scope is in the order LEGB (local, enclosing, global, build-in). One function doesn't have access to variables from another function. If you want to have that, then you either pass them in, declare them globally, or make a class – Abid Hasan May 10 '17 at 03:55

2 Answers2

0

Edit: You might be able to use some kind of function overloading. This answer's example is better than mine: Overloaded functions in python?

A class is not necessary nor would it be appropriate:

The purpose of a class is to bundle a data structure which represents some logical entity with the operations that work with this data structure.

Ref: [Tutor] When to use a Class or just define functions?

You're not working with objects or any entities with properties or data structures. As for as I can tell, you're doing calculations on "data". Just pass parameters when you need to use them:

def string(T, mu, length):

    #legnth is just the number of elements in the array that represents the 
    #string, the strings length is 1m
    delta_x = 1 / length
    #c is the speed of the wave in the string
    c = (T / mu) ** (0.5)
    delta_t = delta_x / c

    prop_1 = propagate(A=5, omega=6)
    prop_2 = propagate(A=7, omega=8, c=9, delta_t=10, delta_x=11)

    return prop_1, prop_2

def propagate(A, omega, c=None, delta_t=None, delta_x=None):

    # check is any of these variables are not passed into this function
    if not all([c, delta_t, delta_x]):
        answer_a = 'c, delta_t and delta_x not used'
    # otherwise, all variables were passed in
    else:
        answer_a = 'all variables used'

    return answer_a

print string(T=1.1, mu=1.1, length=1)

output:

('c, delta_t and delta_x not used', 'all variables used')
Community
  • 1
  • 1
  • Not every class has to be a "data structure". Any database access class basically proves that wrong: `db = new Database('localhost', 'foo', 'bar'); record = db.find('baz')`. Would it make more sense to do `database_find('localhost', 'foo', 'bar', 'baz')` every time? No. – deceze May 10 '17 at 07:21
0

You can either pass variables as parameters from one function to another or you can make the required variables global.

Aanchal1103
  • 917
  • 8
  • 21