H
H
Huma2021-10-07 23:48:22
Python
Huma, 2021-10-07 23:48:22

I'm trying to integrate the equation using the Runge-Kutta method on python, the task itself is set correctly, is there a problem in the program?

Hello!
I have been struggling with this task for a day now, and I had to ask a question in the forum. I'm not a big python expert, just a student, so please forgive me if this is a dumb question.
I need to perform a small task with the integration of the equation, the Runge-Kutta method, the method itself is clear. But the problem arises in the code itself, which I will attach below:

#данные нам значения
x = [0.5]
y = [1.2]
y1 = [0.1]

#количество точек
x_last = 10.5

#шаг
delta_x = 0.1
n = int((x_last - x[0])/delta_x)

#производная
def dy(x, y, y1):
    return (y1*x)/(y**2+2.0)



def fi_0(x, y, y1):
    global delta_x
    return (delta_x/2)*dy(x, y, y1)

def fi_1(x, y, y1):
    return (delta_x/2) * dy(x + delta_x/2, y + y1*(delta_x/2), y1 + fi_0(x, y, y1))

def fi_2(x, y, y1):
    return (delta_x/2) * dy(x + delta_x/2, y + y1*(delta_x/2) + fi_0(x, y, y1)*(delta_x/2), y1 + fi_1(x, y, y1))

def fi_3(x, y, y1):
    return (delta_x/2) * dy(x + delta_x, y + delta_x*(y1 + fi_1(x, y, y1)), y1 + 2 * fi_2(x, y, y1))

def y_n(x, y, y1):
    return y + delta_x * (y1 + 1/3*(fi_0(x, y, y1) + fi_1(x, y, y1) + fi_2(x, y, y1)))
def y_n1(x, y, y1):
    return y1 + 1/3 * (fi_0(x, y, y1) + 2*fi_1(x, y, y1) + 2*fi_2(x, y, y1) + fi_3(x, y, y1))


for i in range(n):
    x0 = x[-1]; y0 = y[-1]; y10 = y1[-1]
    x.append(x0 + delta_x)
    y.append(y0 + y_n(x0, y0, y10))
    y1.append(y10 + y_n1(x0, y0, y10))

#вывод
for xi, yi, y1i in zip(x, y, y1):
    print(f'{xi:.2f}\t{yi:.8f}\t{y1i:.8f}')


As I understand the problem is in this part of the code:
for i in range(n):
    x0 = x[-1]; y0 = y[-1]; y10 = y1[-1]


If I change the values ​​inside the square brackets y0 and y10 to zero, then the answer is similar to the result that should be, but still far from ideal. In its current state, the code shows me the answer in the form of huge numbers of 15-17th order, and the result should not exceed 2.2

I would be very grateful for any help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question