Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

runge kutta

# RK-4 method python program

# function to be solved
def f(x,y):
    return x+y

# or
# f = lambda x: x+y

# RK-4 method
def rk4(x0,y0,xn,n):
    
    # Calculating step size
    h = (xn-x0)/n
    
    print('
--------SOLUTION--------')
    print('-------------------------')    
    print('x0	y0	yn')
    print('-------------------------')
    for i in range(n):
        k1 = h * (f(x0, y0))
        k2 = h * (f((x0+h/2), (y0+k1/2)))
        k3 = h * (f((x0+h/2), (y0+k2/2)))
        k4 = h * (f((x0+h), (y0+k3)))
        k = (k1+2*k2+2*k3+k4)/6
        yn = y0 + k
        print('%.4f	%.4f	%.4f'% (x0,y0,yn) )
        print('-------------------------')
        y0 = yn
        x0 = x0+h
    
    print('
At x=%.4f, y=%.4f' %(xn,yn))

# Inputs
print('Enter initial conditions:')
x0 = float(input('x0 = '))
y0 = float(input('y0 = '))

print('Enter calculation point: ')
xn = float(input('xn = '))

print('Enter number of steps:')
step = int(input('Number of steps = '))

# RK4 method call
rk4(x0,y0,xn,step)
Comment

PREVIOUS NEXT
Code Example
Python :: sklearn logistic regression get probability 
Python :: tdmq python 
Python :: python - count values that contain special characters 
Python :: how to import date python 
Python :: how to download the captions of a youtube video 
Python :: df count zeros 
Python :: python list to bytes 
Python :: death stranding 
Python :: python pyramid 
Python :: python pywhatkit 
Python :: how to use xpath with beautifulsoup 
Python :: renaming column in dataframe pandas 
Python :: how to define a constant in python 
Python :: how to hide turtle in python 
Python :: word generator in python 
Python :: print subscript and superscript python 
Python :: images in django 
Python :: get python path 
Python :: how to return total elements in database django 
Python :: python how to find gcd 
Python :: No package python37 available. 
Python :: python: measure time code 
Python :: python reverse words in string 
Python :: sqlalchemy create engine Microsoft SQL 
Python :: python append a file and read 
Python :: how to create a label in python 
Python :: pandas read from txt separtion 
Python :: how to delete json object using python? 
Python :: correlation analysis of dataframe python 
Python :: remove add button django admin 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =