abs and plot problem

asked 2019-10-22 16:33:38 +0200

cedrik0s gravatar image

Hi there. Using Cocalc, I am encountering problems trying to plot some functions. The following simple example shows how I am able to plot the modulus of a simple function using my own modulus function, but how it fails using abs(). Which is really weird.

_ = var('a,x')

MODULUS(x) = sqrt(real(x)^2 + imag(x)^2)

Hmod = MODULUS(ax + ix^2)

Habs = abs(ax + ix^2)

plot([Hmod(a=n) for n in [1..5]], (x,0, 2))

plot([Habs(a=n) for n in [1..5]], (x,0, 2))

Hmod can be plotted, while Habs will return "TypeError: unable to coerce to a real number".

On the other hand the following works:

plot(Habs(a=1), (x,0, 2))

So it works without list comprehension and with my MODULUS function... Don't know what to think of that.

Anyone can help on that? I have seriously more complicated cases that I seem to be unable to plot because of this very problem. Thanks a lot.

edit retag flag offensive close merge delete


Strange. This works

sage: plot([Habs(a=0) for n in range(1,3)],(x,0,2))
Launched png viewer for Graphics object consisting of 2 graphics primitives

but this fails

sage: plot([Habs(a=1) for n in range(1,3)],(x,0,2))
FrédéricC gravatar imageFrédéricC ( 2019-10-22 17:43:17 +0200 )edit

@FredericC: yes that's really weird, and it actually looks a bit random. I have had your second line work some time and others not... but I can't figure what to do to make it work (or not) systematically...

cedrik0s gravatar imagecedrik0s ( 2019-10-22 18:04:34 +0200 )edit