HP Prime: in need of help with defining functions Alberto Candel Member Posts: 72 Threads: 16 Joined: Sep 2013 10-25-2013, 04:39 PM Hello, I would appreciate some help on defining functions. In CAS, I defined ```f(x):= 1/(1+x^2)^(1/2) ``` Then I computed the second derivative ```diff(f(x), x\$2) ``` and defined a new function ```f2(x):=diff(f(x),x\$2) ``` Now when trying to evaluate `f2(1)` I get an error. I cannot figure out what I am missing. Thank you bluesun08 Member Posts: 207 Threads: 48 Joined: Feb 2010 10-25-2013, 05:26 PM In my opinion defining and working with functions/variables is a disappointing thing in HP Prime. There is no consistence in CAS-/Home-View/Function-App and upper case/lower case letters. It is confusing and not very good for an education tool. Helge Gabert Member Posts: 113 Threads: 20 Joined: Sep 2013 10-25-2013, 08:39 PM The only sane way (for me, anyway) is to do this through pre-defined function names F0,...,F9. Call diff(F0(x),x) store in F1 Call diff(F1(x),x) store in F2 or, faster, call hessian(F0(x),[x]) store in F2, and you can evaluate F2(1). It doesn't seem like user defined functions can readily be used with CAS commands. Too bad! In comparison, How easy is all this with the 50G! Define a function, and use it in your program - - no problem! bluesun08 Member Posts: 207 Threads: 48 Joined: Feb 2010 10-25-2013, 09:09 PM Am I right? Helge Gabert Member Posts: 113 Threads: 20 Joined: Sep 2013 10-25-2013, 09:25 PM Yes (sorry to say). bluesun08 Member Posts: 207 Threads: 48 Joined: Feb 2010 10-25-2013, 09:29 PM Hope HP listen up! Edited: 26 Oct 2013, 12:50 a.m. Alberto Candel Member Posts: 72 Threads: 16 Joined: Sep 2013 10-26-2013, 12:36 AM Quote: The only sane way (for me, anyway) is to do this through pre-defined function names F0,...,F9. Call diff(F0(x),x) store in F1 Call diff(F1(x),x) store in F2 or, faster, call hessian(F0(x),[x]) store in F2, and you can evaluate F2(1). It doesn't seem like user defined functions can readily be used with CAS commands. Too bad! In comparison, How easy is all this with the 50G! Define a function, and use it in your program - - no problem! Thank you! Your last sentence points to my follow-up question: is there a way to call CAS functions and commands from within HP Basic programs? (it looks like not ...) Olivier Lecluse Junior Member Posts: 12 Threads: 2 Joined: Oct 2013 10-26-2013, 03:24 AM A working solution in CAS module would be : f(x):= 1/(1+x^2)^(1/2) f2:=unapply(diff(f(x),x\$2),x) f2(1) gives the desired answer Edited: 26 Oct 2013, 3:30 a.m. Olivier Lecluse Junior Member Posts: 12 Threads: 2 Joined: Oct 2013 10-26-2013, 03:42 AM A little explanation from what I think I understood... First, you have to make a distinction between functions and expressions : expression:=x^2+x+1 is an expression f(x):=x^2+x+1 is a function The difference beetween these guys is that you can't evaluate expression(2). If you do want, you'll have to call subst(expression,x=2) Now you can convert this expression into a function, but be aware that when you define a function with f(x):=expression, the right member is not evaluated : you define f: x --> expression If you want the expression to be replaced with its value, you want to call unapply : f:=unapply(expression,x). Then you can call f(2) The same mechanism is in use when you use the diff command : the result of the diff command is an expression when you type f1:=diff(f(x)), the diff command gives you an expression and you get into trouble. f1:=unapply(diff(f(x)),x) evaluates the diff command and stores the result into the f1 function that is what you want here. Alternatively, you could use function_diff that gives you in return a function and not an expression : f1:=function_diff(f) Edited: 26 Oct 2013, 3:46 a.m. Olivier Lecluse Junior Member Posts: 12 Threads: 2 Joined: Oct 2013 10-26-2013, 03:59 AM Here is an example that helped me understand this : f(x):=x^2+x+1 f1:=x->int(f(x)) displays (x)->int(f(x)) So int(f(x)) is not evaluated. The trap here is that f1(x) gives you the desired answer because f(x) returns the expression of the function and THEN the int operator gives you the primitive. But f1(1) is int(f(1)) so the CAS evaluates f(1)=3 and then int(3)=3x. So f1(1) returns 3*x... In this case, you want to use the unapply function : f1:=unapply(int(f(x)),x) displays (x)->(x^3/3+x^2/2+x) so the correct expression is stored into the f1 function and everything performs as expected : f1(1) equals 11/6 Alberto Candel Member Posts: 72 Threads: 16 Joined: Sep 2013 10-26-2013, 12:13 PM This was very helpful, thank you! Gilles Carpentier Senior Member Posts: 468 Threads: 17 Joined: May 2011 10-26-2013, 06:09 PM An easy way to do this in CAS : f(x):= 1/(1+x^2)^(1/2) f2:=f' By the way, what is x\$2 in you example ? I don't understand what diff(f(x),x\$2,x) is supposed to do.... Edited: 26 Oct 2013, 6:16 p.m. Alberto Candel Member Posts: 72 Threads: 16 Joined: Sep 2013 10-26-2013, 07:15 PM ```diff(f(x),x\$n) ``` is the nth derivative of f. Gilles Carpentier Senior Member Posts: 468 Threads: 17 Joined: May 2011 10-27-2013, 04:31 AM OK ! I didn't know this syntax... you can also do f' f'' f''' ex f2:=f'' define f2 as the second derivative of f or f'' STO> f2 (less keystrokes) Edited: 27 Oct 2013, 4:35 a.m. Alberto Candel Member Posts: 72 Threads: 16 Joined: Sep 2013 10-27-2013, 10:48 AM I know about the repeated ' for derivatives, but I am working on a program for Pade approximants and want to have `n` as a variable in `diff(f(x),\$(x,n))` The ' will not do it I think. « Next Oldest | Next Newest »

 Possibly Related Threads... Thread Author Replies Views Last Post Prime: Scope of Variable and functions within programs Thomas Chrapkiewicz 9 1,141 11-18-2013, 03:39 PM Last Post: Thomas Chrapkiewicz HP Prime - CAS functions in Spreadsheet App CR Haeger 6 937 11-11-2013, 12:37 AM Last Post: Michael de Estrada [41CL] New Extra Functions version Monte Dalrymple 0 404 11-08-2013, 04:32 PM Last Post: Monte Dalrymple HP Prime - Defining a function bluesun08 5 751 10-23-2013, 02:43 PM Last Post: Han HP Prime spreadsheet functions SanS 0 858 10-04-2013, 04:23 AM Last Post: SanS Stats functions on the HP34S Nicholas van Stigt 5 737 09-24-2013, 02:45 AM Last Post: Nick_S Trig Functions Howard Owen 11 1,226 09-16-2013, 02:53 PM Last Post: Fred Lusk 50g piecewise functions Kurt Fankhauser 6 766 09-15-2013, 08:01 PM Last Post: Kurt Fankhauser Computer-scientist functions on HP Prime? Michael O. Tjebben 12 1,249 08-22-2013, 06:59 PM Last Post: plivesey Missing functions on the HP Prime!!!??? :-( Namir 6 763 08-22-2013, 08:40 AM Last Post: Gilles Carpentier

Forum Jump: