SharedSage Intros / Numerical Integration.ipynbOpen in CoCalc
Author: John Hoggard
In [5]:
numerical_integral(e^(x^2), 0, 1)
(1.4626517459071817, 1.6238696453143372e-14)
In [6]:
f(x) = x^2
In [12]:
vf = piecewise([[(0,4),f]])
In [13]:
vf.trapezoid(4)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-13-62abbe355e45> in <module>() ----> 1 vf.trapezoid(Integer(4)) /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression._eval_on_operands.new_f (build/cythonized/sage/symbolic/expression.cpp:73036)() 12682 new_args = list(ex._unpack_operands()) 12683 new_args.extend(args) > 12684 return f(ex, *new_args, **kwds) 12685 return new_f 12686 /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/functions/piecewise.pyc in trapezoid(self, parameters, variable, N) 1013 x0 = a+i*h 1014 x1 = a+(i+1)*h -> 1015 rsum += func(x0, x1) 1016 return piecewise(rsum) 1017 /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/functions/piecewise.pyc in func(x0, x1) 1001 """ 1002 def func(x0, x1): -> 1003 f0, f1 = self(x0), self(x1) 1004 return [[(x0,x1), f0 + (f1-f0) * (x1-x0)**(-1) 1005 * (self.default_variable()-x0)]] /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression._eval_on_operands.new_f (build/cythonized/sage/symbolic/expression.cpp:73036)() 12682 new_args = list(ex._unpack_operands()) 12683 new_args.extend(args) > 12684 return f(ex, *new_args, **kwds) 12685 return new_f 12686 /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/functions/piecewise.pyc in __call__(self, parameters, variable, value, **kwds) 452 if value is not None: 453 substitution[variable] = value --> 454 return self.subs(substitution) 455 456 def _fast_float_(self, *args): /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.substitute (build/cythonized/sage/symbolic/expression.cpp:32803)() 5297 sig_on() 5298 try: -> 5299 res = self._gobj.subs_map(smap, 0) 5300 finally: 5301 sig_off() /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/sage/functions/piecewise.pyc in _subs_(self, subs_map, options, parameters, x) 233 if domain.contains(point): 234 return subs_map.apply_to(func, 0) --> 235 raise ValueError('point {} is not in the domain'.format(point)) 236 237 @staticmethod ValueError: point 0 is not in the domain
In [14]:
?trapezoid
Object `trapezoid` not found.
In [15]:
?vf.trapezoid
Signature: vf.trapezoid(*args, **kwds) Docstring: Return the piecewise line function defined by the trapezoid rule for numerical integration based on a subdivision of each domain interval into N subintervals. EXAMPLES: sage: f = piecewise([[[0,1], x^2], [RealSet.open_closed(1,2), 5-x^2]]) sage: f.trapezoid(2) piecewise(x|-->1/2*x on (0, 1/2), x|-->3/2*x - 1/2 on (1/2, 1), x|-->7/2*x - 5/2 on (1, 3/2), x|-->-7/2*x + 8 on (3/2, 2); x) sage: f = piecewise([[[-1,1], 1-x^2]]) sage: f.trapezoid(4).integral(definite=True) 5/4 sage: f = piecewise([[[-1,1], 1/2+x-x^3]]) ## example 3 sage: f.trapezoid(6).integral(definite=True) 1 Init docstring: x.__init__(...) initializes x; see help(type(x)) for signature File: ~/Sage Intros/sage/symbolic/expression.pyx Type: instancemethod
In [19]:
?vf.riemann_sum_integral_approximation
Object `vf.riemann_sum_integral_approximation` not found.
In [22]:
?vf.riemann*
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/IPython/core/oinspect.pyc in psearch(self, pattern, ns_table, ns_search, ignore_case, show_all) 1006 namespaces_seen.add(id(ns)) 1007 tmp_res = list_namespace(ns, type_pattern, filter, -> 1008 ignore_case=ignore_case, show_all=show_all) 1009 search_result.update(tmp_res) 1010 /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/IPython/utils/wildcard.pyc in list_namespace(namespace, type_pattern, filter, ignore_case, show_all) 105 results = {} 106 for name, obj in iteritems(filtered): --> 107 ns = list_namespace(dict_dir(obj), type_pattern, 108 ".".join(pattern_list[1:]), 109 ignore_case=ignore_case, show_all=show_all) /ext/sage/sage-8.3_1804/local/lib/python2.7/site-packages/IPython/utils/wildcard.pyc in dict_dir(obj) 69 # thing. In particular, Traits use this pattern 70 try: ---> 71 ns[key] = getattr(obj, key) 72 except AttributeError: 73 pass TypeError: descriptor '__weakref__' for 'EvaluationMethods' objects doesn't apply to 'Expression_with_dynamic_methods' object