Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: greg701 on May 02, 2011, 03:17:09 pm

Title: Simple (I think) programming question
Post by: greg701 on May 02, 2011, 03:17:09 pm
Hi all I want to program the secant method on my ti-nspire non cas.

The program requires various arguments

requires x0 , x1, f(x), and the number of iterations.

The iterative formula is:

(http://img222.imageshack.us/img222/5207/pnglatex.png)

I was wondering how I would go about programming this.

I think I would be able to do it if I knew how to pass a function ( f(x) ) into the program. Any help would be much appreciated. :) Hoping I can create a whole library of Numerical Methods :)

Thanks Greg  ;D

P.s.

Was also wondering how to give more decimal places on output to the screen
Title: Re: Simple (I think) programming question
Post by: Freyaday on May 02, 2011, 04:10:33 pm
I don't know much about Nspire programming, but I hear they haven't removed the 84+SE emulator yet, so here's what it would be in 83 series BASIC
Code: (PROGRAM:SECANT) [Select]
ClrHome
Input "X0:",A
Input "X1:",B
Input "F(X):",Str1
Input "ITERATIONS:",I
{A,B->L1
For(Q,2,I+1
L1(Q)->X
expr(Str1->A
L1(Q-1)->X
expr(Str1->B
(L1(Q-1)A-L1(Q)B)/(A-B->L1(Q+1
End
L1
Title: Re: Simple (I think) programming question
Post by: AngelFish on May 02, 2011, 04:22:24 pm
Passing functions in Nspire-BASIC is fairly simple. Here's a small tutorial: http://tibasicdev.wikidot.com/nspire#toc2 (http://tibasicdev.wikidot.com/nspire#toc2)
Title: Re: Simple (I think) programming question
Post by: greg701 on May 02, 2011, 05:00:49 pm
Ok this is what I have created so far.

Anyone suggest any improvements/ways of neating this up.

Also the user has to declare the function, f1(x), e.g (x^2) before the program can run. Anyway to resolve this issue?

Code: [Select]
Define LibPub secant(a,b,c)=
Prgm
:© A program to carry out the secant method
:setMode(1,1)
:Disp "When r = 0"
:Disp "x0 = ",a
:Disp "f(x0) =",f1(a)
:Disp ""
:Disp "When r = 1"
:Disp "x1 = ",b
:Disp "f(x1) =",f1(b)
:Disp ""
:For d,1,c,1
:Local e
:e:=((a*f1(b)-b*f1(a))/(f1(b)-f1(a)))
:Disp "When r =",1+d
:Disp "x",1+d," = ",e▶Decimal
:Disp "f(x",1+d,") = ",f1(e)
:Disp ""
:a:=b
:b:=e
:EndFor
:
:EndPrgm

Thanks for your time :)
Title: Re: Simple (I think) programming question
Post by: shrear on May 02, 2011, 05:11:53 pm
you can define a function in a program with
:define function_name(parameters) = function_body
example:
:define a(x)=x^2
or by
:function_name(parameters) := function_body

also you can normally do also calculations in an "disp" argument if a clear term comes out.