TypeError using plot3d

Hi,

this is my first time using Sage, so this might be a very trivial question for which, however, I could not find an answer:

I would like to produce a 3d plot of a complicated function, the part where it fails seems to be:

T = RealDistribution('gaussian',1)
plot3d(T.cum_distribution_function((1-y)/x), (x,-2,2), (y,-2,2))


where I get the following error message:

   sage: plot3d(T.cum_distribution_function((1-y)/x), (x,-2,2), (y,-2,2))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-db1aa10c790f> in <module>()
----> 1 plot3d(T.cum_distribution_function((Integer(1)-y)/x), (x,-Integer(2),Integer(2)), (y,-Integer(2),Integer(2)))

/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/gsl/probability_distribution.so in sage.gsl.probability_distribution.RealDistribution.cum_distribution_function (sage/gsl/probability_distribution.c:6127)()

/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.__float__ (sage/symbolic/expression.cpp:7716)()

TypeError: unable to simplify to float approximation


Where does this error come from and how can I plot the function?

edit retag close merge delete

Sort by » oldest newest most voted

You need to define the function in the plot using a python lambda function. This has to do with how the distribution function is being evaluated. It is not a symbolic function in this case but is a numeric function from python. The following produces a plot for me.

var('y')
T = RealDistribution('gaussian',1)
plot3d(lambda x,y: T.cum_distribution_function((1-y)/x), (x,-2,2), (y,-2,2))


See the link here to execute the code in the single cell server.

more

Thank you for the rapid answer, this way it worked for me. However, I have another question as my function involes more terms. The problem occurs also when I do the following:

    var('y')
T = RealDistribution('gaussian',1)
plot3d(lambda x,y:  log(1/(2*pi*x)) + log(T.cum_distribution_function((1-y)/x)), (x,-2,2), (y,-2,2))


While the log() in the second term seems to be no problem, the log() in the first term somehow breaks something, and I get the following long error message:

    TypeError                                 Traceback (most recent call last)
<ipython-input-1-6c109b6281a7> in <module>()
1 var('y')
2 T = RealDistribution('gaussian',Integer(1))
----> 3 plot3d(lambda x,y:  log(Integer(1)/(Integer(2)*pi*x)) + log(T.cum_distribution_function((Integer(1)-y)/x)), (x,-Integer(2),Integer(2)), (y,-Integer(2),Integer(2)))

/home/sageserver/sage/ipython/IPython/core/displayhook.pyc in __call__(self, result)
245             self.start_displayhook()
246             self.write_output_prompt()
--> 247             format_dict, md_dict = self.compute_format_data(result)
248             self.write_format_data(format_dict, md_dict)
249             self.update_user_ns(result)

/home/sageserver/sage/ipython/IPython/core/displayhook.pyc in compute_format_data(self, result)
155
156         """
--> 157         return self.shell.display_formatter.format(result)
158
159     def write_format_data(self, format_dict, md_dict=None):

/home/sageserver/sage/ipython/IPython/core/formatters.pyc in format(self, obj, include, exclude)
150             md = None
151             try:
--> 152                 data = formatter(obj)
153             except:
154                 # FIXME: log the exception

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/misc/displayhook.pyc in __call__(self, obj)
503             sage: fmt.set_display('simple')
504         """
--> 505         if self.try_format_graphics(obj):
506             return ''
507         s = self.try_format_obj(obj)

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/misc/displayhook.pyc in try_format_graphics(self, obj)
400         from sage.structure.sage_object import SageObject
401         if isinstance(obj, SageObject) and hasattr(obj, '_graphics_'):
--> 402             return obj._graphics_()
403         return False
404

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/plot/plot3d/base.so in sage.plot.plot3d.base.Graphics3d._graphics_ (sage/plot/plot3d/base.c:3233)()

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/plot/plot3d/base.so in sage.plot.plot3d.base.Graphics3d.show (sage/plot/plot3d/base.c:12029)()

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/plot/plot3d/base.so in sage.plot.plot3d.base.Graphics3d._prepare_for_jmol (sage/plot/plot3d/base.c:8064)()

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/plot/plot3d/base.so in sage.plot.plot3d.base.Graphics3d._box_for_aspect_ratio (sage/plot/plot3d/base.c:8679)()

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/plot/plot3d/base.so in sage.plot.plot3d.base.Graphics3d._safe_bounding_box (sage/plot/plot3d/base.c:4275)()

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/plot/plot3d/parametric_surface.so in sage.plot.plot3d.parametric_surface.ParametricSurface.bounding_box (sage/plot/plot3d/parametric_surface.c:3625)()

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/plot/plot3d/parametric_surface.so in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:4228)()

/home/sageserver/sage/local/lib/python2.7/site-packages/sage/plot/plot3d/parametric_surface.so in sage.plot.plot3d.parametric_surface ...
more

1

That's because you are running x from -2 to 2. And the log(..) term returns a complex number. You have to fix your range first so that you get real numbers out of your expression.

( 2013-12-22 21:06:16 -0500 )edit