Search code examples
pythonsympynumericnumerical-methodsnumerical-integration

How to evaluate this line integral (Python-Sympy)


The idea is to compute the line integral of the following vector field and curve:

enter image description here

enter image description here

This is the code I have tried:

import numpy as np
from sympy import *
from sympy import Curve, line_integrate
from sympy.abc import x, y, t
C = Curve([cos(t) + 1, sin(t) + 1, 1 - cos(t) - sin(t)], (t, 0, 2*np.pi))
line_integrate(y * exp(x) +  x**2 + exp(x) + z**2 * exp(z), C, [x, y, z])

But the ValueError: Function argument should be (x(t), y(t)) but got [cos(t) + 1, sin(t) + 1, -sin(t) - cos(t) + 1] comes up.

How can I compute this line integral then?

I think that maybe this line integral contains integrals that don't have exact solution. It is also fine if you provide a numerical approximation method.

Thanks


Solution

  • The value error you receive does not come from your call to the line_integrate function; it comes because according to the source code for the Curve class, only functions in 2D Euclidean space are supported. This integral can still be computed without using sympy according to this research blog that I found by simply searching for a workable method on Google.

    The code you need looks like this:

    import autograd.numpy as np
    from autograd import jacobian
    from scipy.integrate import quad
    
    def F(X):
        x, y, z = X
        return [y * np.exp(x), x**2 + np.exp(x), z**2 * np.exp(z)]
    
    def C(t):
        return np.array([np.cos(t) + 1, np.sin(t) + 1, 1 - np.cos(t) - np.sin(t)])
    
    dCdt = jacobian(C, 0)
    
    def integrand(t):
        return F(C(t)) @ dCdt(t)
    
    I, e = quad(integrand, 0, 2 * np.pi)
    

    The variable I then stores the numerical solution to your question.