HP Prime: in need of help with defining functions



#16

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


#17

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.


#18

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!


#19

Am I right?


#20

Yes (sorry to say).


#21

Hope HP listen up!

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

#22

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 ...)

#23

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.


#24

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.


#25

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


#26

This was very helpful, thank you!

#27

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.


#28

diff(f(x),x$n)

is the nth derivative of f.


#29

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.


#30

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.

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

Forum Jump: