I am trying to integrate an exponential function using a Lambda function first time. There are two versions of codes that should work the same, but the one with Lambda function is giving an error saying:
The code giving the error is
import numpy as np
import sympy as sym
# Predetermined parameter values
s, t, T= 0.2 ,0, 0.25
a1, a2= 1.2, 2.3
X1, X2, X3=0.5,-2.0,0.3
# Symbolic variable for integration
u = sym.symbols('u')
# Version 1 giving the above error
fx= lambda X1,X2,X3,a1,a2,T,u: (X1*sym.exp(-a1*(T-u)) + X2*sym.exp(-a2*(T-u))+X3)**2
Fx=sym.integrate(sym.expand(fx), (u,t,s))
Fx=float(Fx)
On the other hand, I can obtain the output Fx using the following version without the Lambda function.
# Version 2 works fine
def expFun3fsq(X1,X2,X3,a1,a2,T,u):
# Squared single line exponential function
# It returns a symbolic function where u is the only symbol in the function
fx= (X1*sym.exp(-a1*(T-u))+X2*sym.exp(-a2*(T-u))+X3) **2
return fx
Fx=sym.integrate(sym.expand(expFun3fsq(X1,X2,X3,a1,a2,T,u)), (u,t,s))
Fx=float(Fx)
What are the causes of the error and how can I fix the problem?
When you write
f = lambda x: 1 + x
you are telling python that you will provide a single argument tof
(likef(1)
) and that argument should be referenced asx
in the formula. You only need a lambda if you want to be able to change an argument of an expression easily (in my example thef
will add 1 to whatever you pass to it).When you write
f = lambda: 1 + x
this would mean that you aren't going to pass any parameters and you just want the function to use the local value ofx
and add 1.It's important to make sure you understand the above outputs. The lambda behave the same way as a defined function -- and you showed that you know how to use the defined function.
Since you fix all but
u
and you want to integrate an expression that is a function ofu
you have to define that expression in some way: you can either pass the parameters to a function/lambda (properly) to get that expression or just write the expression. So changefx= lambda X1,X2,X3,a1,a2,T,u:
tofx =
and your code will work. OR, pass all the variables to the lambda just like you did in the case of using the function.Here,
fx
is the expressionHere,
fx
is a function to which we must pass all parametersHere,
fx
is a function ofu
and we use the local values of the variablesAll methods give the same answer: