HP Prime: in need of help with defining functions - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: HP Prime: in need of help with defining functions (/thread-253878.html) HP Prime: in need of help with defining functions - Alberto Candel - 10-25-2013 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 Re: HP Prime: in need of help with defining functions - bluesun08 - 10-25-2013 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. Re: HP Prime: in need of help with defining functions - Helge Gabert - 10-25-2013 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! Re: HP Prime: in need of help with defining functions - bluesun08 - 10-25-2013 Am I right? Re: HP Prime: in need of help with defining functions - Helge Gabert - 10-25-2013 Yes (sorry to say). Re: HP Prime: in need of help with defining functions - bluesun08 - 10-25-2013 Hope HP listen up! Edited: 26 Oct 2013, 12:50 a.m. Re: HP Prime: in need of help with defining functions - Alberto Candel - 10-26-2013 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 ...) Re: HP Prime: in need of help with defining functions - Olivier Lecluse - 10-26-2013 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. Re: HP Prime: in need of help with defining functions - Olivier Lecluse - 10-26-2013 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. Re: HP Prime: in need of help with defining functions - Olivier Lecluse - 10-26-2013 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 Re: HP Prime: in need of help with defining functions - Alberto Candel - 10-26-2013 This was very helpful, thank you! Re: HP Prime: in need of help with defining functions - Gilles Carpentier - 10-26-2013 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. Re: HP Prime: in need of help with defining functions - Alberto Candel - 10-26-2013 ```diff(f(x),x\$n) ``` is the nth derivative of f. Re: HP Prime: in need of help with defining functions - Gilles Carpentier - 10-27-2013 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. Re: HP Prime: in need of help with defining functions - Alberto Candel - 10-27-2013 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.