use sympy to find gradient and plot vector fieldWhat is the purpose of meshgrid in Python / NumPy?Finding the index of an item given a list containing it in PythonHow to put the legend out of the plotFind current directory and file's directorySave plot to image file instead of displaying it using MatplotlibHow to make IPython notebook matplotlib plot inlineSpeedup sympy-lamdified and vectorized functionPlotting second figure with matplotlib while first is still openHow to plot 2D gradient(rainbow) by using matplotlib?Python: Getting a Vector Field from Gradient of Scalar FieldLambdifying a Sympy vector
Why would a home insurer offer a discount based on credit score?
What is Gilligan's full name?
Is it a good security practice to force employees hide their employer to avoid being targeted?
Nth term of Van Eck Sequence
Is plausible to have subspecies with & without separate sexes?
What's the relation between у.е. to USD?
I am caught when I was about to steal some candies
Must I use my personal social media account for work?
A life of PhD: is it feasible?
If absolute velocity does not exist, how can we say a rocket accelerates in empty space?
In Pandemic, why take the extra step of eradicating a disease after you've cured it?
Is all-caps blackletter no longer taboo?
David slept with Bathsheba because she was pure?? What does that mean?
What is the theme of analysis?
Keeping track of theme when improvising
Was the Lonely Mountain, where Smaug lived, a volcano?
Is it advisable to add a location heads-up when a scene changes in a novel?
Realistic, logical way for men with medieval-era weaponry to compete with much larger and physically stronger foes
Must a CPU have a GPU if the motherboard provides a display port (when there isn't any separate video card)?
The best in flight meal option for those suffering from reflux
How do I type a hyphen in iOS 12?
Are skill challenges an official option or homebrewed?
Why does there seem to be an extreme lack of public trashcans in Taiwan?
What do you call the action of "describing events as they happen" like sports anchors do?
use sympy to find gradient and plot vector field
What is the purpose of meshgrid in Python / NumPy?Finding the index of an item given a list containing it in PythonHow to put the legend out of the plotFind current directory and file's directorySave plot to image file instead of displaying it using MatplotlibHow to make IPython notebook matplotlib plot inlineSpeedup sympy-lamdified and vectorized functionPlotting second figure with matplotlib while first is still openHow to plot 2D gradient(rainbow) by using matplotlib?Python: Getting a Vector Field from Gradient of Scalar FieldLambdifying a Sympy vector
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I wrote some code to use sympy to find the gradient of a function f(x,y) = x*y**2, and then to plot the vector field from the gradient. See Below:
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
X,Y = np.meshgrid(np.linspace(-3,3,15), np.linspace(-3,3,15))
U=[g[0].subs(x:x1, y:y1) for (x1,y1) in zip(X,Y)]
V=[g[1].subs(x:x1, y:y1) for (x1,y1) in zip(X,Y)]
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
What i'm wondering about is why the sympy "subs" function is not working in this code. Its just returns the expression without inserting a value of X and Y to evaluate to a numerical value, instead its just returning the sympy object without any subsitution.
python matplotlib sympy
add a comment |
I wrote some code to use sympy to find the gradient of a function f(x,y) = x*y**2, and then to plot the vector field from the gradient. See Below:
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
X,Y = np.meshgrid(np.linspace(-3,3,15), np.linspace(-3,3,15))
U=[g[0].subs(x:x1, y:y1) for (x1,y1) in zip(X,Y)]
V=[g[1].subs(x:x1, y:y1) for (x1,y1) in zip(X,Y)]
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
What i'm wondering about is why the sympy "subs" function is not working in this code. Its just returns the expression without inserting a value of X and Y to evaluate to a numerical value, instead its just returning the sympy object without any subsitution.
python matplotlib sympy
stackoverflow.com/questions/36013063/…
– DiscreteMath
Mar 25 at 1:17
add a comment |
I wrote some code to use sympy to find the gradient of a function f(x,y) = x*y**2, and then to plot the vector field from the gradient. See Below:
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
X,Y = np.meshgrid(np.linspace(-3,3,15), np.linspace(-3,3,15))
U=[g[0].subs(x:x1, y:y1) for (x1,y1) in zip(X,Y)]
V=[g[1].subs(x:x1, y:y1) for (x1,y1) in zip(X,Y)]
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
What i'm wondering about is why the sympy "subs" function is not working in this code. Its just returns the expression without inserting a value of X and Y to evaluate to a numerical value, instead its just returning the sympy object without any subsitution.
python matplotlib sympy
I wrote some code to use sympy to find the gradient of a function f(x,y) = x*y**2, and then to plot the vector field from the gradient. See Below:
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
X,Y = np.meshgrid(np.linspace(-3,3,15), np.linspace(-3,3,15))
U=[g[0].subs(x:x1, y:y1) for (x1,y1) in zip(X,Y)]
V=[g[1].subs(x:x1, y:y1) for (x1,y1) in zip(X,Y)]
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
What i'm wondering about is why the sympy "subs" function is not working in this code. Its just returns the expression without inserting a value of X and Y to evaluate to a numerical value, instead its just returning the sympy object without any subsitution.
python matplotlib sympy
python matplotlib sympy
asked Mar 25 at 0:23
DiscreteMathDiscreteMath
84
84
stackoverflow.com/questions/36013063/…
– DiscreteMath
Mar 25 at 1:17
add a comment |
stackoverflow.com/questions/36013063/…
– DiscreteMath
Mar 25 at 1:17
stackoverflow.com/questions/36013063/…
– DiscreteMath
Mar 25 at 1:17
stackoverflow.com/questions/36013063/…
– DiscreteMath
Mar 25 at 1:17
add a comment |
1 Answer
1
active
oldest
votes
The problem with your code is that you need to access a meshgrid as a 2-dimensional array.
Example: U[i,j] not U[i]
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
xrange = np.linspace(-3,3,15)
yrange = np.linspace(-3,3,15)
X,Y = np.meshgrid(xrange, yrange)
U=X
V=Y
for i in range(len(xrange)):
for j in range(len(yrange)):
x1 = X[i,j]
y1 = Y[i,j]
U[i,j] = g[0].subs(x:x1, y:y1)
V[i,j] = g[1].subs(x:x1, y:y1)
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329890%2fuse-sympy-to-find-gradient-and-plot-vector-field%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem with your code is that you need to access a meshgrid as a 2-dimensional array.
Example: U[i,j] not U[i]
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
xrange = np.linspace(-3,3,15)
yrange = np.linspace(-3,3,15)
X,Y = np.meshgrid(xrange, yrange)
U=X
V=Y
for i in range(len(xrange)):
for j in range(len(yrange)):
x1 = X[i,j]
y1 = Y[i,j]
U[i,j] = g[0].subs(x:x1, y:y1)
V[i,j] = g[1].subs(x:x1, y:y1)
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
add a comment |
The problem with your code is that you need to access a meshgrid as a 2-dimensional array.
Example: U[i,j] not U[i]
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
xrange = np.linspace(-3,3,15)
yrange = np.linspace(-3,3,15)
X,Y = np.meshgrid(xrange, yrange)
U=X
V=Y
for i in range(len(xrange)):
for j in range(len(yrange)):
x1 = X[i,j]
y1 = Y[i,j]
U[i,j] = g[0].subs(x:x1, y:y1)
V[i,j] = g[1].subs(x:x1, y:y1)
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
add a comment |
The problem with your code is that you need to access a meshgrid as a 2-dimensional array.
Example: U[i,j] not U[i]
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
xrange = np.linspace(-3,3,15)
yrange = np.linspace(-3,3,15)
X,Y = np.meshgrid(xrange, yrange)
U=X
V=Y
for i in range(len(xrange)):
for j in range(len(yrange)):
x1 = X[i,j]
y1 = Y[i,j]
U[i,j] = g[0].subs(x:x1, y:y1)
V[i,j] = g[1].subs(x:x1, y:y1)
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
The problem with your code is that you need to access a meshgrid as a 2-dimensional array.
Example: U[i,j] not U[i]
%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sp
import numpy as np
sp.init_printing()
x,y = sp.symbols('x y')
def gradient(f):
return (f.diff(x), f.diff(y))
f = x*y**2
g = gradient(f)
g
xrange = np.linspace(-3,3,15)
yrange = np.linspace(-3,3,15)
X,Y = np.meshgrid(xrange, yrange)
U=X
V=Y
for i in range(len(xrange)):
for j in range(len(yrange)):
x1 = X[i,j]
y1 = Y[i,j]
U[i,j] = g[0].subs(x:x1, y:y1)
V[i,j] = g[1].subs(x:x1, y:y1)
plt.quiver(X,Y,U,V, linewidth=1)
plt.title("vector field")
plt.show()
answered Mar 25 at 1:32
Bill MooreBill Moore
2,0781323
2,0781323
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329890%2fuse-sympy-to-find-gradient-and-plot-vector-field%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
stackoverflow.com/questions/36013063/…
– DiscreteMath
Mar 25 at 1:17