HP Forums

Full Version: CAS grad() function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How does this work without the variable list? The example gives a "Bad Argument Value" when I delete [x y z].

Shouldn't it produce a unit vector that goes in a particular direction? How do I supply values?

And what the heck is the little +/- that appears when you hit []?
(05-24-2017 08:20 PM)KeithB Wrote: [ -> ]How does this work without the variable list? The example gives a "Bad Argument Value" when I delete [x y z].

Shouldn't it produce a unit vector that goes in a particular direction? How do I supply values?

And what the heck is the little +/- that appears when you hit []?

grad() is a CAS command, so make sure that you are in CAS view. As an example:

grad(x^2*y,[x,y]); //--> [ 2*x*y, x^2]

It does not return a unit vector; it returns the gradient, which is defined as

\[ [ f_{x_1}, f_{x_2} , \dotsm, f_{x_n} ] \]

where \( f \) is a function of the variables \( x_1 \) through \( x_n \), and \( f_{x_i} \) denotes the partial derivative of \( f \) with respect to \( x_i \).

The +/- just means you can add row/columns by editing that position. If you place the cursor on the +/- and press + or - you can add/remove that particular row or column.
"Returns the gradient of an expression. With a list of variables as second argument, returns the vector of partial derivatives."

That returns the partials. When you delete the list of variables you just get an error "Bad argument values", and no gradient which should be something where all the variables are a dimension. I think the option is so you can have variable constants, as it were.
Arguments that are optional are shown with []'s surrounding the argument. As an example, look at the arguments for the input() command. The help for grad() does not show any arguments surrounded by []'s. So I took that to mean that the list of variables is required. By specifying all variables (in the list), then one obtains the gradient (which is basically a list of all partial derivatives). On the other hand, specifying only a (proper) subset of all the variables then one obtains a list of partial derivatives. This was how I interpreted the help text.
You can write a short CAS program that will automatically generate the list of variables:

Code:
#cas
gradient(f):=
BEGIN
  return(grad(f,lvar(f)));
END;
#end

Usage: gradient(expression)

This snippet of code, however, is not fool proof. It assumes that all variables in the expression are in fact undefined variables.
Ah, thanks. I was fooled because the example has the []. and the description says "with" making it sound optional.
Reference URL's