A little while ago I was sitting in calculus and thought that since the ti-84s could calculate derivatives at a point, the calculator could calculate it on a certain interval and then, that data could be used to try and work out the derivative of a function. I quickly put together a little program that has a billion issues but sort of works for simple inputs. here is the code...
Code:
ClrList L1
ClrList L2
ClrHome
Input "f(x)=",Str1
String>Equ(Str1,{Y1}
For(A,1,5
   nDeriv({Y1},X,A)->L1(1+dim(L1
   A->L2(1+dim(L2
End
If inString(Str1,"sin(") or inString(Str1,"cos(
Then
   SinReg L2,L1,{Y1}
   Else
   If inString(Str1,"^X
   Then
      ExpReg L2,L1,{Y1}
      Else
      QuartReg L2,L1,{Y1}
End:End
Equ>String({Y1},Str1
Output(3,1,Str1
Pause

Of course, the methods used are very bad and I can think of a million functions for which the program would not work, but nonetheless, it can get some basic stuff done and at only 160 bytes, I think it's worth discussing Razz
Basically, it just calculates the derivative at five points, then using the built-in regression methods, tries to fit the appropriate type of function to it. As one might expect, the output is often very funky but correct (for the few functions that are valid input, which are basically the family of functions that have a built-in regression method)
It can work out the derivatives of polynomials up to degree 5 (since the derivative is a degree 4 polynomial and it uses quartreg) which includes linear functions, it can do sin/cos function with SinReg since any cos function can be expresed as a sine function, and it can do exponential functions, which is probably the most interesting part because working these out by hand requires logarithmic differentiation which means the program could actually be useful. However it can't handle composite functions such as f(x) = sin(x) + (3x³ - 5x² + 4x + 3) even though this would probably be very easy to implement for addition and subtraction of functions.
Here is a screenshot of the derivative of sin(x)



If you squint a little, you can see a cos(x) hidden deep in there Laughing
Also, I'm quite surprised to see how big the rounding errors become, as you can see at the beginning of the output, the rounding errors are not just the last one or two decimal places, but rather the last 8!
This is not a project of mine, I just thought I'd share this because I couldn't find a topic about it and I thought it was interesting even thought it is nowhere near what one would expect a derivative calculator to do.
Pretty interesting to see, although I'm a bit disappointed that you can only derivate simple functions, and people can do that manually Wink Does it run fast, or do you have to wait about 5 seconds to finish? Anyway, I believe it's possible to write such program that can derivate ALL the possible functions, using the general functions, like [f(x)g(x)]' would be f'(x)g(x)+f(x)g'(x) etc. If you implement that all, you would get an insane program Smile

Maybe I will give it a try. I once started such program, but never finished it Very Happy
mr womp womp wrote:
A little while ago I was sitting in calculus and thought that since the ti-84s could calculate derivatives at a point, the calculator could calculate it on a certain interval and then, that data could be used to try and work out the derivative of a function. I quickly put together a little program that has a billion issues but sort of works for simple inputs. here is the code...
Code:
ClrList L1
ClrList L2
ClrHome
Input "f(x)=",Str1
String>Equ(Str1,{Y1}
For(A,1,5
   nDeriv({Y1},X,A)->L1(1+dim(L1
   A->L2(1+dim(L2
End
If inString(Str1,"sin(") or inString(Str1,"cos(
Then
   SinReg L2,L1,{Y1}
   Else
   If inString(Str1,"^X
   Then
      ExpReg L2,L1,{Y1}
      Else
      QuartReg L2,L1,{Y1}
End:End
Equ>String({Y1},Str1
Output(3,1,Str1
Pause

Of course, the methods used are very bad and I can think of a million functions for which the program would not work, but nonetheless, it can get some basic stuff done and at only 160 bytes, I think it's worth discussing Razz
Basically, it just calculates the derivative at five points, then using the built-in regression methods, tries to fit the appropriate type of function to it. As one might expect, the output is often very funky but correct (for the few functions that are valid input, which are basically the family of functions that have a built-in regression method)
It can work out the derivatives of polynomials up to degree 5 (since the derivative is a degree 4 polynomial and it uses quartreg) which includes linear functions, it can do sin/cos function with SinReg since any cos function can be expresed as a sine function, and it can do exponential functions, which is probably the most interesting part because working these out by hand requires logarithmic differentiation which means the program could actually be useful. However it can't handle composite functions such as f(x) = sin(x) + (3x³ - 5x² + 4x + 3) even though this would probably be very easy to implement for addition and subtraction of functions.
Here is a screenshot of the derivative of sin(x)



If you squint a little, you can see a cos(x) hidden deep in there 0x5
Also, I'm quite surprised to see how big the rounding errors become, as you can see at the beginning of the output, the rounding errors are not just the last one or two decimal places, but rather the last 8!
This is not a project of mine, I just thought I'd share this because I couldn't find a topic about it and I thought it was interesting even thought it is nowhere near what one would expect a derivative calculator to do.


Don't forget to put the ")" on your For( loop! It saves time (but not space), it's better code practice. Anything else could have the ")" omitted.
I believe this only applies if the first line in the for loop could skip a line if a condition is false, which is either a lone If statement, or the DS<(, IS>( commands, which I am not currently using Smile
The nDeriv( function estimates the derivative with the formula on the linked page, (f(x+h)-f(x-h))/(2h). Since the default value of h is 10^-3, the error in the derivative is proportional to the square of that times the third derivative of your function (see https://en.wikipedia.org/wiki/Numerical_differentiation, or on the order of 10^-7. Using the optional 4th argument to nDeriv( can reduce this error somewhat.

By the way, ")" only speeds up a For( loop when the first line inside the For( is a single-line If statement that's false.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement