简体   繁体   中英

Printing two values using Runge Kutta Method

I am trying to print b and c from this code, but I am not having any luck. If I am correct, this code should output several points with a step size of 0.05, but I am not seeing it. Does anyone know how to print two values from this code?

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)

Your code does not have any print statement, so it will not print. Try inserting something like:

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

Where you want the print to happen. For Python 3 just add parentheses (print is a function now)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM