在Python中打印Bulirsch-Stoer算法的解决方案 [英] Printing the solution for Bulirsch-Stoer algorithm in Python

查看:68
本文介绍了在Python中打印Bulirsch-Stoer算法的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,为什么我不断出现此错误:

In the following code, why do I keep getting this error:

TypeError:"NoneType"对象不可下标

TypeError: 'NoneType' object is not subscriptable

我的 PrintSoln(X,Y,freq)函数?我该如何解决?

for my PrintSoln(X, Y, freq) function? And how can I fix it?

我正在尝试解决IVP: 2 *(y'')+ y'+(y/0.45)= 9 ,初始条件为 y = 0且y'= 0 .'''

I am trying to solve the IVP: 2*(y'') + y' + (y/0.45) = 9, with initial conditions y = 0 and y' = 0.'''

from numpy import zeros, float, sum
import math

def integrate_BulirschStoeir(F, x, y, xStop, tol):
    def midpoint(F, x, y, xStop, nSteps):
        h = (xStop - x)/ nSteps
        y0 = y
        y1 = y0 + h*F(x, y0)
        for i in range(nSteps-1):
            x = x + h
            y2 = y0 + 2.0*h*F(x, y1)

            y0 = y1
            y1 = y2

        return 0.5*(y1 + y0 + h*F(x, y2))

    def richardson(r, k):
        for j in range(k-1,0,-1):
            const = (k/(k - 1.0))**(2.0*(k-j))
            r[j] = (const*r[j+1] - r[j])/(const - 1.0)
        return     
        kMax = 51
        n = len(y)
        r = zeros((kMax, n), dtype=float)

        # Start with two integration steps
        nSteps = 2
        r[1] = midpoint(F, x, y, xStop, nSteps)
        r_old = r[1].copy()

        # Increase the number of integration points by 2 and refine result by Richardson extrapolation
        for k in range(2,kMax):
            nSteps = 2*k
            r[k] = midpoint(F, x, y, xStop, nSteps)
            richardson(r,k)

            # Compute RMS change in solution
            e = sqrt(sum((r[1] - r_old)**2)/n)

            # Check for convergence
            if e < tol: 
                return r[1]
            r_old = r[1].copy()
        print("Midpoint method did not converge")

# Bulirsch-Stoer Algorithm:- 

''' X, Y = bulStoer(F, x, y, xStop, H, tol=1.0e-6).
    Simplified Bulirsch-Stoer method for solving the
    initial value problem {y}’ = {F(x,{y})}, where {y} = {y[0],y[1],...y[n-1]}
    x, y = initial conditions
    xStop = terminal value of x
    H = increment of x at which results are stored
    F = user-supplied function that returns the array F(x,y) = {y’[0],y’[1],...,y’[n-1]} '''

from numpy import array

def bulStoer(F, x, y, xStop, H, tol=1.0e-6):
    X = []
    Y = []
    X.append(x)
    Y.append(y)
    while x < xStop:
        H = min(H,xStop - x)
        y = integrate_BulirschStoeir(F, x, y, x + H, tol)   # Midpoint method
        x = x + H
        X.append(x)
        Y.append(y)
    return array(X), array(Y)

def printSoln(X, Y, freq):
        def printHead(n):
            print("\n x ")
            for i in range (n):
                print("y[",i,"]")
            print
        def printLine(x, y, n):
            print("%13.4e"% x)
            for i in range (n):
                print("%13.4e"% y[i])
            print
        m = len(Y)
        try: n = len(Y[0])
        except TypeError: n = 1
        if freq == 0: freq = m
        printHead(n)
        for i in range(0,m,freq):
            printLine(X[i], Y[i], n)
        if i != m - 1: printLine(X[m - 1], Y[m - 1], n)

# Code:-

from numpy import array, zeros

def F(x, y):
    F = zeros(2)
    F[0] = y[1]
    F[1] =(-y[1] - (y[0]/0.45) + 9.0)/2.0
    return F

x = 0.0
xStop = 10.0
H = 0.5
y = array([0.0, 0.0])
freq = 1
X, Y = bulStoer(F, x, y, xStop, H)
printSoln(X, Y, freq)

