problem with nspire programing

3,709 views
Skip to first unread message

José Manuel

unread,
Jul 5, 2009, 5:52:24 PM7/5/09
to tinspire
Hi to all, i want to do a program like this on nspire cas:
--------------
nwt(a)
prgm
disp a(2)
endprgm
--------------
When i use the program, if i write nwt(x^2) i want read 4 ( a(2)
=2^2=4), but the calculator say me: The name is no a function. Ok,
how must i define the variable "a"?
I want give to the program a function for that the program can to use
this function, but i dont know do it. who can help me?
Thanks a lot.

Eagle-Man

unread,
Jul 5, 2009, 6:56:41 PM7/5/09
to tins...@googlegroups.com
If I'm understanding you correctly, you have a function called nwt as follows:

define nwt(a)=
prgm
disp a(2)
endprgm

where the argument a is a function, and you wish to display the given
function, but replace the variable with a 2.

The problem is that the CAS understands a to be an expression, not
necessarily a function ("true" and "false" are also expressions).

One thing you could do, if you know the variable will always be x is
the following:

a|x=2

this will solve a, replacing x with 2, but this would require you to
always use the variable x.

One way around that is to use indirection (confusing, but handy).
Redefine nwt as the following:

define nwt(a, var)=
prgm
disp a|#var=2
endprgm

where a is the function and var is a string which contains the
variable that you wish to replace with 2. The # sign is called
indirection, whereby it takes the string stored in the following
variable (var) and uses that string as a variable name.

So, the line in the program reads, "display a where the variable name
in var is equal to 2." Given a=x^2 and var="x", this will be the same
as "disp a|x=2", resulting in 4.

This is also good because it will work for multi-variable expressions,
too (only on CAS), but only the variable given will be replaced.

--Eric

TJ

unread,
Jul 5, 2009, 8:23:58 PM7/5/09
to tinspire
Dear José Manuel,

Correct me if I am wrong, but I am basing my explanations off from
Eric’s views, as well as my own.

You say made a program on the TI-Nspire CAS as the following:
Define nwt(a)=
Prgm
:Disp a(2)
EndPrgm
When you run the program, nwt(2) for instance, the calculator gives an
error message saying, “Error: Name is not a function”.

Explanation:
The reason why this error message occurs is because the calculator is
confused by the way you have written the program. When make the
calculator display “a(2)”, generally, you would expect the calculator
to multiply “a*2”, but this does not happen. Instead the calculator
defines “a” to be a function. Similar to the functions on the “Graphs
& Geometry” page, like “f1(x)=”, the function “f1” would be the same
as the function “a” (except in that page, it is graphed instead). So
in your program, the variable “a” is never actually used at all.
Instead, the calculator thinks of “a” as a function. Since the
function is never defined, the calculator gives the error message,
saying “Error: Name is not a function”.

Solution:
Instead of the program you made, a little change would solve this
problem. Look at the program below:
Define nwt(a)=
Prgm
:Disp a*2
EndPrgm
Now the calculator will not refer the variable “a” as a function.
Instead, two multiplies the variable, and the result is displayed. So
if you run the program as “nwt(x^2)”, the result will give you “2*
(x^2)”. This is also what you wanted. So there is no need for a
function for this program anymore.

Suggestion:
When you write “a(2)”, on the calculator, it will think it is a
function. This happens with any other variable. If I wrote “b(2)”, the
calculator will think “b” is also a function. To avoid this confusion
with multiplication, you should use the multiplication sign in between
“a” and “(2)”. So if I wrote “b*(2)”, it would simply be “b*2”, where
the variable will simply be multiplied. There is nothing to be worried
about, because this confusion is common when dealing with the TI-
Nspire.

I would also refer to Eric’s way of manipulating the way the
calculator sees “a” as. But I’m actually unsure of what Eric is
saying, though.

Sincerely,
-TJ

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

Dear Eric,

Is it okay if you can expound on how your redefined program works? I’m
confused about the “#” sign as well as adding a second variable. When
you run the program, are you defining the variables as strings?

Sincerely,
-TJ

Eagle-Man

unread,
Jul 6, 2009, 1:29:30 AM7/6/09
to tins...@googlegroups.com
TJ,

First of all, I think your are misunderstanding the problem. I may be
wrong, but I think what José is trying to do is pass a mathematical
function (as opposed to a programming function) and refer to it in
f-of-x notation, replacing the variable with the number 2 (ie: f(2),
where f(x) = x^2).

Also, I just realized that I said my program would work on both
Nspires, but it won't because you can't pass the expression x^2 as an
argument on the non-CAS version. D'oh!

I'll create another thread about indirection.

--Eric

--- Original Message ---

>

Eagle-Man

unread,
Jul 6, 2009, 1:49:14 AM7/6/09
to tins...@googlegroups.com
So, TJ was asking what my program in the other thread does, and what
indirection is, so I thought I'd start a new thread about it. Just as
a refresher, here's that program.

define nwt(a, var)=
prgm
disp a|#var=2
endprgm

Indirection is a programming ability that has been borrowed from the
likes of C and C++. What it does is allow you to have one variable
point, or refer, to a different variable. It's a very confusing
ability, but quite nice once you know how to use it. Why is this
useful, you may ask. Because sometimes we don't know which variable
we want to look at, and this saves code.

On the Nspire, how indirection (#) works is you create a variable that
contains a string. This string should be the name of an existing
variable. When you use indirection on the string, it refers you to
the variable whose name it contains. For example, define the following:

x:=2
var:="x"

then "disp #var" would display 2, or in other words, what's stored in
the variable x. #var says look at the name that var is holding. Look
at that variable.

It's like saying you're at a party with Alan and Bob and you ask Bob
for his opinion on something. And Bob's response is "ask Alan."
Here, Bob is var and Alan is x.

So a program like the following:

define indirection(var)=
prgm
disp #var
endprgm

Would display the contents of whatever variable name you pass as the argument.
indirection("x") would display the contents of the variable called x.
indirection("hello") would display the contents of the variable called hello.
indirection("list") would display the contents of the variable called list.

That's assuming these variables exist. I haven't tried it with an
undefined variable, but I assume it returns an error.

Where you might use this in a program is if you want the user to tell
you which variable to use (like in the first example), or if you have
several variables and you want a numerical result to determine which
one you use:

local ind
if num=2 then
ind:="x1"
else if num=3 then
ind:="g5"
else
ind:="default"
endif
#ind blah blah blah...

So if we look back at my original program:

define nwt(a, var)=
prgm
disp a|#var=2
endprgm

what the user provides is an algebraic expression (like x^2 or 3n-5,
etc) and a string which contains the variable in the expression you
want to replace ("x" or "n", etc).

Suppose we look at José's example of a = x^2 and var = "x".

The program then takes the line "disp a|#var=2" and goes "wait a
minute, var is telling me to look somewhere else." the content of var
is "x", so the program goes "ah ha! It should read 'disp a|x=2',
using x like a variable." Then it replaces a with its contents, so we
get "disp x^2|x=2" which is, "display x^2 with x=2." And it will
display "4".

Better? Any questions?

Hope this helps.

--Eric

--- Original Message ---

> Dear Eric,
>
> Is it okay if you can expound on how your redefined program works? I’m
> confused about the “#” sign as well as adding a second variable. When
> you run the program, are you defining the variables as strings?
>
> Sincerely,
> -TJ
>
>

José Manuel

unread,
Jul 6, 2009, 3:53:10 AM7/6/09
to tinspire
Oh yes, now my real program is possible:

define nwt(a,u,x0,c)
prgm
x0->xk
For k,1,c,1
xk-(a|x=xk)/(u|x=xk)->xk
disp xk
endfor
endprgm

now run!!! but i have one more problema, if you see the program, i
should give the derivate of the function "a" ( "u"=derivate ), and i
want that the program does the derivate, but if i use u=d(a)/dx the
calculator say me: the variable is protected.

define nwt(a,x0,c)
prgm
x0->xk
u:=d(a)/dx
For k,1,c,1
xk-(a|x=xk)/(u|x=xk)->xk
disp xk
endfor
endprgm

how i can do the program now? Thanks
NOTE: I know that this program is already done, but i want learn to do
programs for me :).
Excuse me my poor use of english.

