Python从两种方法得到了不同的结果

2024-09-29 23:25:53 发布

您现在位置:Python中文网/ 问答频道 /正文

每天,我都花时间深入研究Python类中编写的两个不同方法。 第一个使用向量,第二个是步进器,每次计算值。。。但在这里,这个类是相同的。你知道吗

class LeapFrog(Multistep):
    '''
        perform the solution of a SODE using 2 step 
    '''
    solved = False
    um1 = 0.0
    stepper_start = True

    def __init__(self, dydt : Rhs, filename: str=None, save: bool= True ):

        self.save = save
        self.file   = filename
        super().__init__(dydt) 

    def solve(self):
        ''' 
            Perform the leap-frog (mid point scheme)
        '''
        self.time , self.u = self.dydt.createArray()


        # start-up the method
        k1 = self.dydt.f(self.time[0],self.u[0])
        k2 = self.dydt.f(self.time[0]+ self.dt , self.u[0]+ self.dt*k1 )

        self.u[1] = self.u[0] + self.dt/2.*(k1+k2)

        for i in range(1,len(self.time)-1):
            self.u[i+1] = self.u[i-1] + 2. * self.dt * self.dydt.f(self.time[i],self.u[i])

        if self.save:
          return self.time,self.u


        LeapFrog.solved = True 

    @classmethod
    def step(cls ,func , t : np.float , u : np.float , dt ):

       def f(ti,ui):
            return  np.array([function(ti,ui) for function in func])     
       # start-up the method 
       if LeapFrog.stepper_start == True:
           LeapFrog.um1 = u.copy()
           u = rungekutta.RK2.Heun.step(func,t,u,dt)
       LeapFrog.stepper_start = False 
       #yield 

       unext = LeapFrog.um1 + 2. * dt * f(t,u)
       LeapFrog.um1 = u 
       return unext 

here the difference

编辑在这两种情况下,我都使用dt=2/100 对不起的!!我忘了说迭代法的调用如下:

 dt = 2/100 
   t,u = t0,u0 
   with open('LeapFrog_p1.dat', 'w') as f:
        while True:
            f.write('%10.4f %14.10f \n' %(t, u[0]) )
            u = multistep.LeapFrog.step(func0,t,u,dt)
            t += dt
            if t >= tf:
               break




   lft = np.genfromtxt('LeapFrog_p1.dat',usecols=(0,)) 
   lfu = np.genfromtxt('LeapFrog_p1.dat',usecols=(1,)) 

编辑@iguanaaut感谢您的提示!t0=0 tf=2.0 u0=np.exp公司(-5个)

Runge Kutta是另一个等级它的步进器是:

        @classmethod
        def step(cls,func , t : np.float , u : np.float , dt ):

            def f(ti,ui):
                return  np.array([function(ti,ui) for function in func])     

            k1 = f(t,u)
            k2 = f(t+dt , u + dt*k1)

            unext = u + dt/2.*(k1+k2)       
            return unext

Your code has several other design problems that I'm concerned about, so it's hard to pin down exactly what the problem would belockquote

你能告诉我是哪个吗??我想改进一下


Tags: theselftruereturntimedefstepnp

热门问题