Author Topic: [Nspire] Buggy Inflection Point Code  (Read 2255 times)

0 Members and 1 Guest are viewing this topic.

Offline Roondak

  • LV3 Member (Next: 100)
  • ***
  • Posts: 54
  • Rating: +4/-3
    • View Profile
[Nspire] Buggy Inflection Point Code
« on: October 24, 2013, 10:35:35 pm »
I've been programming an inflection point solver in Nspire BASIC, and my code seems to have an odd bug in it. If x^4+cx+d, where c and d are constants (ex. x^4+3x), is entered, the program spits out an error of nsolve not having an equation in the first argument, and I can't figure out why.

Code: [Select]
Define inflect(ex)=
Func
:Local ansx,di2,di3,ex,i,out
:derivative(ex,x,2)→di2
:derivative(di2,x)→di3
:ansx:=10^(99)
:out:={}
:For i,1,10
:  nSolve(di2=0,x=ansx,−10^(99),ansx-10^(−7))→ansx
:  If ansx="No solution found" Then
:    Exit
:  EndIf
:  If 0≠di3|x=ansx Then
:    out[2*i-1]:=ansx
:    out[2*i]:=ex|x=ansx
:  EndIf
:EndFor
:Return out
:EndFunc
ex is the expression entered, di2 and di3 are the second and third derivatives respectively, out is the output, ansx is the last x value found, and i is the for loop variable.

Also, any suggestions on how to make my code more efficient and such would be nice.

EDIT: Attached program below.
« Last Edit: October 24, 2013, 10:38:06 pm by Roondak »

Offline Roondak

  • LV3 Member (Next: 100)
  • ***
  • Posts: 54
  • Rating: +4/-3
    • View Profile
Re: [Nspire] Buggy Inflection Point Code
« Reply #1 on: October 29, 2013, 05:45:51 pm »
Managed to fix it, found about zeros(), much better than solve here.
New code if anyone wants to see it:
Code: [Select]
Define inflect(ex)=
Func
:Local ansx,di2,di3,ex,xvals,out,i
:derivative(ex,x,2)→di2
:derivative(di2,x)→di3
:xvals:=zeros(di2,x)
:out:={}
:For i,1,dim(xvals)
:  If di3≠0|x=xvals[i] Then
:    out[dim(out)+1]:=xvals[i]
:    out[dim(out)+1]:=di2|x=xvals[i]
:  ElseIf (di2|x=xvals[i]+1.E−5)*(di2|x=xvals[i]-1.E−5)<0 Then
:    out[dim(out)+1]:=xvals[i]
:    out[dim(out)+1]:=di2|x=xvals[i]
:  EndIf
:EndFor
:If out={} Then
:  Return "No inflection points found."
:EndIf
:out:=list▶mat(out,2)
:Return out
:EndFunc

Offline tpt1234567890

  • LV3 Member (Next: 100)
  • ***
  • Posts: 45
  • Rating: +0/-0
    • View Profile
Re: [Nspire] Buggy Inflection Point Code
« Reply #2 on: October 29, 2013, 05:48:04 pm »
Nice job on fixing it!

Offline Roondak

  • LV3 Member (Next: 100)
  • ***
  • Posts: 54
  • Rating: +4/-3
    • View Profile
Re: [Nspire] Buggy Inflection Point Code
« Reply #3 on: October 29, 2013, 10:46:32 pm »