HP Forums
CAS.diff command results diff command again - 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: CAS.diff command results diff command again (/thread-8480.html)



CAS.diff command results diff command again - josephec - 06-09-2017 10:45 PM

Hi Friends,
I am working with diff command BUT using variable name that stores a function F(X) some results include diff command again

Using diff in HOME direct goes fine:
diff(SEC(X)-1,X)
returns
SIN(X)/COS(X)^2

The problem is if the function is stored in a variable in this case fx
fx:='SEC(X)-1'
diff(fx,X)
returns
diff(SEC(X),X)

returns diff as part of result.... then HOW TO GET the final derivative using variable ?

Pd. I use diff in a program to calculate derivative then show it to user and evaluate in some X points.

Thanks for your answer. Best regards!!
joseph


RE: CAS.diff command results diff command again - roadrunner - 06-10-2017 02:43 PM

Have you tried:

EVAL(diff(fx,X))

-road


RE: CAS.diff command results diff command again - josephec - 06-10-2017 04:46 PM

(06-10-2017 02:43 PM)roadrunner Wrote:  Have you tried:

EVAL(diff(fx,X))

-road

Thanks roadrunner,
I alredy tried EVAL(diff(fx,X))
The problem is if fx has funtion like 'SEC(X)+X^2'
diff returns diff(SEC(X),X)+2*X and EVAL will solve the diff issue but also will replace the X with its value.

I appreciate this and future suggestions.
Best regards!!
jose


RE: CAS.diff command results diff command again - roadrunner - 06-11-2017 12:29 AM

The only way I can think of at the moment is to do it in CAS with small x's and then substitute in the big X's, try this from the home screen:

CAS("fx:=SEC(x)+x^2")
CAS("subst(eval(diff(fx,x)),x='X')")

You should get:

(SIN(X)/COS(X)^2)+2*X

-road


RE: CAS.diff command results diff command again - toml_12953 - 06-11-2017 01:54 AM

(06-10-2017 04:46 PM)josephec Wrote:  
(06-10-2017 02:43 PM)roadrunner Wrote:  Have you tried:

EVAL(diff(fx,X))

-road

Thanks roadrunner,
I alredy tried EVAL(diff(fx,X))
The problem is if fx has funtion like 'SEC(X)+X^2'
diff returns diff(SEC(X),X)+2*X and EVAL will solve the diff issue but also will replace the X with its value.

I appreciate this and future suggestions.
Best regards!!
jose

I get the correct answer with eval and X keeps its old value. I set X:=2 then did EVAL(diff(fx,X)). That worked then when I typed in X and Enter
I got 2 back.

Tom L


RE: CAS.diff command results diff command again - josephec - 06-13-2017 01:58 AM

(06-11-2017 12:29 AM)roadrunner Wrote:  The only way I can think of at the moment is to do it in CAS with small x's and then substitute in the big X's, try this from the home screen:

CAS("fx:=SEC(x)+x^2")
CAS("subst(eval(diff(fx,x)),x='X')")

You should get:

(SIN(X)/COS(X)^2)+2*X

-road

Hi Roadrunner,
Thank You so much, WORKS GREAT...
as user enter X and no x, I used:
dfx:=CAS("subst(eval(diff(subst(fx,'X'=x),x)),x='X')");
then dfx will hold the final derivative with X variable to show to user and continue using it in my program
Thanks and have an excellent weekend!
joseph


RE: CAS.diff command results diff command again - josephec - 06-13-2017 02:04 AM

(06-11-2017 01:54 AM)toml_12953 Wrote:  
(06-10-2017 04:46 PM)josephec Wrote:  Thanks roadrunner,
I alredy tried EVAL(diff(fx,X))
The problem is if fx has funtion like 'SEC(X)+X^2'
diff returns diff(SEC(X),X)+2*X and EVAL will solve the diff issue but also will replace the X with its value.

I appreciate this and future suggestions.
Best regards!!
jose

I get the correct answer with eval and X keeps its old value. I set X:=2 then did EVAL(diff(fx,X)). That worked then when I typed in X and Enter
I got 2 back.

Tom L

Hi Tom
Thanks for trying and suggesting...

Quote:diff returns diff(SEC(X),X)+2*X and EVAL will solve the diff issue but also will replace the X with its value.
I mean that X´s value is replaced in the +2*X part ... but the solucion offered by Roadrunner works great using
CAS("fx:=SEC(x)+x^2")
CAS("subst(eval(diff(fx,x)),x='X')")
I appreciate Your time and suggestion.
Have an excellent week.
joseph


RE: CAS.diff command results diff command again - roadrunner - 06-15-2017 07:00 PM

Here is a function that will differentiate equations written with HOME variables WITHOUT substituting in the value contained in the variable:

PHP Code:
EXPORT HOMEDIFF(z,n)
BEGIN
 LOCAL a
;
 
a:=CAS("subst(z,n=t25369a)");
 RETURN 
CAS(EVAL("subst("+simplify(CAS(EVAL("diff("+a+",QUOTE(t25369a))")))+",t25369a=n)"));
END

example 1:

fx:='SEC(X)+X^2'
HOMEDIFF(fx,'X')

returns:

(2*X*SIN(X)^2-2*X-SIN(X))/(SIN(X)^2-1)

example 2:

HOMEDIFF('X^2+LN(SIN(X))','X')

returns:

(2*X*SIN(X)+COS(X))/SIN(X)

There are a few caveats:

1. It doesn't always work on the handheld calculator. It only works on the emulator and apps. Presumably, if the handheld is ever updated, it should start working there;

2. It doesn't work if you have either a CAS or user variable named t25369a;

3. Simplification isn't very good;

4. You have to use a valid CAS equation: X^3.2 won't work, X^(32/10) will work.

-road

edited to add caveat #4 and to correct a grammatical error


RE: CAS.diff command results diff command again - josephec - 06-16-2017 04:49 PM

Hi Roadrunner,

Thanks for the detailed solution, simplification is not necesary for now... we hope more development in HP Prime CAS,

Best regards!!
joseph

(06-15-2017 07:00 PM)roadrunner Wrote:  Here is a function that will differentiate equations written with HOME variables WITHOUT substituting in the value contained in the variable:

PHP Code:
EXPORT HOMEDIFF(z,n)
BEGIN
 LOCAL a
;
 
a:=CAS("subst(z,n=t25369a)");
 RETURN 
CAS(EVAL("subst("+simplify(CAS(EVAL("diff("+a+",QUOTE(t25369a))")))+",t25369a=n)"));
END

example 1:

fx:='SEC(X)+X^2'
HOMEDIFF(fx,'X')

returns:

(2*X*SIN(X)^2-2*X-SIN(X))/(SIN(X)^2-1)

example 2:

HOMEDIFF('X^2+LN(SIN(X))','X')

returns:

(2*X*SIN(X)+COS(X))/SIN(X)

There are a few caveats:

1. It doesn't always work on the handheld calculator. It only works on the emulator and apps. Presumably, if the handheld is ever updated, it should start working there;

2. It doesn't work if you have either a CAS or user variable named t25369a;

3. Simplification isn't very good;

4. You have to use a valid CAS equation: X^3.2 won't work, X^(32/10) will work.

-road

edited to add caveat #4 and to correct a grammatical error



RE: CAS.diff command results diff command again - Tim Wessman - 06-16-2017 05:27 PM

Well, this is what I have that works here for me:

[Image: expression.png]

[Image: test.png]

The program is:

EXPORT fx,fxPrime;

EXPORT diffTest()
BEGIN
INPUT({{fx,[8]}},"Enter Expression");
fxPrime:=diff(fx,X);
END;


Not sure if Bernard added something to the CAS, but I'm guessing a slight tweak somewhere may be responsible. Everything seems to be working as expected?