Ask Your Question
0

Using interact

asked 2015-08-26 12:32:31 -0500

MLainz gravatar image

updated 2015-08-26 13:22:00 -0500

I want to visualize these functions in the complex plane:

$$f_n(z)=\exp(z) - \sum_{k=0}^{n} \frac{z^k}{k!}$$

I tried this code, but it gives me an error.

z,k = var('z,k')
@interact
    def _(n=(1..8)):
    complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))

I am new to Sage (I previously used Mathematica). I wrote the code based on this example: http://wiki.sagemath.org/interact/ . How can I fix that?

EDIT: I would also like to know why doesn't this work either

def myPlot(n):
   complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))

Then I type myPlot(2) in the notebook, but I get nothing. However, if I type:

complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, 2), (-5, 5), (-5, 5))

I get the correct plot.

EDIT 2:

I tried

myPlot = lambda n: complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))

And now I can evaluate it. However, I still can't use @Interact.

This doesn't make sense to me.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-08-26 14:04:52 -0500

eric_g gravatar image

Hi,

Basically, you have to use the function show() on the graphic object resulting from complex_plot(). Note also the alignment of @interact and def _(n=(1..8)) (alignment is crucial in Python). So the working code is

z,k = var('z,k')
@interact
def _(n=(1..8)):
    g = complex_plot(exp(z)- sum(z^k/factorial(k), k, 0, n), (-5, 5), (-5, 5))
    show(g)
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-08-26 12:32:31 -0500

Seen: 83 times

Last updated: Aug 26 '15