Nelson Sousa

unread,
Jul 6, 2009, 5:16:12 AM7/6/09
to tins...@googlegroups.com
Here's a solution:


Define nwt(a,x0,c)=
Prgm
:approx(x0)→xk
:expr("f(x):="&string(a))
:u(x):=d(f(x),x)
:For k,1,c,1
:xk:=xk-((f(xk))/(u(xk)))
:Disp xk
:EndFor
:EndPrgm

Remarks:
1. d(f(x),x) is the derivative of f(x) with respect to x;
2. When a is, for example, x^2, the expression "f(x):="&string(a) will
return "f(x):=x^2" as a string; taking expr of that will execute that
command, so it'll define function f as a 1 argument function. Like
this you can now call the two functions with the proper arguments on
the main cycle.


Cheers,
Nelson

Nelson Sousa

unread,
Jul 6, 2009, 5:20:49 AM7/6/09
to tins...@googlegroups.com
Indirection is great, especially to work with lots of variables. Here
are two examples:

1. This program deletes all variables from f1 to f99;

For i,1,99
str:="f"&string(i)
delvar #str
endfor

2. Delete all A-Z variables ("a" is char number 97, "z" is number 122)

For i,97,122
delvar #(char(i))
endfor


Indirection is awesome for many reasons.

Cheers,
Nelson

José Manuel

unread,
Jul 6, 2009, 5:43:30 AM7/6/09
to tinspire
Thanks Nelson, one more thing... somebody can give me a tutorial or a
list with all comands of "ti basic on nspire"? Thanks.

José Manuel

unread,
Jul 6, 2009, 6:32:13 AM7/6/09
to tinspire
And one more thing (yes, other more thing), how can i do for i can use
the program with the library? I set the program as public but i do not
see nothing in the library (book where are all function of the
calculator).

Ross

unread,
Jul 6, 2009, 10:34:19 AM7/6/09
to tinspire
I have a great collection of links to tutorials on my website,
Lafacroft, so I would suggest that you check out http://lafacroft.com/?q=node/54
If you just want a specific programming tutorial, then I would suggest
this: http://tibasicdev.wikidot.com/nspire It covers a lot, and it
should help you with your previous post. To make the program show up
in the library, first make sure it is defined with "LibPub" (which you
already did). Then, press ctrl+home and select "6. Refresh
Libraries". This will make the program show up in the library.
However, it doesn't show up in the first tab like the built in
functions. If you own the TI-Nspire, open up the library and press 5
to access user defined functions/programs. On the TI-Nspire CAS, you
need to press 6 instead. I hope that helps!

On a side note, I have a question for eagleman. In the past, I have
used indirection on the TI-89 by using this syntax: #variable. On the
TI-Nspire, you accomplish the same thing by doing #"variable". I was
wondering how you came to the conclusion to use a|#var. I don't
understand how that works, so an explanation would be very helpful.

Nelson Sousa

unread,
Jul 6, 2009, 11:37:41 AM7/6/09
to tins...@googlegroups.com
on both TI-89 and Voyage 200 the indirection command must have a
string afterwards.

So you can either use #"var" or #str where str="var".

The main restriction is that the string that follows # must be a valid
variable name, you can't have #"x^2" (use expr instead).

Eagle-man's program requires that var is a string and it is a valid
variable name, so one would call the program like this: nwt(x^2,"x")


Nelson

Nelson Sousa

unread,
Jul 6, 2009, 11:38:38 AM7/6/09
to tins...@googlegroups.com
There's a better resource to see ALL available commands: the Reference
manual. It has an alphabetical index of all functions and instructions
with some examples. Not the best place to learn from scratch, but an
excellent resource when you have a specific question.

Nelson

Eagle-Man

unread,
Jul 6, 2009, 1:15:41 PM7/6/09
to tins...@googlegroups.com
...and here it is!

Regular:
http://education.ti.com/educationportal/downloadcenter/SoftwareDetail.do?website=US&tabId=2&appId=6691

CAS:
http://education.ti.com/educationportal/downloadcenter/SoftwareDetail.do?website=US&tabId=2&appId=6690

And as for José's question, Nelson pretty much answered it, but I'll
restate it here. The Indirection operator is like a function that
takes a string and returns a variable. Like most functions on the
nspire, I can either pass it a literal string or variable containing a
string.

So, in my program I used a variable containing a string, where the
string is the name of the variable I want returned. Like Nelson said,
you can either do this:

#"varName"

directly, and this will return the variable called varName, or this:

thisIsAString:="varName"
#thisIsAString

the way that it's processed, it will replace the variable,
thisIsAString, with its contents ("varName"), after which we have the
first method anyway. Then it will find the var called varName and
return it.

Again, like Nelson said, if you're using the second method,
thisIsAString must contain a string, and in all cases, the string must
contain the name of an existing variable.

Like I said, confusing, but useful. Hope this helps.

--Eric

--- Original Message ---

>

Nelson Sousa

unread,
Jul 6, 2009, 1:21:45 PM7/6/09
to tins...@googlegroups.com
On Mon, Jul 6, 2009 at 18:15, Eagle-Man<eagl...@duetsoftware.net> wrote:


> thisIsAString must contain a string, and in all cases, the string must
> contain the name of an existing variable.

Doesn't need to be existing, but must be valid. That is, even if
variable x isn't defined, #"x" is a valid indirection string; on the
other hand, #"1+1", #"2a" or #"-x" are not valid.

Valid variable names have up to 16 characters, the first character
must be a letter (uppercase, lowercase, greek letter, international
symbol like á, ã, etc.); other characters can also be numbers or
underscore.


Nelson

Ross

unread,
Jul 6, 2009, 6:23:53 PM7/6/09
to tinspire
Thanks for clearing things up, Nelson. It seems like indirection has
become less valuable on the TI-Nspire than the TI-89, though. For
example, I know that when I learned about indirection on the TI-89, it
was used to display pics, which doesn't exist on the TI-Nspire. Also,
I am still a bit confused on why | was used. If you can use | to pass
values to a program, could you please explain how to do that, as I am
still a little bit confused on exactly how that works. Thanks!

On 6 July, 13:21, Nelson Sousa <nso...@gmail.com> wrote:

Nelson Sousa

unread,
Jul 6, 2009, 8:12:46 PM7/6/09
to tins...@googlegroups.com
| should be read as "such that". I find it most useful on
multi-variable calculations. If you have a very long expression where
a few variables appear, using | makes it much easier to recalculate
without having to replace a lot of values. It's great on iterative
calculations also.

Example:
sin(sqrt(x^2+y^2+z^2)/sqrt(x^2+y^2+z^2) | x=0 and y=0 and z=1
to repeat the calculation you only need to edit the end of the line.

Cheers,
Nelson

Eagle-Man

unread,
Jul 6, 2009, 8:46:14 PM7/6/09
to tins...@googlegroups.com
1) Thanks Nelson. I hadn't tried indirection with non-existent
variables. Good to know it works.

2) Ross, the reason I used | is purely situational. In the particular
example, José wanted to essentially replace all the variables in a
given expression with the number 2.

The pipe ("|") character is listed as "with" in the reference guide.
Basically, it does exactly what José wants. It goes like this: expr |
BooleanExpr1 [and BooleanExpr2]...

expr is any expression. For each Boolean Expression, if the
expression is an = expression, then the LHS is replaced by the RHS in
the original expression. Regular Nspire doesn't really like the other
boolean expressions much, but they work well on CAS.

