Solve for a function defined in a file?

74 views
Skip to first unread message

David Ingerman

unread,
Jun 10, 2014, 2:41:59 AM6/10/14
to sage-s...@googlegroups.com
 
 How  to solve([f(x)==0],x) for a function "f(x)" defined in a .sage file?

 The error message: TypeError: Cannot evaluate symbolic expression to a numeric value.

Thank you...

Dima Pasechnik

unread,
Jun 11, 2014, 4:50:10 AM6/11/14
to sage-s...@googlegroups.com
what is f(x) ?
solve() won't work for a Python function, it needs a symbolic
expression, e.g.

sage: type(sin(x))
<type 'sage.symbolic.expression.Expression'>
sage:

>
> Thank you...
>

David Ingerman

unread,
Jun 19, 2014, 11:33:43 PM6/19/14
to sage-s...@googlegroups.com
 Thank you, so what to do for Python function? Matlab had general purpose 'optim(f)' if my memory is right... 

Dima Pasechnik

unread,
Jun 20, 2014, 5:10:38 AM6/20/14
to sage-s...@googlegroups.com
On 2014-06-20, David Ingerman <davidd...@gmail.com> wrote:
> Thank you, so what to do for Python function? Matlab had general purpose
> 'optim(f)' if my memory is right...

you can e.g. use find_root(); this is a numerical thing that accepts
Python functions. Here is an example:

sage: def f(x):
return x-cos(x)
....:
sage: find_root(f,0,1)
0.7390851332151559
sage:

>
> On Wednesday, June 11, 2014 1:50:10 AM UTC-7, Dima Pasechnik wrote:
>>

David Ingerman

unread,
Jun 21, 2014, 2:51:57 AM6/21/14
to sage-s...@googlegroups.com
 Thank you, that's helpful. Is there a way to get all roots of a Python function on an interval? 

Dima Pasechnik

unread,
Jun 21, 2014, 4:21:06 AM6/21/14
to sage-s...@googlegroups.com
On 2014-06-21, David Ingerman <davidd...@gmail.com> wrote:
> Thank you, that's helpful. Is there a way to get all roots of a Python
> function on an interval?

I never heard of robust procedures for such a task, and doubt they are
even possible (think about roots of sin(1/x) on [0,1]).
Certainly you can partition the interval into pieces
and try finding root in each subinterval where the function has
different signs on its ends.



scipy has a variety of root-finding methods implemented. You
can do
sage: import scipy.optimize
and then read on
sage: scipy.optimize.brentq?
(the method called by Sage's find_root)


>
> On Friday, June 20, 2014 2:10:38 AM UTC-7, Dima Pasechnik wrote:
>>

David Ingerman

unread,
Jun 21, 2014, 11:25:26 PM6/21/14
to sage-s...@googlegroups.com
 Thank you, that makes sense. My Python function is not continuous though, has poles, so I'll probably have to plot it to find its zeros... 

Dima Pasechnik

unread,
Jun 22, 2014, 4:22:48 AM6/22/14
to sage-s...@googlegroups.com
On 2014-06-22, David Ingerman <davidd...@gmail.com> wrote:
> Thank you, that makes sense. My Python function is not continuous though,
> has poles, so I'll probably have to plot it to find its zeros...

you might perhaps try finding a pole p by solving 1/f(x)=0, and
regularise f by multiplying f with (x-p)i^m, for appropriate m.
Repeat until all the poles in the interval are taken care of.
Similarly you can divide f by (x-r)^k for a zero r. In effect this
amounts to finding a Pade approximation of f.
(no idea how well this behaves in practice)


>
> On Saturday, June 21, 2014 1:21:06 AM UTC-7, Dima Pasechnik wrote:
>>

David Ingerman

unread,
Jul 6, 2014, 5:58:40 PM7/6/14
to sage-s...@googlegroups.com
 Have zeros of order 2 too, so the sign change doesn't help in general, but may work for some zeros. Thank you!... 
Reply all
Reply to author
Forward
0 new messages