The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

HP Prime: in need of help with defining functions
Message #1 Posted by Alberto Candel on 25 Oct 2013, 4:39 p.m.

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
Message #2 Posted by bluesun08 on 25 Oct 2013, 5:26 p.m.,
in response to message #1 by Alberto Candel

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
Message #3 Posted by Helge Gabert on 25 Oct 2013, 8:39 p.m.,
in response to message #2 by bluesun08

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
Message #4 Posted by bluesun08 on 25 Oct 2013, 9:09 p.m.,
in response to message #3 by Helge Gabert

Am I right?

                        
Re: HP Prime: in need of help with defining functions
Message #5 Posted by Helge Gabert on 25 Oct 2013, 9:25 p.m.,
in response to message #4 by bluesun08

Yes (sorry to say).

                              
Re: HP Prime: in need of help with defining functions
Message #6 Posted by bluesun08 on 25 Oct 2013, 9:29 p.m.,
in response to message #5 by Helge Gabert

Hope HP listen up!

Edited: 26 Oct 2013, 12:50 a.m.

                  
Re: HP Prime: in need of help with defining functions
Message #7 Posted by Alberto Candel on 26 Oct 2013, 12:36 a.m.,
in response to message #3 by Helge Gabert

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
Message #8 Posted by Olivier Lecluse on 26 Oct 2013, 3:24 a.m.,
in response to message #1 by Alberto Candel

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
Message #9 Posted by Olivier Lecluse on 26 Oct 2013, 3:42 a.m.,
in response to message #8 by Olivier Lecluse

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
Message #10 Posted by Olivier Lecluse on 26 Oct 2013, 3:59 a.m.,
in response to message #9 by Olivier Lecluse

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
Message #11 Posted by Alberto Candel on 26 Oct 2013, 12:13 p.m.,
in response to message #10 by Olivier Lecluse

This was very helpful, thank you!

      
Re: HP Prime: in need of help with defining functions
Message #12 Posted by Gilles Carpentier on 26 Oct 2013, 6:09 p.m.,
in response to message #1 by Alberto Candel

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
Message #13 Posted by Alberto Candel on 26 Oct 2013, 7:15 p.m.,
in response to message #12 by Gilles Carpentier

diff(f(x),x$n)

is the nth derivative of f.

                  
Re: HP Prime: in need of help with defining functions
Message #14 Posted by Gilles Carpentier on 27 Oct 2013, 4:31 a.m.,
in response to message #13 by Alberto Candel

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
Message #15 Posted by Alberto Candel on 27 Oct 2013, 10:48 a.m.,
in response to message #14 by Gilles Carpentier

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.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall