Mathematica: Trouble with defining function from gradiant

Click For Summary
SUMMARY

The discussion centers on defining a gradient function in Mathematica, specifically addressing issues with variable definitions and function evaluations. The user attempted to define a gradient function using the derivative operator D but encountered an error due to improper variable handling. The solution involves using the transformation rule with the /. operator to substitute variable definitions correctly. The final implementation is gradf[u_] := D[f[u], {{x[u], y[u]}}] /. {x -> Exp, y -> Cos}.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of derivatives and gradient concepts
  • Knowledge of variable substitution in symbolic computation
  • Basic experience with function definitions in Mathematica
NEXT STEPS
  • Explore the built-in Grad function in Mathematica for gradient calculations
  • Learn about the /. operator for variable substitution in Mathematica
  • Study function definitions and evaluation in Mathematica
  • Investigate best practices for simplifying expressions in Mathematica notebooks
USEFUL FOR

Mathematica users, particularly beginners transitioning from other software like Maple, and anyone looking to improve their understanding of symbolic computation and function definitions in Mathematica.

Gaco
Messages
17
Reaction score
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

Physics news on Phys.org
You probably should just use the built in function Grad
 
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).
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K