# HP Forums

Full Version: HP Prime: in need of help with defining functions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

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

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.

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.

In comparison, How easy is all this with the 50G! Define a function, and use it in your program - - no problem!

Am I right?

Yes (sorry to say).

Hope HP listen up!

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

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.

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

A working solution in CAS module would be :

f(x):= 1/(1+x^2)^(1/2)

f2:=unapply(diff(f(x),x\$2),x)

Edited: 26 Oct 2013, 3:30 a.m.

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.

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

This was very helpful, thank you!

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.

```diff(f(x),x\$n)
```

is the nth derivative of f.

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.

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.