6

I created a set of programs to calculate the area under a graph using various methods of approximation (midpoint, trapezoidal, simpson) for my Calculus class.

Here is an example of one of my programs (midpoint):

Prompt A,B,N
(A-B)/N->D
Input "Y1=", Y1
0->X
0->E
For(X,A+D/2,b-D/2,D)
Y1(x)+E->E
End
Disp E*D

Instead of applying these approximation rules to a function (Y1), I would like to apply them to a list of data (L1). How do I iterate through a list? I would need to be able to get the last index in the list in order for a "For Loop" to be any good. I can't do anything like L1.length like I would do in Java.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
James T
  • 1,079
  • 3
  • 13
  • 17
  • TI-83 or TI-84? Title says one, tag says the other. – Chris Morgan Nov 16 '10 at 00:23
  • @Chris: They are both essentially the same. TI-84 is just a TI-83 with more memory and other things. – Jeff Mercado Nov 16 '10 at 00:24
  • @Chris It is TI-83. I was unable to create a new tag for the 83. Programming for the 84 should be exactly the same as for the 83. – James T Nov 16 '10 at 00:25
  • @Jeff: still, it offends my righteous soul to see clashing labelling :-) Didn't notice the fact that the ti-84 was only used once before and ti-83 doesn't exist... someone should create the tag and retag a number of questions with the them. – Chris Morgan Nov 16 '10 at 00:26
  • @Chris: I would be able to do that but I'm not sure it really is necessary. If anything, there should be a [ti-83] tag and is synonymous with [ti-84]. However there is hardly any activity in the tags in the first place, I think it'd be fine to leave as is for now until the tag gets active enough. – Jeff Mercado Nov 16 '10 at 00:33

2 Answers2

6

You can obtain the length of the list using dim(). That can be found in 2nd->LIST->OPS->dim(. Just make sure that you use a list variable otherwise dim() will complain about the type. You could then index into the list with a subscript.

e.g.,

{1, 2, 3, 4} -> L1
For (X, 1, dim(L1), 1)
Disp L1(X)
End
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
4

The for loop is the simplest way to iterate over a list in TI-Basic, as it is in many languages. Jeff Mercado already covered that, so I'll mention a few techniques that are powerful tools in specialized situation.

Mapping over lists

TI-Basic supports simple mapping operation over lists that have the same effect as a map function in any other language. TI-Basic support for this extends to most basic arithmetic function, and selection of other functions.

The syntax could not be simpler. If you want to add some number X to every element in some list L1 you type X+L1→L1.

seq(

Most for loops over a lists in TI-Basic can be replaced by cleverly constructed seq( command that will outperform the for loop in time and memory. The exceptions to this rule are loops that contain I/O or storing variables.

The syntax for this command can be quite confusing, so I recommend reading over this documentation before using it. In case that link dies, here's the most relevant information.

Command Summary

Creates a list by evaluating a formula with one variable taking on a range of values, optionally skipping by a specified step.

Command Syntax

seq(formula, variable, start-value, end-value [, step])

Menu Location

While editing a program, press:

2nd LIST to enter the LIST menu RIGHT to enter the OPS submenu 5 to choose seq(, or use arrows.

Calculator Compatibility

TI-83/84/+/SE

Token Size

1 byte

The documentation should do a good job explaining the syntax for seq(, so I'll just provide a sample use case.

If you want the square of every number between 1 and 100 you could do this

For Loop

DelVar L1100→dim(L1
for(A,1,100
A²→L1(A
End

or, this

seq

seq(A²,A,1,100→L1

The drawback of seq( is that you can't do any I/O or store any variables inside the expression.

Predefined list iteration function

Go to the LIST menu and check out all the operations under OPS and MATH. These predefined function are always going to be faster than a for loops or even a seq( expression designed to do the same thing.

ankh-morpork
  • 1,732
  • 1
  • 19
  • 28