` ``Define LibPub test(x)= Func :Disp x :If x<1 Then :Return x :Else :Return (x-1)^(2)+1 :EndIf :EndFunc`

` ``nInt(test(2),x,0,5) 2 10`

The first displayed line is from Disp x (argument). The second is the integral. But this is not the integral from test(x). This is test(2)*(5-0), i.e. constant test(2) is taken out of the integral, so the integration becomes trivial: constant times the length of the integration interval. This is correct, but not what I want.

]]>` ``nInt(test(2),x,0,5)`

The idea that the full form If-Then-Else-EndIf can behave differently is interesting. I have checked it, but with the same negative result.

I think that this is not "If" issue, but rather how nInt() uses the provided function. I have modified the test case. This time I want to integrate numerically function w=w(x) say from 0 to 1, where w and x are related as w*exp(w)=x, so I cannot write the analytical expression w=w(x), but have to solve the non-linear equation numerically. Now my function looks even simpler

` ``Define LibPub productlog(x)= Func :Return nSolve(w*e^(w)=x,w=0) :EndFunc`

There is no If statement at all. Again, by itself, function works fine and returns correct results, but when I try to calcuclate its integral numerically, I get "Argument error"

` ``nInt(productlog(x),x,0,1) "Error: Argument Error"`

"The first argument of nSolve must be an equation in a single variable. It cannot contain a non-valued variable other than the variable of interest."

The error is different.

Let's go back to function test() with "If" statement. The description of the error is the following.

"20 A test did not resolve to TRUE or FALSE

This error usually occurs when comparing an undefined variable, in a statement such as If."

In reply to Trenly (see above) I already suggested that it is possible, that nInt() tries to take integral analytically first. In this case the integrand will be called with the undefined argument and "If" statement will trigger this error. To support this idea, I inserted command Disp x in test(x) function right before the If x<1 statement, so the function prints its argument each time it is called. I get the following result:

` ``nInt(test(x),x,0,5) x "Error: A test did not resolve to TRUE or FALSE"`

So, the function IS called with the undefined argument (just "x" not a number). This is actual source of the error.

And the last nail into the lid of my TI-nspire CX CAS. I borrowed TI Voyage 200 and performed both tests with test(x) and productlog(x) functions. Voyage 200 calculated both integrals without problems and results coincide with what I get from Mathematica on PC.

Now I can confirm, that this issue is *not 68k BASIC issue*, but specific for TI-nspire CX CAS (I did not test non-CAS version). If we want to use user defined function in nInt(), the user defined function MUST be written in such a way, that it can be called with the undefined argument. This, mostly, means that the user defined function must be *explicit* function. By the way, this problem arises not only when using nInt() but also nSolve() function.

If I new these undocumented programming limitations in advance, I would never buy this fast nice looking and expensive calculator.

]]>But to the problem, as you have stated it:

No, I am not sure if this is a definitive bug with Nspire user-defined functions. I do not have an Nspire, and as I said the documentation on the Nspire on TI|BD is rather limited. TI's Nspire documentation is not much help either.

There really is no definitive "programming guide" where such a quirk might be directly documented (unless you think it's here, in which case I suppose we should be flattered); unlike C (which still has *plenty* of bugs that haven't stopped programmers yet) or Python or other computer languages, there is no official, universal documentation system or location beyond TI's manuals or wikis like ours.

As Trenly pointed out, you may be able to fix the general problem with a more explicit `If-Then-ElseIf-EndIf` conditional block, as there *may* be some issues with single `If` statements in combination with `Return` or other commands. Take, for example, the strange TI-83 Basic quirk involving `For(` loops: the following code

` ``:For(X,1,50) :If X=2 :Disp X :End`

will run *much* faster than

` ``:For(X,1,50 :If X=2 :Disp X :End`

due to the `If` statement being false 98% of the time. There's no real reason it exists other than a quirk in the assembly, but it does. Not a perfect analogue of a potential bug with `If` statements not working properly *at all*, but still an example.

With a more "complete" conditional block, your code would become

` ``Define LibPub test(x)= Func If x<1 Then Return x Else Return (x-1)^2+1 EndIf EndFunc`

which seems to be easily adaptable to the general problem you discussed, in combination with `ElseIf` and the like.

The Nspire may have an `If` statement bug. I do not know for sure, nor do we have documentation on the matter; someone else on TI|BD may know, or you can ask around on Cemetech, the most likely place to find an expert on the topic.

First,

This will hopefully fix the issue with nInt converging improperly.

This is wrong statement. nInt() is not just converging improperly to a wrong solution. It is not running at all throwing the error.

You are talking about workaround. I am talking about the language specification. DO WE HAVE SUCH LIMITATION ON USER DEFINED FUNCTION OR NOT? If we do have, than this should be reflected in the Reference manual or programming guide. This is VERY serious limitation which looks very artificial. If we had such limitation for example in C , how many C programmers would we have today?

You understand, that your solution works only for provided test case. I created it only to illustrate the problem. In my situation, I was calculating correction to the star altitude above the horizon due to the atmospheric refraction. I needed to take integral over the refraction index n(r) (where r is the distance from the center of the Earth. You can think of it as the height above the surface of the Earth) over zenith distance z (angle between zenith and the star). r and z are related by non-linear equation r*n(r)-n0*Re*sin(z0)/sin(z)=0, where n(r) is known, but complicated function. All other quantities are known. We cannot solve this equation analytically r = r(z), because of complexity of n(r).

So, for given z I need to solve this equation numerically for r and then calculate n(r) with this just found r and numerically integrate function n(r). By the way, nSolve() failed to solve this equation, so I wrote my own code using Newton's method. But when I did this, I had to use some condition to stop the iterations. That's where I used "If" statement. I do not think it can be removed, because *you need a condition* to stop iterations.

And, of course, such user defined function failed when I used it in nInt(). So, your solution is not universal.

What I really want to know if there is such limitation for the user defined function in TI-nspire BASIC specification.

"… see which commands you can help fill out"

Description must be written by those who wrote the code of these functions and familiar with their internal implementation. The only thing what I can do here without proper documentation is to use try-and-fail method.

Thank you.

]]>TI-nspire supports LibPub. This keyword is just control visibility (scope) of the function. LibPub means that the function will be visible in the main catalog.

The form of the condition statement what I used is just a short form of the general If-Then-Else-EndIf. If the code which has to be conditionally executed is just one statement, you can skip Then-Else-EndIf. This is reflected in the Reference Guide.

68k Error Page is not very helpful.

"20 A test did not resolve to TRUE or FALSE

This error usually occurs when comparing an undefined variable, in a statement such as If."

I have no idea how nInt(…) is implemented internally. As far as nInt() is the *numerical* integration function, I do not understand why nInt() tries to call test(x) with the undefined argument.

It is possible, that nInt() tries to take integral analytically first. In this case the integrand will be called with undefined argument and "If" statement will trigger this error, but I refuse to think that somebody made THAT decision. If I want to integrate function analytically, I use integral() command. Beauty of numerical integration is that you can integrate functions which cannot be represented analytically. Say I want to integrate function y=y(x). Relation between x and y is given by equation y*exp(y)=x. This cannot be written in the form y=y(x), but y can be found for any x numerically, solving this non-linear equation, and definitely such y can be numerically integrated.

If nInt() calls its integrand with undefined argument, it is *not a numerical integration* and makes it pretty useless for any real application. By the way, this limitation on the integrand function is purely artificial and was not mentioned anywhere in the reference guide.

\begin{align} \mathrm{test}(x) = \begin{cases} x & x < 1 \\ (x-1)^2 + 1 & x \geq 1 \end{cases} \end{align}

This will hopefully fix the issue with `nInt` converging improperly. If you'd like to help contribute to our Nspire documentation, please read our guidelines for editing and head over to the Nspire Command Index to see which commands you can help fill out.

I posted this issue in Nspire programming section because I hit this brick wall programming TI-nspire and I am sure that the problem exists there. I am not sure if this general 68k problem, because I cannot test it because TI-Nspire is the only programming calculator I have.

]]>I am new to TI BASIC (I recently bought my calculator). I tried to integrate the following simple user defined function, but got error "Error: A test did not resolve to TRUE or FALSE"

` ``Define LibPub test(x)= Func If x<1 Return x Return (x-1)^2+1 EndFunc`

The function is non-singular, continuous and works fine (you can calculate it for any value of argument x) until you plug it into numerical integration function nInt(…) to calculate its integral say from 0 to 5.

` ``nInt(test(x),x,0,5)`

The result will be

` ``"Error: A test did not resolve to TRUE or FALSE"`

This error is triggered by "If" statement. If the user defined function does not contain "If" statement, it can be used as an argument of nInt(…).

Am I doing something wrong or "If" statement is really prohibited in the user defined function which is used in nInt(..)?

Thank you.

]]>