Convert TI-89 Simplex Program to TI-Nspire

2,544 views
Skip to first unread message

Luke Setzer

unread,
Sep 19, 2008, 8:47:02 PM9/19/08
to tinspire
Hi!

I wanted to send you a link to a downloadable ZIP file with programs
that work together to let you do simplex tableaux on the TI-89:

http://www.ticalc.org/archives/files/fileinfo/364/36453.html

I am trying to compile a set of lessons learned for future students so
I am exploring this further.

This brings me to my main point.

I wanted to know if I could talk you into understanding the code of
this software well enough to convert it to TI-Nspire.

I am not a "something for nothing" person and will reimburse you for a
reasonable price you name.

I could do this myself given enough time but since you have
demonstrated expertise in programming, I thought I would pulse you.


Luke Setzer

Nelson Sousa

unread,
Sep 20, 2008, 9:02:33 AM9/20/08
to tins...@googlegroups.com

It might be easier than you think.
You can "almost" copy TI-89 code into Nspire, as long as you take two very important principles into mind:

1) TI-Nspire does not perform symbolic calculations, while TI-89 does; a lot of functions can return errors if there are any undefined variables being used or any kind of symbolic manipulation; however, using Nspire CAS you'll might overcome those problems without further complications.

2) Nspire only accepts input from the command line, so all "Input" and "Prompt" instructions have to be replaced by arguments passed on to the program. Also, Nspire only outputs with Disp, not begin possible to do any sort of graphic output, for instance.

