# HP Forums

Full Version: [HP-Prime CAS] Vector Calculus Library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Hello

Start to program a library of vector calculus, in different coordinates (spherical, cylindrical and Cartesian) for the current commands grad fromHP-CAS only work on Cartesian Coordinates =(.

First command or function (test) gradient of a function in (x, y, z)

f2(x,y,z):=x^3+y²+z; [ENTER]

[d(f2(x,y,z),x),d(f2(x,y,z),y),d(f2(x,y,z),z)] ; [ENTER] => [3*x²,2*y,1] // ok

// definition as function

gradient(x^3+y²+z); [ENTER] [0,0,0] ?? What mistake I'm making?

Edited: 30 Oct 2013, 11:32 p.m. after one or more responses were posted

Does this do anything different then the built in capability?

TW

The first version (dradient, divergence) is exactly like the command to bring the hp-prime, but only works in Cartesian coordinates, my purpose is to operate in spherical and cylindrical coordinates =)

I'm waiting for an answer where I am wrong encoding my code for gradent ...

Quote:
The first version (dradient, divergence) is exactly like the command to bring the hp-prime, but only works in Cartesian coordinates, my purpose is to operate in spherical and cylindrical coordinates =)

I'm waiting for an answer where I am wrong encoding my code for gradent ...

```gradient(f):=[ diff(f,x), diff(f,y), diff(f,z) ]
```

The input variable of any function (in this case, of gradient) must be exactly that: a variable. You may want to make your gradient function more robust by allowing users to specify the variables of the function, though (rather than assuming it will always be x, y, and z).

```gradient(f,v):=[diff(f,v(1)), diff(f,v(2)), diff(f,v(3)) ];
```

Quote:

```gradient(f):=[ diff(f,x), diff(f,y), diff(f,z) ]
```

The input variable of any function (in this case, of gradient) must be exactly that: a variable. You may want to make your gradient function more robust by allowing users to specify the variables of the function, though (rather than assuming it will always be x, y, and z).

```gradient(f,v):=[diff(f,v(1)), diff(f,v(2)), diff(f,v(3)) ];
```

You probably have to enter this in algebraic mode since there seems to be a bug in textbook mode regarding vectors/matrices and entries consisting of functions.

Quote:
You probably have to enter this in algebraic mode since there seems to be a bug in textbook mode regarding vectors/matrices and entries consisting of functions.

thanks Han

On the entry line and within array [] the [,] key used to skip to the next element, and I can not writing the expression f(x,y) ...

[request for hp]

I think one solution is to [shift-lock]+[,] = ,

Edited: 30 Oct 2013, 11:20 p.m.

Han if I define my original function with diff, not fail, if define symbol d fail, is a possible BUG?

// step to step

f2(x,y,z):=x^3+y²+z; [ENTER]

[d(f2(x,y,z),x),d(f2(x,y,z),y),d(f2(x,y,z),z)] ; [ENTER] => [3*x²,2*y,1] // ok

[diff(f2(x,y,z),x) , diff(f2(x,y,z),y), diff(f2(x,y,z),z)]; [ENTER] => [3*x²,2*y,1] // ok

// define

gradient(f(x,y,z)):= [diff(f(x,y,z),x), diff(f(x,y,z),y), diff(f(x,y,z),z)]; [ENTER] Done

gradient(x^3+y²+z); [ENTER] => [3*x²,2*y,1] // ok

gradient(x^3+y²+z); [ENTER] => [0,0,0] ?? // BUG?

PD: on the entry line" another problem with v([1]) extraction of elements of a list, matrix ([m,n]), I can only be from an external editor (copy/paste) or encode in "algebraic" entry =(

sample

Edited: 31 Oct 2013, 12:05 a.m.

Quote:
Han if I define my original function with diff, not fail, if define symbol d fail, is a possible BUG?

Possibly -- it really depends on whether d is meant as a numerical command or a CAS command. The documentation says it is supposed to compute numerical derivatives. However, it appears the developers have allowed it to also compute symbolic derivatives.

Quote:
// step to step

f2(x,y,z):=x^3+y²+z; [ENTER]

[d(f2(x,y,z),x),d(f2(x,y,z),y),d(f2(x,y,z),z)] ; [ENTER] => [3*x²,2*y,1] // ok

[diff(f2(x,y,z),x) , diff(f2(x,y,z),y), diff(f2(x,y,z),z)]; [ENTER] => [3*x²,2*y,1] // ok

// define

gradient(f(x,y,z)):= [diff(f(x,y,z),x), diff(f(x,y,z),y), diff(f(x,y,z),z)]; [ENTER] Done

gradient(x^3+y²+z); [ENTER] => [3*x²,2*y,1] // ok

You should avoid using non-standard variables. When you create the CAS function gradient, your code above uses "f(x,y,z)" to mean a function of three variables as the desired input. However, the CAS interprets this as a single variable whose name is "f(x,y,z)" and does not see it as a function. To see my point, type:

```myfunc(x*Y+Z):=SIN(x*Y+Z);
myfunc(2); ==> SIN(2)```

Everything works as if "x*Y+Z" is a valid variable -- but you should avoid this. (It may be an un-intended "feature" but it is not a good use of placeholder variables)

Quote: