Message Boards Message Boards

1
|
6150 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

StreamPlot the following gradient?

Posted 5 years ago

Hello, it could be, that plenty of people asked this question already. I didn't search the right way. Sorry!

My Problem is with very basic usage of Mathematica:

First I define my new Function:

myFunction[x_, y_] := x^2 - x^4

then I define the gradient of this function as a function:

myGradient[x_, y_] := Grad[myFunction[x, y], {x, y}]

When I want to StreamPlot[myGradient[x,y], {x, -2, 2}, {y, -2, 2}] I got an empty diagram. When I use the output of the second last command (myGradient...) the Plotting works fine (StreamPlot[%6, {x, -2, 2}, {y, -2, 2}]).

Why does it not work like this and how do I do it right? In general I want to use the output of some input as the value of a function.

Thanks!

POSTED BY: Julian SSS
3 Replies
Posted 5 years ago

Suppose StreamPlot starts by calculating what to display for x= -2 and y= -2.

So it calls myGradient[-2,-2]

Your definition of myGradient then starts to calculate Grad[myFunction[-2,-2],{-2,-2}]

You can probably see how that is going to be a problem.

You might try removing the : and thus changing your code to

myGradient[x_, y_] = Grad[myFunction[x, y], {x, y}]

That graph will display, but might look a little odd, changing your function to x^2 - y^4 might give a StreamPlot that looks more reasonable.

What the differences are between := and = and exactly why the evaluation does what it does in your example is a deeper issue. Understanding the depths of how evaluation works in Mathematica is key.

You also need to be aware of any cached prior definitions of functions which can sometimes change the behavior that you might be expecting. Those might be responsible for some of the problems you are seeing. You can quit the kernel or clear individual definitions or restart Mathematica to eliminate those.

POSTED BY: Bill Nelson

The documentation for StreamPlot shows that StreamPlot is expecting a vector as its argument. The "Details and options" lists show three statements that apply.

  • StreamPlot does not show streamlines at any positions for which the Subscript[v, i] etc. do not evaluate to real numbers.
  • StreamPlot has attribute HoldAll, and evaluates the Subscript[v, i] etc. only after assigning specific numerical values to x and y.
  • In some cases it may be more efficient to use Evaluate to evaluate the Subscript[v, i] etc. symbolically before specific numerical values are assigned to x and y.

With these in mind the command to ensure a plot follows.

StreamPlot[Evaluate[myGradient[x, y]], {x, -2, 2}, {y, -2, 2}]

We can see that the argument was being held unevaluated by running a Trace.

Trace[StreamPlot[myGradient[x, y], {x, -2, 2}, {y, -2, 2}]]

I hope this was also helpful.

POSTED BY: Isaac Abraham
Anonymous User
Anonymous User
Posted 5 years ago

try this: EquationTrekker

EquationTrekker is one way to plot differential equations in Mathematica and not the easiest to find in Help (it's mentioned somewhere).

EquationTrekker has an interface for plotting (gradients) in the way it plots them.

https://reference.wolfram.com/language/EquationTrekker/tutorial/EquationTrekker.html

POSTED BY: Anonymous User
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract