Mathematica: Trouble with defining function from gradiant

In summary, the person is seeking help with two Mathematica issues: defining the gradient as a new function and evaluating a function in an attachment. They received a suggestion to use the built-in function Grad and a solution was provided on a forum to use transformation rules. The person also asks for general suggestions on how to use Mathematica more efficiently.
  • #1
Gaco
17
0
Hi everyone. I've been using basically a whole day on two trivial Mathematica issues, so I'm finally going online for help:


1. Is there any simple way to define the gradient as a new function as such? My try:

Clear["Global`*"]
f[u_] := x*y
gradf[u_] := D[f, {{x, y}}]
gradf
x[u_] := Exp
y := Cos
gradf

Which gives: "General::ivar: E^u is not a valid variable. >>"

I can kind of see what the problem is, but I don't know how to fix it. It works fine if I define gradf as an expression instead of a function (without the [u_]:), but I'd like it as a function. Any simple solution?


2. See the attachment. On the final line, it doesn't evaluate the function r2[0] properly. I think the problem is how h is defined as h[u_]:= f[x,y], doesn't work apparently. Any suggestions?



Also I'd love any general suggestions on how to do things easier or simpler in the notebook attachement. I'm a total beginner in Mathematica (just came from Maple) so I probably do a some things not the easiest or simplest way. Any feedback would be good


Any help is appreciated.
Thanks
 

Attachments

  • Mat1P13a.nb
    23.8 KB · Views: 448
Physics news on Phys.org
  • #2
You probably should just use the built in function Grad
 
  • #3
Thank you for the suggestion.

The solution was given on the official wolfram forum and was to use the transformation rule, the /. operator and make the third line as this:

D[f, {{x, y}}]/.{x->Exp,y->Cos}

That way the fints the gradiant from the general expression, then substitutes in the function expressions for x and y and finally that result is set equal to gradf[u_] (because SetDelayed := is used).
 

1. Why am I having trouble defining a function from a gradient in Mathematica?

Defining a function from a gradient in Mathematica can be tricky because the syntax and notation can be intimidating for new users. It is important to understand the difference between the gradient operator (Grad) and the function for defining a gradient (D). Also, make sure to properly declare any variables or parameters in your function.

2. How do I define a gradient function in Mathematica?

To define a gradient function in Mathematica, use the D function and specify the variables or parameters for which you want to take the gradient. For example, if you want to take the gradient of a function f with respect to variables x and y, you would use the notation D[f[x,y], {x,y}].

3. What is the difference between the gradient operator (Grad) and the function for defining a gradient (D)?

The gradient operator (Grad) is used to evaluate the gradient of a function at a specific point, while the function for defining a gradient (D) is used to define a gradient function for later use. The D function also allows for taking the gradient with respect to multiple variables or parameters.

4. Can I use the gradient function in Mathematica for vector-valued functions?

Yes, the gradient function (D) in Mathematica can be used for vector-valued functions. Make sure to properly declare the variables or parameters in your function and use the appropriate notation for vector operations.

5. What are some common errors when defining a function from a gradient in Mathematica?

Some common errors when defining a function from a gradient in Mathematica include not properly declaring variables or parameters, using incorrect notation for vector operations, and not understanding the difference between the gradient operator (Grad) and the function for defining a gradient (D). It is important to carefully check your syntax and refer to the Mathematica documentation for proper usage.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
832
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
901
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Differential Equations
Replies
3
Views
2K
  • Calculus and Beyond Homework Help
Replies
5
Views
232
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top