Python Using Lambda Within a Function - python

I am trying to create a function called calc(f,a,b) where x is an equation with the variable f and I want to put this code within the function.
def calc(f, a, b):
limits = [a, b]
integral = odeint(lambda y, x : f, 0, limits)
return integral[1]
This function gets the integral using the built in odeint function.
This is what I am trying to do
print calc(x**2, 0, 1)
where x^2 is the function to be integrated. My problem is that this function (x**2)needs to be passed on to the odeint function right after y, x: f where f after the semicolon is the f from the calc(f,a,b)
what I cant figure out is that how can I pass f from the calc function input to the odeint inside. It says that f isnt declared and if I put it within strings.. it doest work
When I run this function.. it doesnt work I get this error
NameError: name 'f' is not defined
I am not sure how to pass my equation to be integrated inside odeint
Thanks

If one were to rewrite the function calc as follows:
def calc(f, a, b):
limits = [a, b]
integral = odeint(lambda y, x: f(x), 0, limits)
return integral[1][0]
Then one may use this function thus:
>>> calc(lambda x: x ** 2, 0, 1) # Integrate x ** 2 over the interval [0, 1] (expected answer: 0.333...)
0.33333335809177234
>>> calc(lambda x: x, 0, 1) # Integrate x over the interval [0, 1] (expected answer: 0.5)
0.50000001490120016
>>> calc(lambda x: 1, 0, 1) # Integrate 1 over the interval [0, 1] (expected answer: 1.0)
1.0
The odeint function from the scipy.integrate module has the signature:
odeint(func, y0, t, ...)
where: func is a callable that accepts parameters y, t0, ... and returns dy/dt at the given point; y0 is a sequence representing initial condition of y; t is a sequence that represents intervals to solve for y (t0 is the first item in the sequence).
It appears that you are solving a first-order differential equation of the form dy/dx = f(x) over the interval [a, b] where y0 = 0. In such a case, when you pass f (which accepts one argument) to the function odeint, you must wrap it in a lambda so that the passed-in function accepts two arguments (y and x--the y parameter is essentially ignored since you need not use it for a first-order differential equation).

I assume odeint is some function to which you are passing the lambda function. odeint will presumably call the lambda and needs to pass x and y to it. So the answer is, if you want odeint to call the function and pass it x and y, then you need to pass x and y to odeint as arguments, in addition to the function itself.
What exactly are you trying to do here? With more details and more code, we could probably get a better answer.

x cannot have two values; therefore, if you need two values, one of them must be named something else. Rename one of your variables.
Edit:
(smacking forehead): In calc(x**2, 0, 1), x**2 is not a function - it is an expression, which gets evaluated before being passed to calc - therefore it complains about it needs to know what x is (in order to calculate x**2).
Try
calc(lambda x: x**2, a, b)
instead. This is equivalent to
def unnamedfunction(x):
return x**2
calc(unnamedfunction, a, b)

I'm not completely sure because odeint() is not a built-in Python function so I don't know much about it, but the first argument you're passing it in your code is not a function that computes x^2. An easy way to do something like that would be to pass a lambda function to calc that does that sort of calculation. For example:
def calc(f, a, b):
limits = [a, b]
integral = odeint(f, 0, limits)
return integral[1]
print calc(lambda x: x**2, 0, 1)

Related

Getting the derivative of a function as a function with sympy (for later evaluation and substitution)

I want to work with generic functions as long as possible, and only substitute functions at the end.
I'd like to define a function as the derivative of another one, define a generic expression with the function and its derivative, and substitute the function at the end.
Right now my attempts is as follows, but I get the error 'Derivative' object is not callable:
from sympy import Function
x, y, z = symbols('x y z')
f = Function('f')
df = f(x).diff(x) # <<< I'd like this to be a function of dummy variable x
expr = f(x) * df(z) + df(y) + df(0) # df is unfortunately not callable
# At the end, substitute with example function
expr.replace(f, Lambda(X, cos(X))) # should return: -cos(x)*sin(z) - sin(y) - sin(0)
I think I got it to work with integrals as follows:
I= Lambda( x, integrate( f(y), (y, 0, x))) but that won't work for derivatives.
If that helps, I'm fine restricting myself to functions of a single variable for now.
As a bonus, I'd like to get this to work with any combination (products, derivatives, integrals) of the original function.
It's pretty disappointing that f.diff(x) doesn't work, as you say. Maybe someone will create support it sometime in the future. In the mean time, there are 2 ways to go about it: either substitute x for your y, z, ... OR lambdify df.
I think the first option will work more consistently in the long run (for example, if you decide to extend to multivariate calculus). But the expr in second option is far more natural.
Using substitution:
from sympy import *
x, y, z = symbols('x y z')
X = Symbol('X')
f = Function('f')
df = f(x).diff(x)
expr = f(x) * df.subs(x, z) + df.subs(x, y) + df.subs(x, 0)
print(expr.replace(f, Lambda(X, cos(X))).doit())
Lambdifying df:
from sympy import *
x, y, z = symbols('x y z')
X = Symbol('X')
f = Function('f')
df = lambda t: f(t).diff(t) if isinstance(t, Symbol) else f(X).diff(X).subs(X, t)
expr = f(x) * df(z) + df(y) + df(0)
print(expr.replace(f, Lambda(X, cos(X))).doit())
Both give the desired output.

Having trouble with using sympy subs command when trying to solve a function at an x value of 0

I have a function [ -4*x/sqrt(1 - (1 - 2*x^2)^2) + 2/sqrt(1 - x^2) ] that I need to evaluate at x=0. However, whenever you graph this function, for some interval of y there are many y-values at x=0. This leads me to think that the (subs) command can only return one y-value. Any help or elaboration on this? Thank you!
Here's my code if it might help:
x = symbols('x')
f = 2*asin(x) # f(x) function
g = acos(1-2*x**2) # g(x) function
eq = diff(f-g) # evaluating the derivative of f(x) - g(x)
eq.subs(x, 0) # substituting 0 for x in the derivative of f(x) - g(x)
After I run the code, it returns NaN, which I assume is because substituting in 0 for x returns not a single number, but a range of numbers.
Here is the graph of the function to be evaluated at x=0:
You should always give SymPy as many assumptions as possible. For example, it can't pull an x**2 out of a sqrt because it thinks x is complex.
A factorization an then a simplification solves the problem. SymPy can't do L'Hopital on eq = A + B since it does not know that both A and B converge. So you have to guide it a little by bringing the fractions together and then simplifying:
from sympy import *
x = symbols('x', real=True)
f = 2*asin(x) # f(x) function
g = acos(1-2*x**2) # g(x) function
eq = diff(f-g) # evaluating the derivative of f(x) - g(x)
eq = simplify(factor(eq))
print(eq)
print(limit(eq, x, 0, "+"))
print(limit(eq, x, 0, "-"))
Outputs:
(-2*x + 2*Abs(x))/(sqrt(1 - x**2)*Abs(x))
0
4
simplify, factor and expand do wonders.

solving differential equation with step function

