Ask Your Question
0

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

asked 2012-11-28 06:11:11 -0500

indiajoe gravatar image

updated 2013-06-26 11:56:38 -0500

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

2 answers

Sort by ยป oldest newest most voted
0

answered 2012-11-28 08:11:05 -0500

calc314 gravatar image

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))
edit flag offensive delete link more

Comments

@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 10:04:33 -0500 )edit
0

answered 2012-11-28 06:44:04 -0500

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 "_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.

edit flag offensive delete link more

Comments

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

calc314 gravatar imagecalc314 ( 2012-11-28 06:48:42 -0500 )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 07:25:48 -0500 )edit

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

kcrisman gravatar imagekcrisman ( 2012-11-28 07:27:50 -0500 )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 12:05:33 -0500 )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 14:48:43 -0500 )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

Stats

Asked: 2012-11-28 06:11:11 -0500

Seen: 473 times

Last updated: Nov 28 '12