0

How to create a variable inside a function name?

So I have this code in Python (just starting with it, still in development, I need to solve this problem first): from sympy import * n = 4 q = 8 L = 10 x = Symbol('x') p = 1 M0p = (n-1)*q*x/2 I0p = (0, L/2) How do I make the "p" in "M0p" and "I0p" behave like a variable? Because I want to have multiple functions called M01, M02, M088, I01, I02, I0444 and more. Sorry for my English and thank you!

1st Jun 2017, 3:40 PM
Victor Martins
Victor Martins - avatar
1 Answer
+ 2
From your code I see that you created seven variables by assigning values to them. "p" won't behave as a variable in mathematical sense because "M0p" is a variable too - you've just calculated its value based on the value of "p". To define a function you need this structure def function_name(parameter1, parameter2, ..., parametern): x = _your_calculations_ return x In your case the function will look something like this: def M0p(x, y, z): var = (x - 1) * y * z / 2 return var (names of the variables you define inside the function don't really matter, just make sure they're consistent) Then you need to call you function on the needed variables and don't forget the right order. You can assign the result of a function to another variable or print it result = M0p(n, q, x) print(result) I understand, that you want to have a function as we usually have them in algebra, but so far I don't see what exactly you want to do, so I can't really help with that. Mathematical operators won't run on the symbol of x, but you can have a list of x values (function definition area) and a list of resulting y values. Will that work for you?
1st Jun 2017, 4:37 PM
Lana
Lana - avatar