The good news are: from the screenshots you mention it seems to me that simplex only uses functions (that gather input as arguments and return a number, list, matrix or anything else as a result). Functions can be translated almost verbatim to the Nspire (some functions change name and/or usage, but you'll probably find out easily the correspondent one on Nspire's reference manual. Probably something like 99% of Nspire functions have the same names and are used the same way as it's TI-89 counterparts.

So I'd suggest you simply copy, char by char, the entirety of the programs to Nspire (with proper authorization from the original author), and try it out. I believe most problems would be easily solved with some analysis of the code.


Regards,
Nelson

Luke Setzer

unread,
Sep 21, 2008, 6:46:37 AM9/21/08
to tinspire
I will give it a shot.

Someone could make some serious money with an application that does
most of this automatically.

Copy and paste works poorly because of text corruption between the
applications.

So my first big hurdle involves cleaning the corrupted text so the
codes look identical between TI-Graphlink and TI-Nspire before
tweaking them to suit the new platform.

Luke Setzer

unread,
Oct 14, 2008, 11:44:47 AM10/14/08
to tinspire
Hi,

I found a set of programs at http://www.ticalc.org and converted them
to Simplex.tns in the Files section.

Here is what the author had to say about the original TI-89 programs:

------------------------------------------------------------------------------------------

SIMPLEX ALGORITHM on the TI89

Four programs are provided to implement the SIMPLEX ALGORITHM on TI
calculators; ZSIMPLEX, SIMPLEX, XSIMPLEX, and XSIMPCFG. After the
programs are installed on the calculator, the Tableaux Matrix, mat,
must be entered either directly or by the matrix editor. Three
examples will be used to illustrate the Tableaux in different cases.
Since these algorithms use the "Big M" method, the Tableaux must be
constructed accordingly.

Notes: < really means <= (less than or equal to)
> really means >= (greater than or equal to)


Example 1:
Maximize: Z = 3x + 5y
Subject to: x < 4
2y < 12
3x + 2y < 18

With the introduction of slack variables, the resulting Tableaux is

-3 -5 0 0 0 0
1 0 1 0 0 4
0 2 0 1 0 12
3 2 0 0 1 18


The solution may be obtained by running the program SIMPLEX(mat). The
TI89 display will be:

DONE

The answer has been stored to the variable, ANSWER, which contains the
list

{36. 2. 6. 2. 0 0}

The maximum value for Z will be 36 when the objective variables, the
next two entries in list ANSWER, take the values x = 2 and y = 6.
The values for the three slack variables are also included in the
list. The Tableaux matrix may be viewed by recalling the variable,
mat, to the home screen.

If the student would prefer to step through the solution process from
one Tableaux to the next then enter the Tableaux matrix, mat, as
previously described. Now run XSIMPLEX(mat)->mat. The next Tableaux
will be computed and displayed. The cursor keys can be used to scroll
through the resulting Tableaux.

-3 0 0 5/2 0 30
1 0 1 0 0 4
0 1 0 1/2 0 6
3 0 0 -1 1 6

If the Tableaux has negative entries in the top row, press [ENTER]
to run XSIMPLEX(mat)->mat again. Continue pressing [ENTER] and
scrolling the resulting matrix until the Tableaux is in the final
form.

0 0 0 3/2 1 36
0 0 1 1/3 -1/3 2
0 1 0 1/2 0 6
1 0 0 -1/3 1/3 2

The resulting values for Z and the objective variables may be read
directly from the matrix, mat. Note that the original Tableaux
Matrix, mat, is changed by both of these programs. It is a good idea
to back it up under another name, bak, before running the programs.

Example 2:
Maximize: Z = 3x + 5y
Subject to: x < 4
2y < 12
3x + 2y = 18


With the introduction of an artificial variable to account for the
equality, the resulting Tableaux, mat, is

-3 -5 0 0 1000 0
1 0 1 0 0 4
0 2 0 1 0 12
3 2 0 0 1 18

1000 is added to the objective function to mark the column containing
the artificial variable as per the "Big M" method. When SIMPLEX(mat)
is run the result is stored to the variable ANSWER as before and we
have the list:

{36. 2. 6. 2. 0 0}

If we prefer to step through the solution process, we must run
XSIMPCFG(mat)->mat once initially to configure the Tableaux and enable
it to handle the Big M.(1000).

-3003 -2005 0 0 0 -18000
1 0 1 0 0 4
0 2 0 1 0 12
3 2 0 0 1 18

Then run XSIMPLEX(mat)->mat repeatedly until the Tableaux is in its
final form.

0 0 0 3/2 1001 36
1 0 0 -1/3 1/3 2
0 0 1 1/3 -1/3 2
0 1 0 1/2 0 6


Example 3:
Minimize: Z = .4x + .5y
Subject to: .3x + .1y < 2.7 .5x + .5y = 6
.6x + .4y > 6

With the introduction of a surplus variable to account for the (>=)
(greater than or equal to) in the last equation, the resulting
Tableaux is

.4 .5 0 1000 0 1000 0
.3 .1 1 0 0 0 2.7
.5 .5 0 1 0 0 6
.6 .4 0 0 -1 1 6

1000 is added to the objective function to mark the columns
containing the artificial variables as per the "Big M" method and -1
marks the surplus variable column. The problem has been transformed
from a minimize problem to a maximize problem by taking the negative
of the objective function. When SIMPLEX is run the result is:

{-5.25 7.5 4.5 0 0 .3 0}


Since we started by taking the negative of the objective function we
must now take the negative of Z to obtain the actual minimum value
5.25. This minimum value is achieved when the variables are x = 7.5
and y = 4.5.

To step through the successive Tableaux we must first run
XSIMPCFG(mat)->mat to configure the Big M's and then repeatedly run
XSIMPLEX(mat)->mat as in Example 2. The final Tableaux is:

0 0 .5 998.9 0 1000 -5.25
1 0 5. -1. 0 0 7.5
0 0 1. .6 1 -1 .3
0 1 -5. 3. 0 0 4.5

This process may be automated somewhat by running the program
ZSIMPLEX(). This program sets up a menu from which all the choices
just described may be easily made. Install all the programs to the
same directory on your calculator.

Send questions or comments to:
Scott Campbell
camp...@fnbnet.net

------------------------------------------------------------------------------------------

I placed the document into My Library for my PC based version of the
software but still cannot get this to work.

Can someone please help?


Luke Setzer

Nelson Sousa

unread,
Oct 14, 2008, 1:38:32 PM10/14/08
to tins...@googlegroups.com

Well, when I said "translate" I really meant "translate and test".
There are a few things that need changing:

1. the function to convert a list into a matrix or a matrix into a list on simplex and xsimple isn't well written (the arrow character that didn't copy&paste properly).
2. the zsimplex program is meant to create a custom menu; however, Nspire doesn't have custom menus, so this program is useless (and produces an error); also all mentions to functions custmon/custmoff should be removed.
3. on the end of program simplex there's no need to use ll->answer; just use Disp ll and the program will output the variable ll (variable answer is in fact unused except for this instruction).
4. There are errors caused by undefined variables. Usually the best way to test for those errors is to comment out all definitions of local variables. Then, debug, reading the values on those variables everytime an error occurs and see what's happening. One of those is the variable col on program xsimplex; due probably to an error on the original program, the variable doesn't have a default assignment; in the example mentioned it doesn't get a value (previous if statement fails), so  there's an error when calling mat[i,col]. What should be the default assignment? I don't know, better ask the original author.
5. The same happens with row variable on xsimpcfg: variable row only receives a value if an if statement is true; if that if statement is false, there's no assignment and you'll get an error.


