科学优化用数组求解非线性方程?

时间:2018-08-14 10:14:11

标签: python scipy nonlinear-optimization

我必须解决以下方程式:

f1 = -k1 + self.f(
    self.time[i] + (0.5 + np.sqrt(3)/6) * self.dt,
    self.u[i] + 0.25 * self.dt * k1 + (0.25 + np.sqrt(3)/6) * self.dt * k2) 
f2 = -k2 + self.f(
    self.time[i] + (0.5 - np.sqrt(3)/6) * self.dt,
    self.u[i] + (0.25 - np.sqrt(3)/6) * self.dt * k1 + 0.25 * self.dt * k2)

在上一篇文章中进行搜索,我找到了这个解决方案:

def equations(variable):
    k1,k2 = variable
    f1 = -k1 + self.f(
        self.time[i] + (0.5 + np.sqrt(3)/6) * self.dt,
        self.u[i] + 0.25 * self.dt * k1 + (0.25 + np.sqrt(3)/6) * self.dt * k2)
    f2 = -k2 + self.f(
        self.time[i] + (0.5 - np.sqrt(3)/6) * self.dt,
        self.u[i] + (0.25 - np.sqrt(3)/6) * self.dt * k1 + 0.25 * self.dt * k2)
    return np.array([f1,f2]).ravel()     #.reshape(2,)  

k1, k2 = fsolve(equations, (self.u[i],self.u[i]))

但不适用于我,因为我的self.u[i]位于len(self.u[i]) == 2上 在这种情况下我该如何应对?

我收到此错误:

    k1,k2 = variable
ValueError: too many values to unpack (expected 2)

但是我不知道如何解决它,如果我只是通过2 float (2.,2.)我就知道了:

TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'equations'.
Shape should be (2,) but it is (4). 

如果我将.ravel()留在返回数组上,我会遇到相同的错误,但是:

TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'equations'.
Shape should be (2,) but it is (2,2). 

请有人帮我解决这个问题吗?

class ImpRK4 :
    def __init__(self, fun , t0, tf, dt , y0):
        self.func = fun
        self.t0=t0
        self.tf=tf
        self.dt=dt
        self.u0=y0
        self.n = round((tf-t0)/dt)
        self.time  = np.linspace(self.t0, self.tf, self.n+1 )
        self.u     = np.array([self.u0  for i in range(self.n+1) ])

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

    def solve(self): 
       for i in range(len(self.time)-1):
            def equations(variable):
                k1,k2 = variable
                f1 = -k1 + self.f(self.time[i]+ (0.5+np.sqrt(3)/6)* self.dt , self.u[i]+0.25*self.dt* k1+ (0.25+ np.sqrt(3)/6)*self.dt*k2) 
                f2 = -k2 + self.f(self.time[i]+ (0.5-np.sqrt(3)/6)* self.dt , self.u[i]+(0.25-np.sqrt(3)/6)*self.dt *k1 + 0.25*self.dt* k2)
                return np.array([f1,f2]).ravel() #.reshape(2,)  

            k1 , k2 = fsolve(equations,(2,2)) #(self.u[i],self.u[i]))
            self.u[i+1] = self.u[i] + self.dt/2* (k1 + k2)

       plt.plot(self.time,self.u)
       plt.show()    

def main():
    func00 = lambda t,u : -10*(t-1)*u[0]

    func01 = lambda t,u : u[1] 
    func02 = lambda t,u : (1-u[0]**2)*u[1] - u[0]

    func0x = np.array([func00])
    func0 = np.array([func01,func02])

    y01 = np.array([1.,1.])
    t0 = 0. 
    tf = 2.      
    u0 = y01   
    dt = 0.008 

    y0  = np.array([np.exp(-5)])
    diffeq = ImpRK4(func0,t0,tf,dt,y01)    

    #diffeq.solve()
    #diffeq = ImpRK4(func0x,t0,tf,dt,y0) ## with single equations works
    diffeq.solve()

if __name__ == '__main__': 
    main()

在这里单击最小,完整和可验证的示例

0 个答案:

没有答案