I am trying to solve this differential equation as part of my assignment. I am not able to understand on how can i put the condition for u in the code. In the code shown below, i arbitrarily provided
u = 5.
2dx(t)dt=−x(t)+u(t)
5dy(t)dt=−y(t)+x(t)
u=2S(t−5)
x(0)=0
y(0)=0
where S(t−5) is a step function that changes from zero to one at t=5. When it is multiplied by two, it changes from zero to two at that same time, t=5.
def model(x,t,u):
dxdt = (-x+u)/2
return dxdt
def model2(y,x,t):
dydt = -(y+x)/5
return dydt
x0 = 0
y0 = 0
u = 5
t = np.linspace(0,40)
x = odeint(model,x0,t,args=(u,))
y = odeint(model2,y0,t,args=(u,))
plt.plot(t,x,'r-')
plt.plot(t,y,'b*')
plt.show()
I do not know the SciPy Library very well, but regarding the example in the documentation I would try something like this:
def model(x, t, K, PT)
"""
The model consists of the state x in R^2, the time in R and the two
parameters K and PT regarding the input u as step function, where K
is the infimum of u and PT is the delay of the step.
"""
x1, x2 = x # Split the state into two variables
u = K if t>=PT else 0 # This is the system input
# Here comes the differential equation in vectorized form
dx = [(-x1 + u)/2,
(-x2 + x1)/5]
return dx
x0 = [0, 0]
K = 2
PT = 5
t = np.linspace(0,40)
x = odeint(model, x0, t, args=(K, PT))
plt.plot(t, x[:, 0], 'r-')
plt.plot(t, x[:, 1], 'b*')
plt.show()
You have a couple of issues here, and the step function is only a small part of it. You can define a step function with a simple lambda and then simply capture it from the outer scope without even passing it to your function. Because sometimes that won't be the case, we'll be explicit and pass it.
Your next problem is the order of arguments in the function to integrate. As per the docs (y,t,...). Ie, First the function, then the time vector, then the other args arguments. So for the first part we get:
u = lambda t : 2 if t>5 else 0
def model(x,t,u):
dxdt = (-x+u(t))/2
return dxdt
x0 = 0
y0 = 0
t = np.linspace(0,40)
x = odeint(model,x0,t,args=(u,))
Moving to the next part, the trouble is, you can't feed x as an arg to y because it's a vector of values for x(t) for particular times and so y+x doesn't make sense in the function as you wrote it. You can follow your intuition from math class if you pass an x function instead of the x values. Doing so requires that you interpolate the x values using the specific time values you are interested in (which scipy can handle, no problem):
from scipy.interpolate import interp1d
xfunc = interp1d(t.flatten(),x.flatten(),fill_value="extrapolate")
#flatten cuz the shape is off , extrapolate because odeint will go out of bounds
def model2(y,t,x):
dydt = -(y+x(t))/5
return dydt
y = odeint(model2,y0,t,args=(xfunc,))
Then you get:
#Sven's answer is more idiomatic for vector programming like scipy/numpy. But I hope my answer provides a clearer path from what you know already to a working solution.

How to find root of two implicit functions in Python, without using fsolve for the set of equations

I am dealing with a set of several non-linear equations that I was able to reduce analytically to set of two implicit equations with two variables. Now I wish to find roots of those equations using Brent's method. I would like to pass one function as an argument to another and solve the equation for variable 2 depending on every variable 1.
In mathematical terms I wish to solve: f(x,y) and g(x,y) in this way f(x,g(x)).
Simplify example of what I wish to do can be presented here.
Instead of:
import scipy.optimize
from scipy.optimize import fsolve
def equations(p):
y,z = p
f1 = -10*z + 4*y*z - 5*y + 4*z**2 - 7
f2 = 2*y*z + 5*y - 3
return (f1,f2)
and solve it by:
y, z = fsolve(equations,[0,19])
I wish to write something like that:
def func2(x, y):
f2= 2*y*x + 5*y - 3
return brentq(f2, -5, 5)
def func(x,y):
y = func2(x,y)
return -10*x + 4*x*y - 5*y + 4*x**2 - 7
sol, = brentq(lambda x: func(x, func2), -5, 5)
I wish to ask for help in how to pass a function as an argument for this particular purpose and explain what am I doing wrong. I am new to Python and maybe there is a better way to ensure precise solution to my problem.

Fitting only one parameter of a function with many parameters in python

