Ask Your Question

Revision history [back]

The test whether z == z1 is too sensitive since the values are floating point numbers.

Replace the test by some approximate equality test, such as abs(z - z1) < 1e-2.

Here is a rewrite implementing that change (and a little polishing).

f(x) = (3/2) - (1/2)*x
g(x) = (11/2) - 2*x
h(x) = -1 + x
opt = {'thickness': 1.5}
xx = (x, 0, 3)
gr = plot(f(x), xx, color="#4b59ea", legend_label=r'$y = f(x)$', **opt)
gr += plot(g(x), xx, color="#e52411", legend_label=r'$y = g(x)$', **opt)
gr += plot(h(x), xx, color="#2ea012", legend_label=r'$y = h(x)$', **opt)
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)
xf = [0, fs1[0].rhs(), fs2[0].rhs(), fs3[0].rhs()]
xg = [0, gs1[0].rhs(), gs2[0].rhs(), gs3[0].rhs()]
xh = [0, hs1[0].rhs()]
pts = [(x, F(x)) for F, xF in [(f, xf), (g, xg), (h, xh)] for x in xF]
c = [circle(x, .05, fill=True, edgecolor='#ff9933', facecolor='#ff9933') for x in pts]
poly = polygon2d([(x, F(x))
                  for Fs, F in  [(hs1, h), (fs1, f), (gs2, g), (gs3, g)]
                  for x in [Fs[0].rhs()]],
                 fill=True, color=(.75, .75, .75))

# show(pts)

z1 = solve(f(gs2[0].rhs()) == x - 1/3 * gs2[0].rhs(), x)[0].rhs()


@interact
def _(z=slider(z1 - 0.5, z1 + 0.5, step_size=1/30.)):
    if (z - z1).abs() < 1e-2:  # consider z is at z1
        zplot = plot(z - 1/3 * x, (x, 0, 5), color="#4b26ea", linestyle='--')
        show(sum(c) + gr + poly + zplot, figsize=8)
    else:
        zplot = plot(z - 1/3 * x, (x, 0, 5), color="#4b26ea", linestyle='-.')
        show(sum(c) + gr + poly + zplot, figsize=8)

The test whether z == z1 is too sensitive since the values are for floating point numbers.values.

Replace the test it by some an approximate equality test, test such as abs(z - z1) < 1e-2.

Here is a rewrite implementing that change (and a little polishing).

f(x) = (3/2) - (1/2)*x
g(x) = (11/2) - 2*x
h(x) = -1 + x
opt = {'thickness': 1.5}
xx = (x, 0, 3)
gr = plot(f(x), xx, color="#4b59ea", legend_label=r'$y = f(x)$', legend_label=fr'$y = f(x) = {latex(f(x))}$', **opt)
gr += plot(g(x), xx, color="#e52411", legend_label=r'$y = g(x)$', legend_label=fr'$y = g(x) = {latex(g(x))}$', **opt)
gr += plot(h(x), xx, color="#2ea012", legend_label=r'$y = h(x)$', legend_label=fr'$y = h(x) = {latex(h(x))}$', **opt)
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)
xf = [0, fs1[0].rhs(), fs2[0].rhs(), fs3[0].rhs()]
xg = [0, gs1[0].rhs(), gs2[0].rhs(), gs3[0].rhs()]
xh = [0, hs1[0].rhs()]
pts = [(x, F(x)) for F, xF in [(f, xf), (g, xg), (h, xh)] for x in xF]
c = [circle(x, .05, fill=True, edgecolor='#ff9933', facecolor='#ff9933') for x in pts]
poly = polygon2d([(x, F(x))
                  for Fs, F in  [(hs1, h), (fs1, f), (gs2, g), (gs3, g)]
                  for x in [Fs[0].rhs()]],
                 fill=True, color=(.75, .75, .75))

# show(pts)

z1 = solve(f(gs2[0].rhs()) == x - 1/3 * gs2[0].rhs(), x)[0].rhs()
 print(f'z1 = {z1} = {z1.n()}', flush=True)

@interact
def _(z=slider(z1 - 0.5, z1 + 0.5, step_size=1/30.)):
    if (z - z1).abs() < 1e-2: 1e-8:  # consider z is at z1
        zplot = plot(z - 1/3 * x, (x, 0, 5), color="#4b26ea", linestyle='--')
        show(sum(c) + gr + poly + zplot, figsize=8)
    else:
        zplot = plot(z - 1/3 * x, (x, 0, 5), color="#4b26ea", linestyle='-.')
        show(sum(c) + gr + poly + zplot, figsize=8)

The test whether z == z1 is too sensitive for floating point values.

Replace it by an approximate equality test such as abs(z - z1) < 1e-2.

Here is a rewrite implementing that change (and a little polishing).

f(x) = (3/2) - (1/2)*x
g(x) = (11/2) - 2*x
h(x) = -1 + x
opt = {'thickness': 1.5}
xx = (x, 0, 3)
gr = plot(f(x), xx, color="#4b59ea", legend_label=fr'$y = f(x) = {latex(f(x))}$', **opt)
gr += plot(g(x), xx, color="#e52411", legend_label=fr'$y = g(x) = {latex(g(x))}$', **opt)
gr += plot(h(x), xx, color="#2ea012", legend_label=fr'$y = h(x) = {latex(h(x))}$', **opt)
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)
xf = [0, fs1[0].rhs(), fs2[0].rhs(), fs3[0].rhs()]
xg = [0, gs1[0].rhs(), gs2[0].rhs(), gs3[0].rhs()]
xh = [0, hs1[0].rhs()]
pts = [(x, F(x)) for F, xF in [(f, xf), (g, xg), (h, xh)] for x in xF]
c = [circle(x, .05, fill=True, edgecolor='#ff9933', facecolor='#ff9933') for x in pts]
poly = polygon2d([(x, F(x))
                  for Fs, F in  [(hs1, h), (fs1, f), (gs2, g), (gs3, g)]
                  for x in [Fs[0].rhs()]],
                 fill=True, color=(.75, .75, .75))

# show(pts)

z1 = solve(f(gs2[0].rhs()) == x - 1/3 * gs2[0].rhs(), x)[0].rhs()
print(f'z1 = {z1} = {z1.n()}', flush=True)

@interact
def _(z=slider(z1 - 0.5, z1 + 0.5, step_size=1/30.)):
    if (z - z1).abs() < 1e-8:  # consider z is at z1
        zplot = plot(z - 1/3 * x, (x, 0, 5), color="#4b26ea", linestyle='--')
        show(sum(c) + gr + poly + zplot, figsize=8)
    else:
        zplot = plot(z - 1/3 * x, (x, 0, 5), color="#4b26ea", linestyle='-.')
        show(sum(c) + gr + poly + zplot, figsize=8)

Check the result on SageCell: