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;








0















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.










share|improve this question






















  • stackoverflow.com/questions/36013063/…

    – DiscreteMath
    Mar 25 at 1:17

















0















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.










share|improve this question






















  • stackoverflow.com/questions/36013063/…

    – DiscreteMath
    Mar 25 at 1:17













0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 0:23









DiscreteMathDiscreteMath

84




84












  • 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





stackoverflow.com/questions/36013063/…

– DiscreteMath
Mar 25 at 1:17












1 Answer
1






active

oldest

votes


















0














vector field



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()





share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    vector field



    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()





    share|improve this answer



























      0














      vector field



      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()





      share|improve this answer

























        0












        0








        0







        vector field



        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()





        share|improve this answer













        vector field



        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()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 1:32









        Bill MooreBill Moore

        2,0781323




        2,0781323





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

            밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

            1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