In python I have a function which has many parameters. I want to fit this function to a data set, but using only one parameter, the rest of the parameters I want to supply on on my own. Here is an example:
def func(x,a,b):
return a*x*x + b
for b in xrange(10):
popt,pcov = curve_fit(func,x1,x2)
In this I want that the fitting is done only for a and the parameter b takes the value of the loop variable. How can this be done?
You can wrap func in a lambda, as follows:
def func(x, a, b):
return a*x*x + b
for b in xrange(10):
popt, pcov = curve_fit(lambda x, a: func(x, a, b), x1, x2)
A lambda is an anonymous function, which in Python can only be used for simple one line functions. Basically, it's normally used to reduce the amount of code when don't need to assign a name to the function. A more detailed description is given in the official documentation: http://docs.python.org/tutorial/controlflow.html#lambda-forms
In this case, a lambda is used to fix one of the arguments of func. The newly created function accepts only two arguments: x and a, whereas b is fixed to the value taken from the local b variable. This new function is then passed into curve_fit as an argument.
A better approach would use lmfit, which provides a higher level interface to curve-fitting. Among other features, Lmfit makes fitting parameters be first-class objects that can have bounds or be explicitly fixed (among other features).
Using lmfit, this problem might be solved as:
from lmfit import Model
def func(x,a,b):
return a*x*x + b
# create model
fmodel = Model(func)
# create parameters -- these are named from the function arguments --
# giving initial values
params = fmodel.make_params(a=1, b=0)
# fix b:
params['b'].vary = False
# fit parameters to data with various *static* values of b:
for b in range(10):
params['b'].value = b
result = fmodel.fit(ydata, params, x=x)
print(": b=%f, a=%f+/-%f, chi-square=%f" % (b, result.params['a'].value,
result.params['a'].stderr,
result.chisqr))
Instead of using the lambda function which might be less intuitive to digest I would recommend to specify the scikit curve_fit parameter bounds that will force your parameter to be searched within custom boundaries.
All you have to do is to let your variable a move between -inf and +inf and your variable b between (b - epsilon) and (b + epsilon)
In your example:
epsilon = 0.00001
def func(x,a,b):
return a*x*x + b
for b in xrange(10):
popt,pcov = curve_fit(func,x1,x2, bounds=((-np.inf,b-epsilon), (np.inf,b+epsilon))
I effectively use Anton Beloglazov's solution, though I like to avoid using lambda functions for readability so I do the following:
def func(x,a,b):
return a*x*x + b
def helper(x,a):
return func(x,a,b)
for b in xrange(10):
popt,pcov = curve_fit(helper, x1, x2)
This ends up being reminiscent of Rick Berg's answer, but I like having one function dedicated to the "physics" of the problem and a helper function to get the code to work.
Another way is to use upper and lower bounds that are identical (+ eps) as the initial value.
Using the same example with initial conditions and bounds:
def func(x,a,b):
return a*x*x + b
# free for a and b
popt,pcov = curve_fit(func, x1, x2,
p0=[1,1],
bounds=[(-inf,-inf),(inf,inf)])
# free for a; fixed for b ;
eps=1/100
popt,pcov = curve_fit(func, x1, x2,
p0=[1,1],
bounds=[(-inf,(1-eps)),(inf,(1+eps))])
Remember to insert an epsilon, otherwise, a and b must be the same
There is a simpler option if you are willing/able to edit the original function.
Redefine your function as:
def func(x,a):
return a*x*x + b
Then you can simply put it in your loop for parameter b:
for b in xrange(10):
popt,pcov = curve_fit(func, x1, x2)
Caveat: the function needs to be defined in the same script in which it is called for this to work.
Scipy's curve_fit takes three positional arguments, func, xdata and ydata.
So an alternative approach (to using a function wrapper) is to treat 'b' as xdata (i.e. independent variable) by building a matrix that contains both your original xdata (x1) and a second column for your fixed parameter b.
Assuming x1 and x2 are arrays:
def func(xdata,a):
x, b = xdata[:,0], xdata[:,1] # Extract your x and b
return a*x*x + b
for b in xrange(10):
xdata = np.zeros((len(x1),2)) # initialize a matrix
xdata[:,0] = x1 # your original x-data
xdata[:,1] = b # your fixed parameter
popt,pcov = curve_fit(func,xdata,x2) # x2 is your y-data

Categories