Ask Your Question

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

asked 2012-11-28 13:11:11 +0200

indiajoe gravatar image

updated 2023-01-09 23:59:31 +0200

tmonteil gravatar image

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))

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-11-28 15:11:05 +0200

calc314 gravatar image

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

def T(L): 
    return ans[0]

plot(lambda L: T(L),(L,5,7))
edit flag offensive delete link more


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

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

answered 2012-11-28 13:44:04 +0200

calc314 gravatar image

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 "", line 10, in <module>
    exec compile(u'open("","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("VCg1LjMp"),globals())+"\\n"); execfile(os.path.abspath(""))
  File "", line 1, in <module>

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

  File "/tmp/tmp4oZQrZ/", 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/", 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/", 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/", line 173, in _eval_
    return integrator(*args)
  File "/home/sageserver/sage-5.0.1/local/lib/python2.7/site-packages/sage/symbolic/integration/", 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/", line 747, in sr_integral
    raise error
RuntimeError: ECL says: In function GCD, the value of the second argument is
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.

edit flag offensive delete link more


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

calc314 gravatar imagecalc314 ( 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.

kcrisman gravatar imagekcrisman ( 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`.

kcrisman gravatar imagekcrisman ( 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.

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

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

Jason Grout gravatar imageJason Grout ( 2012-11-28 21:48:43 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-11-28 13:11:11 +0200

Seen: 752 times

Last updated: Nov 28 '12