使用龙格库塔方法打印两个值

时间:2016-04-25 00:17:46

标签: python runge-kutta

我正在尝试从此代码中打印b和c,但我没有运气。如果我是正确的,这段代码应输出步长为0.05的几个点,但我没有看到它。有谁知道如何从这段代码中打印两个值?

import math

def rK3(a, b, c, fa, fb, fc, hs):
    a1 = fa(a, b, c)*hs
    b1 = fb(a, b, c)*hs
    c1 = fc(a, b, c)*hs
    ak = a + a1*0.5
    bk = b + b1*0.5
    ck = c + c1*0.5
    a2 = fa(ak, bk, ck)*hs
    b2 = fb(ak, bk, ck)*hs
    c2 = fc(ak, bk, ck)*hs
    ak = a + a2*0.5
    bk = b + b2*0.5
    ck = c + c2*0.5
    a3 = fa(ak, bk, ck)*hs
    b3 = fb(ak, bk, ck)*hs
    c3 = fc(ak, bk, ck)*hs
    ak = a + a3
    bk = b + b3
    ck = c + c3
    a4 = fa(ak, bk, ck)*hs
    b4 = fb(ak, bk, ck)*hs
    c4 = fc(ak, bk, ck)*hs
    a = a + (a1 + 2*(a2 + a3) + a4)/6
    b = b + (b1 + 2*(b2 + b3) + b4)/6
    c = c + (c1 + 2*(c2 + c3) + c4)/6
    return a, b, c

def fa2(a, b, c):
    return 0.9*(1 - b*b)*a - b + math.sin(c)

def fb2(a, b, c):
    return a

def fc2(a, b, c):
    return 0.5

def VDP2():
    a, b, c, hs = 1, 1, 0, 0.05
    while (c<6):
        a, b, c = rK3(a, b, c, fa2, fb2, fc2, hs)

1 个答案:

答案 0 :(得分:0)

您的代码没有任何打印声明,因此无法打印。 尝试插入类似的内容:

print 'b = {0}, c = {1}'.format(b,c)

您希望打印发生的位置。对于Python 3,只需添加括号(print现在是一个函数)

print('b = {0}, c = {1}'.format(b,c))