Bah. I'm sure someone else can explain it better than me. Basically
in the program I posted, a is an expression, and the indirection tells
the Nspire which variable in the expression we're going to replace.

--Eric

--- Original Message ---

>

Ross

unread,
Jul 6, 2009, 10:32:46 PM7/6/09
to tinspire
Ok, I found out why I was confused. I was thinking that "a" was a
program, and that you were passing values to it. I understood the |
character, because I've used it before, but I thought you were using
it to pass values to a program. Thanks for clearing things up!

Andy Kemp

unread,
Jul 7, 2009, 4:38:49 AM7/7/09
to tins...@googlegroups.com
It's also worth noting that the use of the | symbol is very common in written mathematics at degree level, used in exactly the way Nelson described it.  So should be read as 'such that'  I don't know for certain but I suspect it's use on calc's was to reflect it's use in written mathematics...

Nelson Sousa

unread,
Jul 7, 2009, 6:03:57 AM7/7/09
to tins...@googlegroups.com
I just remembered an excellent example of using "such that":

Try this on a CAS:

desolve(m*x''+k*x=0,t,x)

This is the harmonic oscillator ODE but the solution will be presented
as the sum of two exponentials with exponent sqrt(-k/m).

However,

desolve(m*x''+k*x=0,t,x) | m>0 and k>0

will display the result as the sum of sin(sqrt(k/m)*t) and cos(sqrt(k/m)*t).

Another example is a very simple definition of the Laplace transform: defining

laplace(f)=int(f*e^(-s*t),t,0,infty)

will result in undefined output regardless of what function you use as
argument. However, defining

laplace(f)=int(f*e^(-s*t),t,0,infty) | s>1000

(or any number large enough to be surely inside the domain of the
Laplace transform of the functions you're studying) will give you the
expected results for laplace transforms of most simple functions
(functions that are piecewise defined requires extra work).


Nelson

John Hanna

unread,
Jul 11, 2009, 3:00:54 PM7/11/09
to tins...@googlegroups.com

So can indirection be used to pass a variable into a program that the
program can change?

I only can pass values into a program, but the program does not change the
passed global variable.

I have not had luck getting this idea to work. I some programming languages
this is called 'variable parameters' vs. 'value parameters'.

Sail Upwind,
John Hanna
973.398.3815
jeh...@optonline.net
www.johnhanna.us
The future is not what it used to be. - Paul Valery

Nelson Sousa

unread,
Jul 11, 2009, 3:26:40 PM7/11/09
to tins...@googlegroups.com
you can use "such that" statements to change the input passed to
functions, but those conditions are evaluated before the function is
called.

Example: suppose you have a function to return the solutions of 2nd
degree equations. You can call it on the numeric Nspire by typing in

quadratic(a,b,c) | a=2 and b=1 and c=-1

However you get an error when trying to pass this to a program.


As to variables used as a program's arguments: a program can change
the value of any external (global) variable, but the variable names
used as arguments are dummy variables and can't be changed. They'll be
deleted when the program stops. Even if an external variable already
exists with the same name, it won't be changed by a program.

Example:
a:=1

Define a program
prog(a)
Prgm
2=:a
EndPrgm

When the program finishes the external variable a will remain
unchanged. The variables used as arguments of both functions or
programs are dummy variables, they're not global variables.


When I want to allow parameters on a program (parameters are usually
optional) I prefer to have the user input a string with the desired
parameter values. That will add some complexity to the code, as the
parameter string must be parsed. My preferred format is
"param1=value1,param2=value2"; I run throught the string looking for
commas, then I go through each block looking for = and parsing the lhs
of the block as a parameter name (looking to see if it's in a list of
allowed parameters) and the rhs as its value (with try-endtry to see
if it evaluates to a number or checking whether it takes on of the
allowed values). This way I can keep the number of arguments fixed,
which is a requirement of Nspire's language (as was with 89 and V200).


I hope this helps.

Nelson
Reply all
Reply to author
Forward
0 new messages