Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在每次步骤中将迭代解决方案的输出调用到数组中(使用Python)_Python_Arrays_Numpy_Sequence - Fatal编程技术网

如何在每次步骤中将迭代解决方案的输出调用到数组中(使用Python)

如何在每次步骤中将迭代解决方案的输出调用到数组中(使用Python),python,arrays,numpy,sequence,Python,Arrays,Numpy,Sequence,下面是我通过(1)使用牛顿法迭代求解方程组来解决问题的代码。该迭代的结果是,在第二步,即步骤(2)中使用三元组(z0,z1,z2)来计算x=(x0,x1,x2)的值。我将对t=(0,1,5)重复这个过程。也就是说,在下一步中,牛顿再次求解z,而这个新的z值被用来再次获得x。这意味着在一天结束时,我需要一个包含x值的3*6数组 问题是:(1)我似乎发现很难调用为z获得的值来用于解x的函数 (2) 在函数StepFunction中,我似乎将序列“Newton”调用到数组中。我认为这是一个问题。我实际

下面是我通过(1)使用牛顿法迭代求解方程组来解决问题的代码。该迭代的结果是,在第二步,即步骤(2)中使用三元组(z0,z1,z2)来计算x=(x0,x1,x2)的值。我将对t=(0,1,5)重复这个过程。也就是说,在下一步中,牛顿再次求解z,而这个新的z值被用来再次获得x。这意味着在一天结束时,我需要一个包含x值的3*6数组

问题是:(1)我似乎发现很难调用为z获得的值来用于解x的函数 (2) 在函数StepFunction中,我似乎将序列“Newton”调用到数组中。我认为这是一个问题。我实际上想称之为牛顿得到的解 (3) 有时我会得到这个值错误:无法将输入数组从形状(3,5)广播到形状(3)

你的帮助将不胜感激。谢谢

import numpy as np
def Newton_system(F, J, z, zz, x, eps):
    """
    Solve nonlinear system F=0 by Newton's method.
    J is the Jacobian of F. Both F and J must be functions of x.
    At input, z holds the start value. The iteration continues
    until ||F|| < eps.
    """
    #call in the value x from previous iteration
    F_value = F(z,zz,x)
    F_norm = np.linalg.norm(F_value, ord=2)  # l2 norm of vector
    iteration_counter = 0
    while abs(F_norm) > eps and iteration_counter < 100:
        delta = np.linalg.solve(J(z), -F_value)
        z = z + delta
        F_value = F(z,zz,x)
        F_norm = np.linalg.norm(F_value, ord=2)
        iteration_counter += 1

    # Here, either a solution is found, or too many iterations
    if abs(F_norm) > eps:
        iteration_counter = -1
    return z, iteration_counter
z.shape

您正在尝试将二维数组放入一维数组。

尝试使用array重新调整数组的形状,将值放入3x5数组。重新调整(3,5)。

尝试将二维数组放入一维数组。
尝试使用array.reforme(3,5)重新调整数组的形状,将值放入3x5数组中

def F(z,zz,x):
    k,a,b,c,h = 0.1,0.2,0.2,5.7,6.0` code `
    return np.array([ z[0]-x[0]+h/2*(z[1]+z[2]+k*z[0]-k*zz[0]),
                      z[1]-x[1]-h/2*(z[0]+a*z[1]-k*z[1]+k*zz[1]),
                      z[2]-x[2]-h/2*(b+z[2]*(z[0]-c)-k*z[2]+k*zz[2])])
  


def J(z):
    k,a,b,c,h = 0.1,0.2,0.2,5.7,6.0
    return np.array([[1+h*k/2,h/2,h/2],
                     [-h/2,1-(h*a)/2,h*k/2],
                     [-h*z[2]/2,0,1-h/2*(z[0]-c)-k]])



def DelayedRossler(z,zz):     
    a,b,c,k,h = 0.2,0.2,5.7,0.1,6.0
    return np.array([ -z[1] -z[2] -k*z[2] +k*z[0]-k*zz[0] , 
                     z[0] +a*z[1]-k*z[2] +k*z[1]-k*zz[1],
                     b+z[2]*(z[0]-c)-k*z[2] +k*z[0]-k*zz[0] ])

def StepFunction(f, zinit, xinit, tinit,tfinal, dt):   
    global z
    t = np.arange(tinit,tfinal, dt)
    nt = t.size
    nx = xinit.size  #number of state variables
    nz = zinit.size  #number of state variables
    
    x = np.zeros((nx,nt)) #initialization with nx rows and nt columns
    z = np.zeros((nz,nt))  #initialization with nz rows and nt columns
    zz = np.zeros((nz,nt))  #initialization with nzz rows and nt columns

    x[:,0] = xinit    
    z[:,0] = zinit
    zz[:,0] = zinit    
    
    for n in range (nt-1): 
        z[:,n] = z
        zz[:,n+1] = z[:,n]
        x[:,n] =x
        z[:,n+1] = Newton_system(F, J, z , zz, x , eps =0.0001)
        s = dt*f(t[n] +dt/2, z[:,n+1],zz[:,n+1]) 
        x[:,n+1] =  x[:,n] + s
    return x, t 
  

#Define Problem
f= lambda t, z: DelayedRossler(z,zz)

#solve the problem
zinit = np.array([0.0,0.0,0.0])
xinit = np.array([0.0,0.0,0.0])
tinit = 0.0
tfinal = 30.0
dt = 6

x, t = StepFunction(f, zinit, xinit, tinit,tfinal, dt)
print(x)