HP Forums
using diff() in a program - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: using diff() in a program (/thread-6222.html)



using diff() in a program - hpfx - 05-08-2016 09:18 AM

Hi,
I made a program that need to derivate a function, I don't understand why it does not work.
when I'm in home, I can type diff(3*X+1) I get the expected result.
But when I do the same in a program, I got error.

I tried all kind of possible syntax to make it work.
here is a simple example :
Code:
EXPORT ABCD()
BEGIN
'3*X+1'▶F9;
PRINT(diff(F9));
END;
as you can see, I use F9 to see if expression is correctly entered,
but I get this : "program(X,0,3)" instead.

is it a bug ???

FW version 10077


RE: using diff() in a program - roadrunner - 05-08-2016 11:15 AM

Try these changes:

1. change X to lower case
2. use double quotes for the equation
3. store it in a local variable
4. run diff from the CAS

Code:
EXPORT ABCD()
 BEGIN
 LOCAL temp;
 "3*x+1"▶temp;
 PRINT(CAS.diff(temp));
END;

Everything mentioned may not be required, but those are rules I generally follow.

In the articles section Mr. Han has a good post about running CAS functions from a program.

-road


RE: using diff() in a program - jrozsas - 05-08-2016 01:13 PM

(05-08-2016 11:15 AM)roadrunner Wrote:  Try these changes:

1. change X to lower case
2. use double quotes for the equation
3. store it in a local variable
4. run diff from the CAS

Code:
EXPORT ABCD()
 BEGIN
 LOCAL temp;
 "3*x+1"▶temp;
 PRINT(CAS.diff(temp));
END;

Everything mentioned may not be required, but those are rules I generally follow.

In the articles section Mr. Han has a good post about running CAS functions from a program.

-road
does not work
[Image: erro10.png]


RE: using diff() in a program - informach - 05-08-2016 02:13 PM

Hi!, hpfx:

Syntax:
diff(Expr,[Var or ListVar])

Description:
Returns, the derivative, of an expression, with respect to, a given variable.

d/dx=(3*x+1)=3

Kind Regards.
informach.


RE: using diff() in a program - DrD - 05-08-2016 03:03 PM

Code:

EXPORT ABCD()
BEGIN
  F9:="3*X+1";
  RETURN diff(F9,X);  // Returns 3
END;



RE: using diff() in a program - hpfx - 05-08-2016 08:47 PM

Thank you,
But I still have an issue,
in fact the expression comes from an input.
in that case, I saw that I must use a very strange INPUT syntax {{var,[2]}} in someone else post.

but when I do that :
Code:

EXPORT ABCD()
 BEGIN
 LOCAL a;
 INPUT({{a,[2]}});
 PRINT(diff(a,X));
 END;
I got Error: Bad Argument Type

my a var is supposed to be a string, like F9 in DrD code ?

I don't get the logic here...



Also INPUT builtin help is incomplete and wrong.

syntax : INPUT(var,...) *1st form syntax
and
var -> { var_name, [allowed_type_matrix] ... } *2nd form of var definition.

according this, I should be able to type INPUT({var_name},[types]})
but it gives an error ! I must type INPUT({{var_name, [types]}})

also, help is not complete, where is the list of types ? how can we guess ?


RE: using diff() in a program - DrD - 05-08-2016 09:46 PM

Code:

EXPORT ABCD()
BEGIN
  LOCAL a;
  INPUT({{a,[2]}}); //  You don't need to quote your input, since type [2] is a string var
  //F9:="3*X+1";
  RETURN diff(EVAL(a),X);  //  You need to EVAL(a).
END;

Use: 3*X+1 as input to a ==> returns 3


RE: using diff() in a program - roadrunner - 05-08-2016 11:56 PM

(05-08-2016 01:13 PM)jrozsas Wrote:  does not work
That's strange, it works here. What version software are you running? I'm running 8151.

-road

edit: upon further investigation it seems that code will only run on 8151


RE: using diff() in a program - informach - 05-09-2016 12:14 AM

Hi!, hpfx:

You can see, this link of (Eddie Shore), for learn, the use, of INPUT ... http://edspi31415.blogspot.com.ar/2014/05/hp-prime-ways-to-use-input-command.html
However, it's are some examples and the 6030 version.

Kind Regards.
informach.


RE: using diff() in a program - cyrille de brébisson - 05-09-2016 05:16 AM

Hello

>in that case, I saw that I must use a very strange INPUT syntax {{var,[2]}} in someone else post.

INPUT is a home command, and in home, you can not use a variable that has not been defined.
so, INPUT is (normally) unable to let you enter an equation in 'x' if 'x' does not exits.
INPUT takes as first argument a variable and let you request it from the user.
but INPUT can also take 2 or more variables, using {var1,.. varn} syntax.
Furthermore, you can tell INPUT that the variable requested have to follow a given type or types. This is done by placing the variable in a list and having the allowed types as a matrix in the 2nd element of the list.
It is only to make it easier for the system to distinguish between the use of the 3 syntaxes that you are forced, if you want to specify the type for one single variable to still create a list of list...

anyhow, here the 2 means string. So it forces the input to be a string. and strings, contrary to expression can contain anything you want, including the text of undefined variables.

This is why you use {{var [2]}}

Cyrille


RE: using diff() in a program - jrozsas - 05-09-2016 12:52 PM

(05-08-2016 11:56 PM)roadrunner Wrote:  
(05-08-2016 01:13 PM)jrozsas Wrote:  does not work
That's strange, it works here. What version software are you running? I'm running 8151.

-road

edit: upon further investigation it seems that code will only run on 8151
version 10077


RE: using diff() in a program - roadrunner - 05-09-2016 01:05 PM

Jrozsas,

Cyrille's document explains everything, located here:

http://www.hpmuseum.org/forum/thread-6218.html

Below is a modified program that works with 10077:

Code:
EXPORT ABCD()
BEGIN
 LOCAL temp;
 temp:="3*x^2+1";
 CAS.diff(EVAL(temp),"x");
END;

-road


RE: using diff() in a program - leprechaun - 05-10-2016 09:56 AM

There are some citations I found very important.

1)
Cas function calls from home, using fully qualified names or not, have all the parameters defined as ‘do not evaluate’.
->This seems to be the main subject to change in the latest update. You need to EVAL() the local variable for the call. Otherwise it is just the name that is used. That is where EVAL() and QUOTE() come in.

2)
This is solved by allowing you to use strings as parameters for the CAS. These strings will be parsed at the time of the CAS function call.
->As usual I would say

3 -(local variables)
These are temporary and local to this specific call of the function.
->Something that personally I was not aware of. My intuition dictated that a local variable would be passed by value, but it seems that a local variable is indeed invisible outside the function.

Please correct me if I am talking nonsense. i don't want to baffle people but sometimes the view of a different person can be enlightening.