Ask Your Question
0

interact variable not found

asked 2020-12-13 14:05:35 -0600

cybervigilante gravatar image

I keep getting ValueError: Variable 'h' not found when I try to plot an interact:

    f = (x-h)^2 - 4*a*(y-k)
    Parabola = solve(f,y)[0].rhs()
    print(Parabola)
    @interact
    def _(h=slider(-5,5,default=1), a=slider(-5,5,default=1), k=slider(-5,5,default=1)):
            show(plot(Parabola,x,-5,5))

It even creates the sliders but the plot can't see h. What am I doing wrong? print(Parabola)image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-12-13 16:53:21 -0600

Juanjo gravatar image

updated 2020-12-13 16:54:12 -0600

This works for me:

var("a,h,k,x,y")
f = (x-h)^2 - 4*a*(y-k)
Parabola = y.subs(solve(f,y))
print(Parabola)
@interact
def _(h=slider(-5,5,default=1), a=slider(-5,5,default=1),
      k=slider(-5,5,default=1)):
    if a==0: 
        print("Invalid value for a. Please, select another value")
    else:
        show(plot(Parabola(a=a,h=h,k=k),x,-5,5))

Parabola is a symbolic expression which depends on x, but also on a, h and k. You must explicitly substitute the values of these variables in Parabola before plotting it as a function of x. Please, also note the alternative way to define Parabola and the caution that should be taken in the case a=0.

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-12-13 14:05:35 -0600

Seen: 28 times

Last updated: Dec 13 '20