# def f(x): evaluvates individually but not inside plot

Hi, I am stuck with a curious error. The code to reproduce error is given below. The function T basically integrates the function with a given value of L.

def T(L): return n(integral(4/sqrt(pi)*exp(-L)*(x^2)*exp(-x^2/4)/cosh(sqrt(L)*x),x,0,+Infinity))
plot(T,5,7)


The function T works fine if I ask

print T(5)


But it gives error, when i give the above mentioned plot command. What might be going wrong here?

edit retag close merge delete

Sort by » oldest newest most voted

Using numerical_integral will allow you to produce a plot as follows.

def T(L):
ans=numerical_integral(4/sqrt(pi)*exp(-L)*(x^2)*exp(-x^2/4)/cosh(sqrt(L)*x),0,oo)
return ans[0]

plot(lambda L: T(L),(L,5,7))

more

@calc314 Thankyou very much. This works for me. Though I am still curious why the integral() function didn't work.

( 2012-11-28 17:04:33 +0200 )edit

I can't get T to evaluate for noninteger points. For example, T(5.3) gives the following error.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_22.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("VCg1LjMp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>

File "/tmp/tmpudPhvP/___code___.py", line 3, in <module>
exec compile(u'T(_sage_const_5p3 )
File "", line 1, in <module>

File "/tmp/tmp4oZQrZ/___code___.py", line 5, in T
return integral(_sage_const_4 /sqrt(pi)*exp(-L)*(x**_sage_const_2 )*exp(-x**_sage_const_2 /_sage_const_4 )/cosh(sqrt(L)*x),x,_sage_const_0 ,oo)
File "/home/sageserver/sage-5.0.1/local/lib/python2.7/site-packages/sage/misc/functional.py", line 728, in integral
return x.integral(*args, **kwds)
File "expression.pyx", line 8745, in sage.symbolic.expression.Expression.integral (sage/symbolic/expression.cpp:33707)
File "/home/sageserver/sage-5.0.1/local/lib/python2.7/site-packages/sage/symbolic/integration/integral.py", line 633, in integrate
return definite_integral(expression, v, a, b)
File "function.pyx", line 413, in sage.symbolic.function.Function.__call__ (sage/symbolic/function.cpp:4678)
File "/home/sageserver/sage-5.0.1/local/lib/python2.7/site-packages/sage/symbolic/integration/integral.py", line 173, in _eval_
return integrator(*args)
File "/home/sageserver/sage-5.0.1/local/lib/python2.7/site-packages/sage/symbolic/integration/external.py", line 21, in maxima_integrator
result = maxima.sr_integral(expression, v, a, b)
File "/home/sageserver/sage-5.0.1/local/lib/python2.7/site-packages/sage/interfaces/maxima_lib.py", line 747, in sr_integral
raise error
RuntimeError: ECL says: In function GCD, the value of the second argument is
1.0
which is not of the expected type INTEGER


From some experimenting, the problem appears to be in cosh. If I take the sqrt(L) out of cosh, then it will evaluate. This seems odd.

In the meantime, you might using a combinations of exponentials in place of cosh to force the computation to work.

more

Using exponentials also does not work when sqrt(L) is not an integer.

( 2012-11-28 13:48:42 +0200 )edit

Huh. Note that sage: n(integral(4/sqrt(pi)*exp(-5.3)*(x^2)*exp(-x^2/4)/cosh(sqrt(53/10)*x),x,0,oo)) gives 0.00231492212520274.

( 2012-11-28 14:25:48 +0200 )edit

But I'm having trouble getting a Maxima command that will reproduce this error with 5.3.

( 2012-11-28 14:27:50 +0200 )edit

The following doesn't work either: integrate(exp(-5.3*x),x,0,1) Somehow the float isn't being passed somewhere correctly. Why does the error message mention the GCD function? I can't figure out why that function is involved here.

( 2012-11-28 19:05:33 +0200 )edit

The error is coming from ECL, so that indicates the problem may be somewhere in maxima.

( 2012-11-28 21:48:43 +0200 )edit