simple definition

30 views
Skip to first unread message

valer...@gmail.com

unread,
Aug 27, 2017, 8:35:59 AM8/27/17
to sage-support
I would like to understand why this definition of the slope of the secant line does not allow me to define the tangent line at a=1, even though it works if I use it by itself:

f(x)=x^2

def sl(a):
    if a!=1:
        m=(f(a)-f(1))/(a-1)
    else:
        m=f.diff(x)(1)
    return m

sl(1)
   2

L(a,x)=f(a)+sl(a)*(x-a)

L(1,x)
ValueError: power::eval(): division by zero

Thank you for any help.

Simon King

unread,
Aug 27, 2017, 9:17:34 AM8/27/17
to sage-s...@googlegroups.com
Hi Valerio,

On 2017-08-27, valer...@gmail.com <valer...@gmail.com> wrote:
> L(a,x)=f(a)+sl(a)*(x-a)
>
> L(1,x)
> ValueError: power::eval(): division by zero

The above syntax, as innocent as it looks, implies a lot of things,
via Sage's preparser:

sage: preparse("L(a,x) = f(a)+sl(a)*(x-a)")
'__tmp__=var("a,x"); L = symbolic_expression(f(a)+sl(a)*(x-a)).function(a,x)'

So, what exactly is happening here? Two symbolic variables are
created, and a symbolic function is created, whose definition
on the *result* of evaluating f(a)+sl(a)*(x-a).

And since `a` is just a symbolic variable at that point, which evaluates
unequal to one, sl(a) returns a fraction:

sage: L(a,x) = f(a)+sl(a)*(x-a)
sage: L(a,x)
a^2 - (a^2 - 1)*(a - x)/(a - 1)

Apparently, inserting a=1 results in a division by zero.

Solution: Instead of invoking Sage's preparser and using a symbolic
function, just create a Python function, either like this:
sage: L = lambda a,x:f(a)+sl(a)*(x-a)
sage: L(1,x)
2*x - 1
or like this:
sage: def L(a,x): return f(a)+sl(a)*(x-a)
sage: L(1,x)
2*x - 1

Best regards,
Simon

Simon King

unread,
Aug 27, 2017, 9:32:34 AM8/27/17
to sage-s...@googlegroups.com
PS:

On 2017-08-27, Simon King <simon...@uni-jena.de> wrote:
> So, what exactly is happening here? Two symbolic variables are
> created, and a symbolic function is created, whose definition
> on the *result* of evaluating f(a)+sl(a)*(x-a).

I omitted a word: "... whose definition *depends* on the result..."


valer...@gmail.com

unread,
Aug 27, 2017, 2:51:13 PM8/27/17
to sage-support
Thank you Simon for the clear explanation
Reply all
Reply to author
Forward
0 new messages