Newton's method - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP Prime Software Library (/forum-15.html) +--- Thread: Newton's method (/thread-9783.html) |
Newton's method - Namir - 12-27-2017 03:41 AM This is a version of Newton's method for the HP Prime where you specify a string representing f(x), along with the initial guess for the root, and the tolerance value. Thus you can solve for different functions without having to hard code these functions in the RPL code. Here is the source code: Code: EXPORT NEWTON(sfx, x, toler) Examples of using function NEWTON are: NEWTON("exp(x)-3*x^2",4,0.0000001) NEWTON("ln(100)-x",4,0.0000001) The code convert the string of the expression into lowercase to make sure it uses variable x in lowercase, to match the lowercase parameter x. Also, the code controls the number of iterations not to exceed 99. You can insert several function declarations with the above code and then pass a string containing the call to a particular function. For example, if we insert the following code before NEWTON: Code: EXPORT fx1(x) Then you can make the following calls: NEWTON("fx1(x),4,0.0000001) NEWTON("fx2(x)",4,0.0000001) Which give the same results as before. This approach allows you to insert a catalogue of functions you frequently work and then invoke the solution for their roots by specifying the function's name instead of its expression. In the case the function requires more than a single statement to calculate the result, then this approach is definitely the one to choose! If you comment out the statement that converts parameter sfx to lower case, then you can specify the Functions App's F0 through F9. Make sure that you use formats like "F1(x)" with the lowercase x. Enjoy! Namir RE: Newton's method - Mark Power - 01-01-2018 03:49 PM Hi Namir, I like the compactness of the solution! I hope you don't mind a few tweaks that perhaps make it better:
Code:
With these changes, if you have functions in the Function App that plot nicely like for example F1(X)=17*SIN(1.26*X-90)-5, then you can run NEWTON("F1(X)",1,0.00000001) and find the root 1.85233166719. |