CAS diff() giving incorrect results
09-22-2021, 07:10 PM (This post was last modified: 09-23-2021 02:57 AM by Wes Loewer.)
Post: #1
 Wes Loewer Senior Member Posts: 427 Joined: Jan 2014
CAS diff() giving incorrect results
A couple of years ago I posted about a problem with the diff() command (https://www.hpmuseum.org/forum/thread-13532.html). The problem still exists in the most recent firmware.

I guess I had forgotten about the issue because I ran into the problem again yesterday, only it's worse than I original thought. The problem arises when you try to evaluate a derivative at a value, which is something that frequently comes up in calculus and physics.

For instance, evaluating the 2nd derivative of x^3 at x=4.

diff(x^3,x,2)|x = 4

which should evaluate to 6*x|x=4 which is 24. However, the CAS substitutes 4 in for x BEFORE differentiating, so the expression is interpreted as

diff(4^3,4,2) = diff(64,4,2)

which is meaningless.

Even worse than no answer is giving the wrong answer. If the value of x contains pi, then you get incorrect results.

diff(x^3+π^4,x)|x = π

should result in 3*x^2|x=π which is 3*π^2. Instead, the line is interpreted as

diff(π^3+π^4,π)

and now π is treated as a variable. So the result is 3*π^2+4*π^3.

This issue came to light yesterday when I was checking my test answer key and entered this (using primes which is equivalent to diff).

(x^2*cos(x))''|x = π

evaluated to -2 instead of π^2-2. I assumed that I had made some silly mistake in my work by hand. It took several repeated attempts before I concluded that I was correct and the calculator was wrong.

I realize I could use the d/dx template which works correctly, but there is no template for 2nd or higher order derivatives and

(∂(∂(x^2*cos(x),x),x)|(x = π))

looks really awkward (especially in Textbook mode) and using the ' (primes) is so very convenient. It's really an order of operations issue in which |x=a should be applied AFTER the diff() command has completed, not BEFORE.

And it seems only natural that the HP "Prime" should be able to correctly work with "primes". 09-22-2021, 07:26 PM
Post: #2
 parisse Senior Member Posts: 1,272 Joined: Dec 2013
RE: CAS diff() giving incorrect results
I have already tried to make | work as you expect with diff, but that was not successfull, it's too complicated. Please use subst
Code:
 subst(diff(x^3,x,2),x = 4)
or just parentheses like
Code:
 diff(x^3,x,2)(x = 4)
09-23-2021, 06:20 PM
Post: #3
 Wes Loewer Senior Member Posts: 427 Joined: Jan 2014
RE: CAS diff() giving incorrect results
(09-22-2021 07:26 PM)parisse Wrote:  I have already tried to make | work as you expect with diff, but that was not successfull, it's too complicated. Please use subst

Okay, thanks for that info. That's good to know.

Quote:or just parentheses like
Code:
 diff(x^3,x,2)(x = 4)

That's an interesting notation. It appears to work for other functions as well. Is there any way to use it with primes? When I try (x^3)''(x = 4) it gives a warning and returns (6*x) = 24, with the expression on the left and the result on the right. Is this the intended behavior?
09-23-2021, 07:26 PM
Post: #4
 toml_12953 Senior Member Posts: 2,040 Joined: Dec 2013
RE: CAS diff() giving incorrect results
(09-23-2021 06:20 PM)Wes Loewer Wrote:
(09-22-2021 07:26 PM)parisse Wrote:  I have already tried to make | work as you expect with diff, but that was not successfull, it's too complicated. Please use subst

Okay, thanks for that info. That's good to know.

Quote:or just parentheses like
Code:
 diff(x^3,x,2)(x = 4)

That's an interesting notation. It appears to work for other functions as well. Is there any way to use it with primes? When I try (x^3)''(x = 4) it gives a warning and returns (6*x) = 24, with the expression on the left and the result on the right. Is this the intended behavior?

For that expression, the pipe does work

(x^3) | (x = 4) 64

Tom L
Cui bono?
09-24-2021, 10:48 AM
Post: #5
 Albert Chan Senior Member Posts: 2,231 Joined: Jul 2018
RE: CAS diff() giving incorrect results
(09-23-2021 06:20 PM)Wes Loewer Wrote:  When I try (x^3)''(x = 4) it gives a warning and returns (6*x) = 24,

When I try same expression in the emulator, above return only 24, without warning.
I beiieve (x^3)''(x = 4) equivalent to subst((x^3)'', x = 4), only less typing.

Quote:That's an interesting notation. It appears to work for other functions as well.

If the expression is not a function, the notation may get confused with implied multiply.

XCAS> (1+x+x^2) (x=0.1)

Warning : using implicit multiplication for (1+x+x^2)(x=0.1)
x*(x^2+x+1)=(0.1*(x^2+x+1))

CAS> (1+x+x^2) (x=0.1) ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ → "Syntax Error"

We can get around the problem (both CAS and XCAS) with identity function.

CAS> id(1+x+x^2) (x=0.1) ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ → 1.11
09-24-2021, 04:28 PM
Post: #6
 Wes Loewer Senior Member Posts: 427 Joined: Jan 2014
RE: CAS diff() giving incorrect results
(09-24-2021 10:48 AM)Albert Chan Wrote:
(09-23-2021 06:20 PM)Wes Loewer Wrote:  When I try (x^3)''(x = 4) it gives a warning and returns (6*x) = 24,

When I try same expression in the emulator, above return only 24, without warning.
I beiieve (x^3)''(x = 4) equivalent to subst((x^3)'', x = 4), only less typing.

That's interesting. That's exactly what I was hoping to accomplish.
What version are you using? I tried this on the following versions:

2020-01-16 (HW-A, VC)
no warning, returns 24

2020-09-08 (HW-A), 2021-05-05 (HW-D), 2021-06-09 (VC)
has warning, returns 6*x=24
09-24-2021, 05:23 PM
Post: #7
 Albert Chan Senior Member Posts: 2,231 Joined: Jul 2018
RE: CAS diff() giving incorrect results
Hi, Wes Loewer

It was an old version: Build: 2.1.14181 (2018 10 16)
09-24-2021, 07:59 PM
Post: #8
 Wes Loewer Senior Member Posts: 427 Joined: Jan 2014
RE: CAS diff() giving incorrect results
(09-24-2021 05:23 PM)Albert Chan Wrote:  It was an old version: Build: 2.1.14181 (2018 10 16)

Okay, I guess something changed since then.
09-25-2021, 04:04 PM
Post: #9
 parisse Senior Member Posts: 1,272 Joined: Dec 2013
RE: CAS diff() giving incorrect results
f(x=...) will work if f is an expression. It will not work with e.g. (x^3)'' because the parser will not detect that it's an expression (and it will do like if it was implicit multiplication because x appears in both).
If you work with derivatives, it's a good idea to save them in variables. First f:=x^3'', then f(x=4)
09-25-2021, 05:03 PM
Post: #10
 Albert Chan Senior Member Posts: 2,231 Joined: Jul 2018
RE: CAS diff() giving incorrect results
(09-23-2021 06:20 PM)Wes Loewer Wrote:  When I try (x^3)''(x = 4) it gives a warning and returns (6*x) = 24
(09-25-2021 04:04 PM)parisse Wrote:  f(x=...) will work if f is an expression. It will not work with e.g. (x^3)'' because the parser will not detect that it's an expression (and it will do like if it was implicit multiplication because x appears in both).

Why does older version detected the expression, but not newer version ?
Does this mean newer versions of HP Prime CAS support implied multiplication ?

It still does not explain Wes Loewer weird result.
Assuming parser get confused, assumed implicit multiplication, we should have:

CAS> (6*x) * (x = 4) ﻿ ﻿ ﻿ ﻿ ﻿ ﻿ → 6*x^2 = 24*x
09-26-2021, 12:04 PM (This post was last modified: 09-26-2021 04:42 PM by Albert Chan.)
Post: #11
 Albert Chan Senior Member Posts: 2,231 Joined: Jul 2018
RE: CAS diff() giving incorrect results
I thought that expression|(x=a) notation can be simulated with eval(subst(quote(expression), x=a))
But, not quite. subst version is better ! (I skipped eval, because f(x) not defined)

CAS> 'integrate(f(x),x)' (x = t*t

integrate(f(t*t)*(t+t),t)

CAS> 'integrate(f(x),x, 1, 2)' (x = t*t

integrate(f(t*t)*(t+t),t, 1, √2)

CAS> integrate(f(x),x) |x = t*t

x*f(t^2)

CAS> integrate(f(x),x, 1, 2) |x = t*t

integrate(f(x),x, 1, 2)

---

I may have found a quoting bug. If quoted expression is saved to a variable, quote is gone.

CAS> assume( t >= 0)
CAS> m := 'integrate(1/(√x*(x+1)),x)' (x = t*t)

integrate(1/(√(t*t)*(t*t+1))*(t+t),t)

CAS> m // should have matched Ans

2*atan(t)

Update: another bug, if we use notation x = a .. b

CAS> 'integrate(f(x),x = 1 .. 2)' (x = t*t

integrate(f(t*t),(t*t) = (1 .. 2))
 « Next Oldest | Next Newest »

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