Also I noted that on the programs you never give out credit to the original author. Being not a program of your own but just a port of another program, credit should be given at all times.


My advice: as long as you can read these programs, you allready know enough about programing the Nspire to create a new program, of your own. Just study the simplex algorithm,  and start working on it. It's not easy, but it's not that hard either.


Nelson

Luke Setzer

unread,
Oct 15, 2008, 7:00:09 AM10/15/08
to tinspire
> Also I noted that on the programs you never give out credit to the original
> author. Being not a program of your own but just a port of another program,
> credit should be given at all times.

Well, of course ... if I can ever get it to work ... otherwise I want
to avoid besmirching a good name! :-P

Nelson Sousa

unread,
Oct 15, 2008, 7:03:55 AM10/15/08
to tins...@googlegroups.com

point taken. sorry if I ofended you. Wasn't my intention.

Nelson

Luke Setzer

unread,
Oct 17, 2008, 3:23:38 PM10/17/08
to tinspire
Okay, I have it (sort of) working now -- thanks for the tips on the
various errors.

See Simplex.tns and Test.tns for results.

I have added appropriate credits as promised.

I want to modify Simplex so it returns a result instead of just
displaying or storing a result, e.g.

y:=simplex\simplex(x) {36. 2.
6. 2. 0 0}

y
{36. 2. 6. 2. 0 0}

How can I do this?

Eagle-Man

