# What's the syntax error in this code? 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 close merge delete

Sort by » oldest newest most voted 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()

more

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() 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.

more

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 :)

more

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

more