Ask Your Question

Revision history [back]

Since the imaginary parts you get are just tiny error terms, the best is probably to ignore them by selecting the real part, as you understood.

The problem you are getting is that Sage's floating point numbers don't behave exactly like floats, in particular when raising negative numbers to fractional powers.

The "plot" command uses Python floats, not Sage "RealNumber"s.

So the trick with your definition of ev, would be to plot as follows:

sage: plot(lambda x: ev.subs(u=RR(x)).real(), (2.6, 3))

Since the imaginary parts you get are just tiny error terms, the best is probably to ignore them by selecting the real part, as you understood.

The problem you are getting is that Sage's floating point numbers don't behave exactly like floats, in particular when raising negative numbers to fractional powers.

The "plot" command uses Python floats, not Sage "RealNumber"s.

So the trick with your definition of ev, would be to plot as follows:

sage: plot(lambda x: ev.subs(u=RR(x)).real(), (2.6, 3))

To pinpoint where the error occurs, observe the difference between:

sage: (-1.0)^(1/3)
0.500000000000000 + 0.866025403784439*I

sage: RR(-1)^(1/3)
0.500000000000000 + 0.866025403784439*I

sage: RealNumber(-1)^(1/3)
0.500000000000000 + 0.866025403784439*I

and

sage: float(-1)^(1/3)
Traceback (most recent call last)
...
ValueError: negative number cannot be raised to a fractional power

sage: RDF(-1)^(1/3)
Traceback (most recent call last)
...
ValueError: negative number cannot be raised to a fractional power