推荐答案

  1. "def integration_BulirschStoeir(F,x,y,xStop,tol)"中存在缩进问题(从"kMax = 51"到函数结尾).因此,在"X,Y = bulStoer(F,x,y,xStop,H)"和Y bacame矩阵的第一行为[0,0],其他行为None的情况下,Y中没有新数据.

  1. There was an indentation problem in "def integrate_BulirschStoeir(F, x, y, xStop, tol)" (from "kMax = 51" to the end of function). So, there was no new data in Y after "X, Y = bulStoer(F, x, y, xStop, H)" and Y bacame a matrix with [0,0] as first row and None as other rows.

此外,在"def printLine"中的打印(在打印功能中未添加"end ="")导致列向量,并添加了"end ="(")以使结果更易读.从numpy导入

Also the print in "def printLine" without addition of " end=' ' " in print-function led to a column-vector and " end=' ' " was added to make results more readable.

from numpy import zeros, float, sum, sqrt
import math

def integrate_BulirschStoeir(F, x, y, xStop, tol):

    def midpoint(F, x, y, xStop, nSteps):
        h = (xStop - x)/ nSteps

        y0 = y
        y1 = y0 + h*F(x, y0)
        for i in range(nSteps-1):

            x = x + h
            y2 = y0 + 2.0*h*F(x, y1)
            y0 = y1
            y1 = y2

        return 0.5*(y1 + y0 + h*F(x, y2))

    def richardson(r, k):
        for j in range(k-1,0,-1):
            const = (k/(k - 1.0))**(2.0*(k-j))
            r[j] = (const*r[j+1] - r[j])/(const - 1.0)
        return     
    kMax = 51 ## Indentation problem from here
    n = len(y)
    r = zeros((kMax, n), dtype=float)

    # Start with two integration steps
    nSteps = 2
    r[1] = midpoint(F, x, y, xStop, nSteps)
    r_old = r[1].copy()

    # Increase the number of integration points by 2 and refine result by Richardson extrapolation
    for k in range(2,kMax):
        nSteps = 2*k
        r[k] = midpoint(F, x, y, xStop, nSteps)
        richardson(r,k)

        # Compute RMS change in solution
        e = sqrt(sum((r[1] - r_old)**2)/n)

        # Check for convergence
        if e < tol: 
            return r[1]
        r_old = r[1].copy()
    print("Midpoint method did not converge")

# Bulirsch-Stoer Algorithm:- 

''' X, Y = bulStoer(F, x, y, xStop, H, tol=1.0e-6).
    Simplified Bulirsch-Stoer method for solving the
    initial value problem {y}’ = {F(x,{y})}, where {y} = {y[0],y[1],...y[n-1]}
    x, y = initial conditions
    xStop = terminal value of x
    H = increment of x at which results are stored
    F = user-supplied function that returns the array F(x,y) = {y’[0],y’[1],...,y’[n-1]} '''

from numpy import array

def bulStoer(F, x, y, xStop, H, tol=1.0e-6):
    X = []
    Y = []
    X.append(x)
    Y.append(y)
    while x < xStop:
        H = min(H,xStop - x)
        y = integrate_BulirschStoeir(F, x, y, x + H, tol)   # Midpoint method
        x = x + H
        X.append(x)
        Y.append(y)
    return array(X), array(Y)

def printSoln(X, Y, freq):
    def printHead(n):
        print("\n        x  ",end=" ") ## end=" " here
        for i in range(n):
            print("      y[",i,"] ",end=" ") ## end=" " here
        print()
    def printLine(x, y, n):
        print('{:13.4e}'.format(x), end=' ') ## end=" " here
        for i in range(n):
            print('{:13.4e}'.format(y[i]), end=' ') ## end=" " here
        print()
    m = Y.shape[0]
    try: n = Y.shape[1]
    except Exception: n = 1
    if freq == 0: freq = m
    printHead(n)
    for i in range(0,m,freq):
        printLine(X[i], Y[i], n)
    if i != m - 1: printLine(X[m - 1], Y[m - 1], n)

from numpy import array, zeros

def F(x, y):
    F = zeros(2)
    F[0] = y[1]
    F[1] =(-y[1] - (y[0]/0.45) + 9.0)/2.0
    return F

x = 0.0
xStop = 10.0
H = 0.5
y = array([0.0, 0.0])
freq = 1
X, Y = bulStoer(F, x, y, xStop, H)
printSoln(X, Y, freq)

这篇关于在Python中打印Bulirsch-Stoer算法的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