Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using interactive controls


Time for action – an interactive example

Let's take an example from Chapter 8 and make it interactive. Run the following code in a worksheet:

var('x,y')

@interact
def _(c1 = slider(vmin=0, vmax=3, default=2, label='c1:'),
        c2 = slider(vmin=-3, vmax=3, default=1, label='c2:'),
        c3 = slider(vmin=-3, vmax=3, default=1, label='c3:'),
        c4 = slider(vmin=0, vmax=3, default=2, label='c4:')):

    c=vector(RDF,[-4,-5])
    G=matrix(RDF,[[c1,c2],[c3,c4],[-1,0],[0,-1]])
    h=vector(RDF,[3,3,0,0])

    sol=linear_program(c,G,h)
    print "Minimum:" + str(sol['x'])
    print "Slack variables: " + str(sol['s'])

    c1_plot = implicit_plot(c1*x + c2*y ==3, (x,0,6), (y,0,6))
    c2_plot = implicit_plot(c3*x + c4*y == 3, (x,0,6), (y,0,6))
    c3_plot = implicit_plot(x == 0, (x,0,6), (y,0,6))
    c4_plot = implicit_plot(y == 0, (x,0,6), (y,0,6))

    min_plot = point(sol['x'], color='red', size=50)

    rp = region_plot([c1*x + c2*y <= 3, c3*x + c4*y &lt...