Ask Your Question
0

some questions about @interact

asked 2020-09-25 03:12:19 -0500

Cyrille gravatar image

the following plot works nicely

var('x, z') # ne pas oublier de définir une variable avant de l'utiliser.
f(x) = (3/2) - (1/2)*x
g(x) = (11/2) - 2*x
h(x) = -1 + x
gr = plot(f(x), (x, 0, 3),thickness=1.5, color="#4b59ea",legend_label='$f(x)=\\frac{3}{2} - \\frac{1}{2}x$')
gr+= plot(g(x), (x, 0, 3),thickness=1.5, color="#e52411",legend_label='$g(x)=\\frac{11}{2} - 2x$')
gr+= plot(h(x), (x, 0, 3),thickness=1.5, color="#2ea012",legend_label='$h(x)=-1 + x$')
gr.axes_labels(['$x_1$','$x_2$'])
fs1=solve(f(x) == h(x), x)
fs2=solve(f(x) == g(x), x)
fs3=solve(f(x) == 0, x)
gs1=solve(g(x) == h(x), x)
gs2=solve(g(x) == f(x), x)
gs3=solve(g(x) == 0, x)
hs1=solve(h(x) == 0,x)
pint=[(0,f(0)),(fs1[0].rhs(),f(fs1[0].rhs())),(fs2[0].rhs(),f(fs2[0].rhs())),(fs3[0].rhs(),f(fs3[0].rhs())),(0,g(0)),(gs1[0].rhs(),g(gs1[0].rhs())),(gs2[0].rhs(),g(gs2[0].rhs())),
      (gs3[0].rhs(),g(gs3[0].rhs())),(0,h(0)),(hs1[0].rhs(),h(hs1[0].rhs()))]
c=[circle(x, .05,fill=true,edgecolor='#ff9933', facecolor='#ff9933') for x in pint]
poly=polygon2d([(hs1[0].rhs(),h(hs1[0].rhs())),(fs1[0].rhs(),f(fs1[0].rhs())),(gs2[0].rhs(),g(gs2[0].rhs())),(gs3[0].rhs(),g(gs3[0].rhs()))],fill=true,rgbcolor=(.75,.75,.75))
#show(pint)
z1=solve(f(gs2[0].rhs())==z - (0.3333) *gs2[0].rhs(),z)[0].rhs()
X=RealSet(z1-0.1, z1+0.1)
@interact
def _(z=slider(z1-(5*0.1),z1+(5*0.1),step_size=0.333333)):
    if z not in X:
        zplot=plot(z - (0.3333) *x, (x, 0,5),color="#4b26ea",linestyle='-.', axes_labels=['$x_1$', '$x_2$'])
        l1= line([(fs1[0].rhs(),0),(fs1[0].rhs(),f(fs1[0].rhs())),(0,f(fs1[0].rhs()))],color="#585858",linestyle='--')
        t1= text("$x_1^\\star$",(fs1[0].rhs(),-.20)) 
        t2= text("$x_2^\\star$",(-.2,f(fs1[0].rhs())))
        show(sum(c)+gr+poly+zplot+l1+t1+t2,figsize=8)
    elif z in X:
        zplot=plot(z - (0.3333) *x, (x, 0,5),color="#4b26ea",linestyle='--')
        show(sum(c)+gr+poly+zplot,figsize=8)

But it is not what I am expecting. I want that the dashed lines and the $x_i$'s apeear only at the optimum (yellow circle). I have tried many instance and the following one but I do not obtain what I expect.

var('x, z') # ne pas oublier de définir une variable avant de l'utiliser.
f(x) = (3/2) - (1/2)*x
g(x) = (11/2) - 2*x
h(x) = -1 + x
gr = plot(f(x), (x, 0, 3),thickness=1.5, color="#4b59ea",legend_label='$f(x)=\\frac{3}{2} - \\frac{1}{2}x$')
gr+= plot(g(x), (x, 0, 3),thickness=1.5, color="#e52411",legend_label='$g(x)=\\frac{11}{2} - 2x$')
gr+= plot(h(x), (x, 0, 3),thickness=1.5, color="#2ea012",legend_label='$h(x)=-1 + x$')
gr.axes_labels(['$x_1$','$x_2$'])
fs1=solve(f(x) == h(x), x)
fs2=solve(f(x) == g(x), x)
fs3=solve(f(x) == 0, x)
gs1=solve(g(x) == h(x), x)
gs2=solve(g(x) == f(x), x)
gs3=solve(g(x) == 0, x)
hs1=solve(h(x) == 0,x)
pint=[(0,f(0)),(fs1[0].rhs(),f(fs1[0].rhs())),(fs2[0].rhs(),f(fs2[0].rhs())),(fs3[0].rhs(),f(fs3[0].rhs())),(0,g(0)),(gs1[0].rhs(),g(gs1[0].rhs())),(gs2[0].rhs(),g(gs2[0].rhs())),
      (gs3[0].rhs(),g(gs3[0].rhs())),(0,h(0)),(hs1[0].rhs(),h(hs1[0].rhs()))]
c=[circle(x, .05,fill=true,edgecolor='#ff9933', facecolor='#ff9933') for x in pint]
poly=polygon2d([(hs1[0].rhs(),h(hs1[0].rhs())),(fs1[0].rhs(),f(fs1[0].rhs())),(gs2[0].rhs(),g(gs2[0].rhs())),(gs3[0].rhs(),g(gs3[0].rhs()))],fill=true,rgbcolor=(.75,.75,.75))
#show(pint)
z1=solve(f(gs2[0].rhs())==z - (0.3333) *gs2[0].rhs(),z)[0].rhs()
X=RealSet(z1-0.1, z1+0.1)
@interact
def _(z=slider(z1-(5*0.1),z1+(5*0.1),step_size=0.333333)):
    if z not in X:
        zplot=plot(z - (0.3333) *x, (x, 0,5),color="#4b26ea",linestyle='--')
        show(sum(c)+gr+poly+zplot,figsize=8)
    elif z in X:
        zplot=plot(z - (0.3333) *x, (x, 0,5),color="#4b26ea",linestyle='-.', axes_labels=['$x_1$', '$x_2$'])
        l1= line([(fs1[0].rhs(),0),(fs1[0].rhs(),f(fs1[0].rhs())),(0,f(fs1[0].rhs()))],color="#585858",linestyle='--')
        t1= text("$x_1^\\star$",(fs1[0].rhs(),-.20)) 
        t2= text("$x_2^\\star$",(-.2,f(fs1[0].rhs())))
        show(sum(c)+gr+poly+zplot+l1+t1+t2,figsize=8)

Can I put an expression mixing text and computation like with LatexExpr in a plot ?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2020-09-25 05:29:20 -0500

Cyrille gravatar image
Here is a not so nice solution to my question (it was not the fault of @interact).

var('x, z') # ne pas oublier de définir une variable avant de l'utiliser.
f(x) = (3/2) - (1/2)*x
g(x) = (11/2) - 2*x
h(x) = -1 + x
gr = plot(f(x), (x, 0, 3),thickness=1.5, color="#4b59ea",legend_label='$f(x)=\\frac{3}{2} - \\frac{1}{2}x$')
gr+= plot(g(x), (x, 0, 3),thickness=1.5, color="#e52411",legend_label='$g(x)=\\frac{11}{2} - 2x$')
gr+= plot(h(x), (x, 0, 3),thickness=1.5, color="#2ea012",legend_label='$h(x)=-1 + x$')
gr.axes_labels(['$x_1$','$x_2$'])
fs1=solve(f(x) == h(x), x)
fs2=solve(f(x) == g(x), x)
fs3=solve(f(x) == 0, x)
gs1=solve(g(x) == h(x), x)
gs2=solve(g(x) == f(x), x)
gs3=solve(g(x) == 0, x)
hs1=solve(h(x) == 0,x)
pint=[(0,f(0)),(fs1[0].rhs(),f(fs1[0].rhs())),(fs2[0].rhs(),f(fs2[0].rhs())),(fs3[0].rhs(),f(fs3[0].rhs())),(0,g(0)),(gs1[0].rhs(),g(gs1[0].rhs())),(gs2[0].rhs(),g(gs2[0].rhs())),
      (gs3[0].rhs(),g(gs3[0].rhs())),(0,h(0)),(hs1[0].rhs(),h(hs1[0].rhs()))]
c=[circle(x, .05,fill=true,edgecolor='#ff9933', facecolor='#ff9933') for x in pint]
poly=polygon2d([(hs1[0].rhs(),h(hs1[0].rhs())),(fs1[0].rhs(),f(fs1[0].rhs())),(gs2[0].rhs(),g(gs2[0].rhs())),(gs3[0].rhs(),g(gs3[0].rhs()))],fill=true,rgbcolor=(.75,.75,.75))
#show(pint)
z1=solve(f(gs2[0].rhs())==z - (0.3333) *gs2[0].rhs(),z)[0].rhs()
X=RealSet(1.-0.1, 2+0.1)
@interact
def _(z=slider(z1-(5*0.1),z1+(5*0.1),step_size=0.333333)):
    if z not in X:
        zplot=plot(z - (0.3333) *x, (x, 0,5),color="#4b26ea",linestyle='--')
        show(sum(c)+gr+poly+zplot,figsize=8)
    elif z in X:
        zplot=plot(z - (0.3333) *x, (x, 0,5),color="#4b26ea",linestyle='-.', axes_labels=['$x_1$', '$x_2$'])
        l1= line([(fs1[0].rhs(),0),(fs1[0].rhs(),f(fs1[0].rhs())),(0,f(fs1[0].rhs()))],color="#585858",linestyle='--')
        t1= text("$x_1^\\star$",(fs1[0].rhs(),-.20)) 
        t2= text("$x_2^\\star$",(-.2,f(fs1[0].rhs())))
        show(sum(c)+gr+poly+zplot+l1+t1+t2,figsize=8)
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-09-25 03:12:19 -0500

Seen: 39 times

Last updated: Sep 25