Ask Your Question
0

What's the syntax error in this code?

asked 2012-12-15 20:26:39 -0500

Hi4ko gravatar image

updated 2015-07-31 10:53:50 -0500

FrédéricC gravatar image

Hello I'm writing a program which outputs ,according to the user's choice, either Lissagous curve or beat.

Here's a code and I can't find my error because Sage on Android doesn't show where error is.

@interact
print "Choose 1 for Lissajous curve and 2 for beat"
def choise(v =(1..2)):
  if v==1:
    def lissa(delta = ContiniousSlider(interval=(-pi,pi)),a = (1..10),b = (1,10)):
      p = parametric_plot(cos(a*t+delta),cos(b*t),(t,0,5*pi))
      p.show()
  else
    def beat(a = ContiniousSlider(interval = (0,5)), omega = ContiniousSlider(interval = (0,5)),percent = ContiniousSlider(interval = (0,0.1))):
      domega = percent*omega;
      p = parametric_plot(2*a*cos(domega/2*t)*cos(omega*t),2*a*cos(domega/2*t),(t,0,5*pi))
      p.show()
edit retag flag offensive close merge delete

4 answers

Sort by » oldest newest most voted
4

answered 2012-12-15 21:24:13 -0500

ndomes gravatar image

updated 2012-12-15 23:04:06 -0500

I am afraid there is more than one syntax error ;-)

You forgot to declare t as variable.

@interact has to be followed by the definition of a function, not by print statement.

You have to repeat the @interact decorator for each interacting function.

missing ':' after else.

In parametric_plot( x(t), y(t) , ...) is wrong,

you need a tuple of functions parametric_plot( ( x(t), y(t) ), ...).

( My sage version (4.7.1) doesn't know ContiniousSlider ).

var('t')
print "Choose 1 for Lissajous curve and 2 for beat"
@interact
def choise(v =(1..2)):
  if v==1:
    @interact
    def lissa(delta =(-pi,pi),a = (1..10),b = (1,10)):
      p = parametric_plot((cos(a*t+delta),cos(b*t)),(t,0,5*pi))
      p.show()
  else:
    @interact
    def beat(a = (0,5), omega = (0,5),percent = (0,0.1)):
      domega = percent*omega;
      p = parametric_plot((2*a*cos(domega/2*t)*cos(omega*t),2*a*cos(domega/2*t)),(t,0,5*pi))
      p.show()
edit flag offensive delete link more
1

answered 2012-12-16 14:15:29 -0500

calc314 gravatar image

updated 2012-12-16 14:22:08 -0500

The following code produces the beats you are looking for.

var('t')
f1=110
f2=104
fs=(f1-f2)
fr=(f1+f2)/2
t1=2/fs
p = plot(a*cos(pi*fs*t)*sin(2*pi*fr*t),(t,0,t1),color='red')
p+=plot([a*cos(pi*fs*t),-a*cos(pi*fs*t)],(t,0,t1))
p.show()

image description

The code below produces an interact that is probably more like want you want. Note that the slider command lets you initialize the sliders.

var('t')
print "Choose 1 for Lissajous curve and 2 for beat"
@interact
def choise(v =(1..2)):
  if v==1:
    @interact
    def lissa(delta =slider(-pi,pi,0.1,1),a = slider(1,10,1,6),b = slider(1,10,1,2)):
      p = parametric_plot((cos(a*t+delta),cos(b*t)),(t,0,5*pi))
      p.show()
  else:
    @interact
    def beat(a = slider(0,5,1,1), f1=slider(0,150,10,110),f2=slider(0,150,10,100)):
        fs=(f1-f2)
        fr=(f1+f2)/2
        t1=2/fs
        p = plot(a*cos(pi*fs*t)*sin(2*pi*fr*t),(t,0,t1),color='red')
        p+=plot([a*cos(pi*fs*t),-a*cos(pi*fs*t)],(t,0,t1))
        p.show()

Here's a link to the code using the Sage single-cell server.

edit flag offensive delete link more
0

answered 2012-12-15 22:04:29 -0500

Hi4ko gravatar image

Sage can't plot beat right, I can't understand why.

I want to see plot like this , but my code shows me only graph, which has black line in a pic. But I want to have both graphs on my plot :)

edit flag offensive delete link more
0

answered 2012-12-15 21:08:11 -0500

ppurka gravatar image

@interact is a decorator to a function, not a print statement.

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

Stats

Asked: 2012-12-15 20:26:39 -0500

Seen: 312 times

Last updated: Dec 16 '12