Ask Your Question
0

Programming an interaction

asked 2020-04-10 10:18:06 +0200

Cyrille gravatar image

updated 2020-04-10 11:17:31 +0200

FrédéricC gravatar image

Certainly due to my lack of competence this code

  pol = polygon([(0,0), (0,10), (10,10), (0,0)], fill=True,  rgbcolor=(0.81,0.81,0.81)) 
  g = plot(x,(0,10),color="blue",ymin=0,ymax=10)
  @interact
  def _(c=slider(0,1,step_size=0.2,default=.5),inac=checkbox(True,"Domaine Inaccessible")):
      a = solve(-c*x+6==x, x)
      aa = a[0].right()
      h = plot(-c*x+6,(aa, 6),color="green",ymin=0,ymax=10)
      graph = h+g 
      if inac :
          graph += pol    
       show(graph)

conplains that graph is not defined.

Is there some thing I have not understood in the construction of @interact?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2020-04-10 18:33:59 +0200

Juanjo gravatar image

updated 2020-04-10 20:08:32 +0200

Perhaps the problem came from indentation, that I have had to adjust a bit. Try this:

pol = polygon([(0,0), (0,10), (10,10), (0,0)], fill=True, rgbcolor=(0.81,0.81,0.81)) 
g = plot(x, (0,10), color="blue")
@interact
def _(c=slider(0.01,1, step_size=0.2, default=.5), 
      inac=checkbox(True,"Domaine Inaccessible")):
    a = x.subs(solve(-c*x+6==x, x))
    h = plot(-c*x+6, (a,6), color="green")
    graph = h+g 
    if inac:
        graph += pol    
    show(graph, aspect_ratio=1, ymin=0,ymax=10)

By the way, the code failed if c=0, as I already pointed in other question, and the plot didn't keep proportions when checking or unchecking inac. I've modified the code to deal with these problems.

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: 2020-04-10 10:18:06 +0200

Seen: 535 times

Last updated: Apr 10 '20