Author Topic: Simple (I think) programming question  (Read 2210 times)

0 Members and 1 Guest are viewing this topic.

Offline greg701

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 10
  • Rating: +0/-0
    • View Profile
Simple (I think) programming question
« 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:



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
« Last Edit: May 02, 2011, 03:49:48 pm by greg701 »

Offline Freyaday

  • The One And Only Serial Time Killing Catboy-Capoeirista-Ballerino
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1970
  • Rating: +128/-15
  • I put on my robe and pixel hat...
    • View Profile
Re: Simple (I think) programming question
« Reply #1 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
In other news, Frey continues kicking unprecedented levels of ass.
Proud member of LF#N--Lolis For #9678B6 Names


I'm a performer at heart; I stole it last week.
My Artwork!

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Simple (I think) programming question
« Reply #2 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
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline greg701

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 10
  • Rating: +0/-0
    • View Profile
Re: Simple (I think) programming question
« Reply #3 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 :)

Offline shrear

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 193
  • Rating: +17/-0
    • View Profile
Re: Simple (I think) programming question
« Reply #4 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.
« Last Edit: May 02, 2011, 05:15:01 pm by shrear »