First time here? Check out the FAQ!

Ask Your Question
1

Fast numerical plot command that always works?

asked 13 years ago

jk gravatar image

updated 13 years ago

I typically encounter expressions of the type

 C = -I*sqrt(pi)*x*e^(-1/4*x^2)
 Abs = abs(C)

Sage's built-in plot commands fails:

verbose 0 (4101: plot.py, generate_plot_points) WARNING: When plotting,
failed to evaluate function at 200 points.
verbose 0 (4101: plot.py, generate_plot_points) Last error message:
'unable to simplify to float approximation'

We can evaluate the expression numerically:

X = map(lambda x: x/10, range(-100,100))
Y = map(lambda xx: Abs.subs(x=xx).n(), X)
Yreal = map(real, Y)
Yimag = map(imag, Y)
list_plot(zip(X,Yreal)) + list_plot(zip(X,Yimag),color='red')

This works, but looks like a dirty workaround. Should I use numpy arrays and fast_callable? Or use fast_callable() without numpy?

Here is an example worksheet.

Preview: (hide)

Comments

Can you give a complete example (e.g., give us a C function) illustrating the problem? I'm sure we'll be able to help you better then. LIkely, using fast_callable will solve your issue.

Jason Grout gravatar imageJason Grout ( 13 years ago )

@Jason Grout, I update the question with an example and a link to a public worksheet. In this simple example, simplify() would solve the issue, but on more sophisticated expressions simplify will fail.

jk gravatar imagejk ( 13 years ago )

1 Answer

Sort by » oldest newest most voted
2

answered 13 years ago

Jason Grout gravatar image

I would probably do it like this:

C = -I*sqrt(pi)*x*e^(-1/4*x^2)
f=fast_callable(abs(C),vars=[x],domain=CC)
plot(f, (-10,10))
Preview: (hide)
link

Comments

Thank You @Jason Grout. This methods works, but only if `domain=CC` is specified explicitly. Mind to explain?

jk gravatar imagejk ( 13 years ago )

domain=CC means that the problem is evaluated and at each step of the evaluation, the number is converted to the complex numbers (CC). That lets you do the calculation using I. You can specify other domains like RR (real numbers), RDF (machine real numbers), etc. I think the original problem is that the default is to do fast_callable with domain RDF, which means that the intermediate steps have to be real numbers.

Jason Grout gravatar imageJason Grout ( 13 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 13 years ago

Seen: 1,203 times

Last updated: Oct 19 '11