Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

numpy - Problem in writing an itegration in python

I have 2 columns of arrays. I also have an equation(integration) that uses 2 columns. I need to have the integral values, using two columns values. In the sense that, for each s there is c. add the third column which would be the integration result based on a specific index number as an upper and under the limit. As an example, take a look at the values below:

ID=50
s = np.arange(0,100)
c = np.arange(200,300)
lanr=-4.56
lani=-2.33

and the integration that I need to be solved is c(s) * exp(lanr * s) * sin (lani * s). Now my problem is adding the third column with the result of the integration between 0,s[ID], which means that and I need to have the integral with detail that I mentioned in the question between s=0 to s=ID. I have written something below which does not work:

from scipy.integrate import quad
import numpy as np
from sympy import *
def f(s):
    return c*(s) * exp(lanr * s) * sin (lani * s)
integ = []
for i in enumerate(s):
    g = c * np.exp(lanr * s) * np.sin(lani * s)

    integrate( f(s), s,0,ID)

question from:https://stackoverflow.com/questions/65890615/problem-in-writing-an-itegration-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe the following is similar to what you're looking for?

At the start, we can try to only work symbolically. s is the basic variable. c is a function of s, in this case writing c = s + 200 makes c such a function.

f=c*exp(lanr*s)*sin(lani*s) is a more complicated function of s. print(f) gives -(s+200)*exp(-4.56*s)*sin(2.33*s).

Now, you seem to be interested in the integral of f for s going from 0 till some value. Let's call that value t. Then, that integral would be a function g of t.

from sympy import exp, sin, symbols, integrate, lambdify

s, t = symbols('s t')
lanr = -4.56
lani = -2.33
c = s + 200
f = c * exp(lanr * s) * sin (lani * s)
g = integrate(f, (s, 0, t))

If only 101 values are needed, we can stay inside sympy:

values = [g.subs(t, ti).evalf() for ti in range(0, 101)]

If more numeric calculations are needed, lambdify() can convert g from sympy to numpy. Then numpy can also calculate the first 101 values (this works much faster than in sympy, but that is only important if many more calculations are needed):

g_np = lambdify(t, g)

import  numpy as np

x = np.arange(0,100)
y = g_np(x)

In this case the result would be

array([  0.        , -17.66531171, -17.80584637, -17.80185932,
       -17.8019015 , -17.80190133, -17.80190133, -17.80190133,
       -17.80190133, -17.80190133, -17.80190133, -17.80190133,
       -17.80190133, -17.80190133, -17.80190133, -17.80190133,
       ...

This looks quite strange. Maybe there is some misunderstanding somewhere? Or maybe the original formula has some mistake?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...