Post Reply 
CAS.diff command results diff command again
06-09-2017, 10:45 PM
Post: #1
CAS.diff command results diff command again
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


Attached File(s) Thumbnail(s)
   
Find all posts by this user
Quote this message in a reply
06-10-2017, 02:43 PM
Post: #2
RE: CAS.diff command results diff command again
Have you tried:

EVAL(diff(fx,X))

-road
Find all posts by this user
Quote this message in a reply
06-10-2017, 04:46 PM
Post: #3
RE: CAS.diff command results diff command again
(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
Find all posts by this user
Quote this message in a reply
06-11-2017, 12:29 AM
Post: #4
RE: CAS.diff command results diff command again
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
Find all posts by this user
Quote this message in a reply
06-11-2017, 01:54 AM
Post: #5
RE: CAS.diff command results diff command again
(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

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
06-13-2017, 01:58 AM
Post: #6
RE: CAS.diff command results diff command again
(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
Find all posts by this user
Quote this message in a reply
06-13-2017, 02:04 AM
Post: #7
RE: CAS.diff command results diff command again
(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
Find all posts by this user
Quote this message in a reply
06-15-2017, 07:00 PM (This post was last modified: 06-15-2017 07:33 PM by roadrunner.)
Post: #8
RE: CAS.diff command results diff command again
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
Find all posts by this user
Quote this message in a reply
06-16-2017, 04:49 PM
Post: #9
RE: CAS.diff command results diff command again
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
Find all posts by this user
Quote this message in a reply
06-16-2017, 05:27 PM
Post: #10
RE: CAS.diff command results diff command again
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?

TW

Although I work for HP, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)