Calculate curl of a vector field in Python and plot it with matplotlib

后端 未结 3 658
长发绾君心
长发绾君心 2020-12-29 09:50

I need to calculate the curl of a vector field and plot it with matplotlib. A simple example of what I am looking for could be put like that:

How can I calculate and

3条回答
  •  借酒劲吻你
    2020-12-29 10:07

    To calculate the curl of a vector function you can also use numdifftools for automatic numerical differentiation without a detour through symbolic differentiation. Numdifftools doesn't provide a curl() function, but it does compute the Jacobian matrix of a vector valued function of one or more variables, and this provides the derivatives of all components of a vector field with respect to all of the variables; this is all that's necessary for the calculation of the curl.

    import import scipy as sp
    import numdifftools as nd
    
    def h(x):
        return sp.array([3*x[0]**2,4*x[1]*x[2]**3, 2*x[0]])
    
    def curl(f,x):
        jac = nd.Jacobian(f)(x)
        return sp.array([jac[2,1]-jac[1,2],jac[0,2]-jac[2,0],jac[1,0]-jac[0,1]])
    
    x = sp.array([1,2,3)]
    curl(h,x)
    

    This returns the value of the curl at x: array([-216., -2., 0.]) Plotting is as suggested above.

提交回复
热议问题