unread,
Oct 17, 2008, 7:14:26 PM10/17/08
to tins...@googlegroups.com
Simply use the Return command as opposed to the Disp command (ie:
"Return {36. 2. 6. 2. 0 0}" or whatever you're var is called).

Note that ONLY FUNCTIONS can return a value that is useful (and can be
stored). If you return a value in a program, it will be ignored.

--Eric

Luke Setzer

unread,
Oct 17, 2008, 7:22:04 PM10/17/08
to tinspire
How can I convert program SIMPLEX to a function?

I tried earlier and it gave me an error of some sort.

Will library functions work on matrices?

Nelson Sousa

unread,
Oct 17, 2008, 7:24:59 PM10/17/08
to tins...@googlegroups.com

yes, a function works the same way as a program, but has some limitations (cannot use Disp nor Try-Endtry blocks).
Another limitation is that functions cannot store into variables, except local ones.

To convert you must create a new one, on type select function instead of program, and simply copy& paste all the code into the new editor.

You should have a return instruction in the end, otherwise the function will be pretty much useless.

Nelson

Luke Setzer

unread,
Oct 17, 2008, 7:46:12 PM10/17/08
to tinspire
I made MAT into a local variable and ... it worked!

Thanks!

Luke Setzer

unread,
Oct 17, 2008, 8:11:33 PM10/17/08
to tinspire
So I updated the file and loaded the latest version to the Files
section here.

Now I need to fix the other two programs and perhaps make them into
functions as well -- not sure I can do it.

I'm thinking that I have to leave MAT global for XSIMPCFG and XSIMPLEX
for them to accomplish their respective purposes.

Nelson Sousa

unread,
Oct 17, 2008, 8:32:59 PM10/17/08
to tins...@googlegroups.com

you cannot create global variables within functions. You can recall a variable, if it was allready defined, but you can't change it.
the idea behind functions is that you use a function and it will give you (return) a result. You can then call another function using that result and so forth. So if mat is the only variable that needs to be passed on to the other programs, use it as an argument and convert them into functions.

You would then call xsimpcfg and xsimplex as
xsimpcfg(mat) and xsimplex(mat).

as a rule of thumb: if your "program" only outputs 1 object (a number, a list, vector, matrix, string, etc.) then use a function. If you need to output results as you go, or if you need more complex output (say, text combined with numbers in multiple lines), then use a program.

If you need to create several variables, some of which are used in a subsequent calculation and others on different calculations, have a program create global variables.

If you need to recall it from another application, other than the Calculator app, you must use a function. Programs can only be called from a calculator app, while functions can be called from other applications.

Hope this helps.

Nelson

Matte

unread,
Dec 13, 2008, 1:04:42 AM12/13/08
to tinspire
Hi,
I'm a student from Italy and I found your simplex port just wonderful,
I was working on a way to create it from a level0 when I found this
group, thanks Luke :)
Just one question...I understood how the simplex function work, the
others (xsimplex etc) are a bit difficult for me to understand, but
now I read they're not completely ported, right?
Apart from finding the objective function and the decision variables,
it would be interesting for me to know which is the order of the base
indexes...or the final tableau if you prefer. I read about xsimplex
(mat)->mat which should print the correct tableau but I didn't really
understand how it works.
The main problem perhaps is I didn't learn the simplex method with the
use of the tableau and could be I didn't understand what the tableau
is.
I'll try to explain what I understood, it would be useful if you can
correct me where I'm wrong...
the tableau contains
c | 0
A | b
So, when we speak about the 'correct' tableau, it has the A and c
columns exchanged to let me understand how they're entered in basis,
in which order, ok?
If all this is right, I still have to understand how to use xsimplex.
I found x,y both contain the tableau I sent as an argument to simplex
(), but when I launch, as suggested by the document, xsimplex(y)->y
for example (argument is the tableau itself, so it could be x too, and
in the "->"y part we mean y is where the first step of the simplex
tableau resulted from the pivot operation is stored), I have an error.
I haven't any error if I only run xsimplex(x) or (y), but the tableau
given by result is always the same...(I used examples included to try,
hope basis aren't ordered 1,2,3,4 :) ).
Hope you can make me understand, thanks!!

Oh and..thanks again for the porting, great work.

Matteo Vicini

On 18 Ott, 01:32, "Nelson Sousa" <nso...@gmail.com> wrote:
> you cannot create global variables within functions. You can recall a
> variable, if it was allready defined, but you can't change it.
> the idea behind functions is that you use a function and it will give you
> (return) a result. You can then call another function using that result and
> so forth. So if mat is the only variable that needs to be passed on to the
> other programs, use it as an argument and convert them into functions.
>
> You would then call xsimpcfg and xsimplex as
> xsimpcfg(mat) and xsimplex(mat).
>
> as a rule of thumb: if your "program" only outputs 1 object (a number, a
> list, vector, matrix, string, etc.) then use a function. If you need to
> output results as you go, or if you need more complex output (say, text
> combined with numbers in multiple lines), then use a program.
>
> If you need to create several variables, some of which are used in a
> subsequent calculation and others on different calculations, have a program
> create global variables.
>
> If you need to recall it from another application, other than the Calculator
> app, you must use a function. Programs can only be called from a calculator
> app, while functions can be called from other applications.
>
> Hope this helps.
>
> Nelson
>

Luke Setzer

unread,
Dec 14, 2008, 7:35:20 AM12/14/08
to tinspire
Unfortunately, my grasp of the inner workings of this code is sketchy,
so I can offer little more help here.

Good luck!
Reply all
Reply to author
Forward
0 new messages