You can do
def Y(t):
return 2500+numerical_integral(S(u)-R(u),0,t)[0]
but then it won't be a symbolic object. (It will be a Python function.)
- Robert
How come n(integrate(some expression that doesn't integrate, x, 0, 1))
doesn't return the result of numerical_integral(same expression, 0, 1) ?
It seems that would solve the problem, as then you would just call
integrate normally in the above expression, so Y(t) would generally
return an unevaluated integral, and then you would just do n(Y(4)) to
get the numerical approximation given by numerical_integral.
Example:
sage: var('t')
t
sage: assume(t>0)
sage: a=(integrate(sin(cos(x)), 0, t)).subs(t=4)
sage: a
integrate(sin(cos(x)), x, 0, 4)
sage: n(a) # Why doesn't this return the result of numerical_integral?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/jason/.sage/temp/littleone/29880/_home_jason__sage_init_sage_0.py
in <module>()
/home/jason/sage/local/lib/python2.5/site-packages/sage/misc/functional.pyc
in numerical_approx(x, prec, digits)
765 prec = int((digits+1) * 3.32192) + 1
766 try:
--> 767 return x.numerical_approx(prec)
768 except AttributeError:
769 from sage.rings.complex_double import
is_ComplexDoubleElement
/home/jason/sage/local/lib/python2.5/site-packages/sage/calculus/calculus.pyc
in numerical_approx(self, prec, digits)
1514 except TypeError:
1515 # try to return a complex result
-> 1516 approx = self._complex_mpfr_field_(ComplexField(prec))
1517
1518 return approx
/home/jason/sage/local/lib/python2.5/site-packages/sage/calculus/calculus.pyc
in _complex_mpfr_field_(self, field)
1748
1749 def _complex_mpfr_field_(self, field):
-> 1750 raise TypeError
1751
1752 def _complex_double_(self, C):
TypeError:
sage: numerical_integral(sin(cos(x)), 0, 4)
(-0.6589735634713888, 2.714926799945442e-14)
Jason
> On Mar 26, 7:16 pm, Robert Bradshaw <rober...@math.washington.edu>
> wrote:
>> def Y(t):
>> return 2500+numerical_integral(S(u)-R(u),0,t)[0]
>>
>> but then it won't be a symbolic object. (It will be a Python
>> function.)
>
> Wait. What is the difference between a "symbolic object" and a
> "Python function" ?
A symbolic object can be manipulated, reasoned about, and has a bunch
of methods attached.
sage: f(x) = x^3-x
sage: f + 1
x |--> x^3 - x + 1
sage: f.integrate(x)
x |--> x^4/4 - x^2/2
...
A Python function can just be called (pretty much). It's just a chunk
of executable code (for example, could include loops, branching, look
stuff up in a file/online, etc.).
sage: def f(x): x^3-x
....:
sage: f.integrate()
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
AttributeError: 'function' object has no attribute 'integrate'
> Not sure whey the "def" way works but not the "Y(t) = ..." way.
The body of a function is not executed until it is called. In the
latter case, when you call Y(3) the "t" is specialized at 3 and the
numerical integration actually can evaluate at a given point.
- Robert
We already have plenty of approximation functions attached to the
symbolic functions. I think it's extremely valuable. For example, it's
very handy for plotting things.
sage: sin(1)
sin(1)
sage: sin(1).numerical_approx()
0.841470984807897
In fact, the first thing n() tries is the numerical_approx() method of
the object. I think all we have to do is define an
integral.numerical_approx() function that returns the results of
numerical_integral()
Jason
Um, duh, that's where the TypeError occurs above. Okay, I guess I
meant: one should just fix the numeric_approx function for integral...
Jason