1

我刚刚定义了一个函数,它是给定阶跃函数的积分,现在我想对获得的三角形函数进行积分。在此处输入图像描述 jerk:1,-1,1-1 步进函数对应于给定的连续 jerk 列表。为了获得三角形函数,

我使用了这个公式:a(t)=kt+ack 是 Jerk,ac 是一个常数,它确保了每一步的连续性和变化。但是要知道,如果我使用公式v(t)=(1/2)kt^2+ac*t+vc,我会得到不正确的速度负值。

我找到了一些 scipy 函数,它返回一个整数的数字,但我需要一条曲线,你知道如何整合它吗?谢谢你

4

2 回答 2

1

你可以使用scipy.integrate.quad函数...

from scipy.integrate import quad

def g(x): # your triangle function
    return abs((x+1)%2-1)

for x in (0.1*i for i in xrange(40)):
    I = quad(g, 0, x)[0]
    print "x = %f -> g(x) = %f -> I(g, 0, x) = %f" % (x, g(x), I)

是你要找的东西吗?

@编辑:

当 g 函数是离散的时,您可以对非离散区间的积分求和:

from math import floor
from scipy.integrate import quad

I = 0.0
for x in (1.0*i for i in xrange(4)):
    print "x = %f -> g(x) = %f -> I(g, 0, x) = %f" % (x, g(x), I)
    if floor(x) % 2 == 0:
        g = lambda x: 1
    else:
        g = lambda x: -1
    I += quad(g, x, x+1.0)[0]
于 2012-10-15T08:55:48.670 回答
0

这可能是您正在寻找的:

在此处输入图像描述

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate

# step Function
def jerkstep(xprime,k):
    return (1 - 2*( int(xprime)%2 == 1 ) )*k

# Triangle function ( step integration)
def jerkint(x,k,ac): 
    return  integrate.quad( jerkstep ,0, x, args =(k) )[0] + ac*x

# Integrate of triangle
def jerkintint(x,k,ac,vc):
    return integrate.quad( jerkint, 0 , x , args = (1,0) )[0] + vc*x

# linearly spaced time samples (~ x axis)
t = np.linspace(2.0, 10.0, num = 100)

#optional arguments
k = 1
ac = 0
vc = 0

#output functions
jerk = np.array(t);
triangle = np.array(t);
curve = np.array(t);

# Warning : it can be long ( integration not optimized )
for indice,time in zip(range(len(t)),t): # Double indexation on t indices and t values
    print "Time, in seconds : ", time

    jerk[indice] = jerkstep(time,k)
    print "Step function : " , jerk[indice]

    triangle[indice] =jerkint(time,k,ac)
    print "Step function integrate ( = triangle ) : " , triangle[indice]

    curve[indice] = jerkintint(time,k,ac,vc)
    print "Step function double integrate ( = triangle integrate ) : " , curve[indice]
    print "\n"


# plot figure in mathplotlib
fig, axs = plt.subplots(3, 1, sharex=True, sharey=False)
axs[0].plot(t, jerk, 'o-', label=str("Step Function"))
axs[1].plot(t, triangle, 'o-' , label=str("Triangle Function"))
axs[2].plot(t, curve, 'o-' , label=str("Curve Function"))
fig.show()
plt.show()
于 2012-10-15T09:10:57.843 回答