Question
I am trying to write a Python program that solves a second degree diferantial equation by using 4th degree Runge-Kutta. but there are some mistakes
I am trying to write a Python program that solves a second degree diferantial equation by using 4th degree Runge-Kutta.
but there are some mistakes so the program does not work and i get some errors.
this is the matrix form of the dif. eq.:
the program
import matplotlib.pyplot as plt import numpy as np from math import pi import math
m_1=465 m_2=50 k=5700 k_t=135 c=290
def x_t(t0): return math.sin(t0)
t0=0 h=0.01 tend=float(input("define the ending time= ")) Q,U,V,W,T=[],[],[],[],[]
def q(d1,d2,d3,d4,t): return d2 def u(d1,d2,d3,d4,t): return (-k/m_1)*d1+(-c/m_1)*d2+(k/m_1)*d3+(-c/m_1)*d4 def v(d1,d2,d3,d4,t): return d4 def w(d1,d2,d3,d4,t): return (k/m_2)*d1+(c/m_2)*d2+((-k_t-k)/m_2)*d3+(-c/m_2)*d4+(k_t/m_2)*x_t
def rkutta(q,u,v,w,d1_0,d2_0,d3_0,d4_0,t0):
while t0
t=t0+h
k1=q(d1_0,d2_0,d3_0,d4_0,t0) l1=u(d1_0,d2_0,d3_0,d4_0,t0) m1=v(d1_0,d2_0,d3_0,d4_0,t0) n1=w(d1_0,d2_0,d3_0,d4_0,t0)
k2=q(d1_0+(h/2),d2_0+(h/2)*k1,d3_0+(h/2),d4_0+(h/2)*k1,t0+(h/2)*k1) l2=u(d1_0+(h/2),d2_0+(h/2)*l1,d3_0+(h/2),d4_0+(h/2)*l1,t0+(h/2)*l1) m2=v(d1_0+(h/2),d2_0+(h/2)*m1,d3_0+(h/2),d4_0+(h/2)*m1,t0+(h/2)*m1) n2=w(d1_0+(h/2),d2_0+(h/2)*n1,d3_0+(h/2),d4_0+(h/2)*n1,t0+(h/2)*n1)
k3=q(d1_0+(h/2),d2_0+(h/2)*k2,d3_0+(h/2),d4_0+(h/2)*k2,t0+(h/2)*k2) l3=u(d1_0+(h/2),d2_0+(h/2)*l2,d3_0+(h/2),d4_0+(h/2)*l2,t0+(h/2)*l2) m3=v(d1_0+(h/2),d2_0+(h/2)*m2,d3_0+(h/2),d4_0+(h/2)*m2,t0+(h/2)*m2) n3=w(d1_0+(h/2),d2_0+(h/2)*n2,d3_0+(h/2),d4_0+(h/2)*n2,t0+(h/2)*n2)
k4=q(d1_0+(h/2),d2_0+(h/2)*k3,d3_0+(h/2),d4_0+(h/2)*k3,t0+(h/2)*k3) l4=u(d1_0+(h/2),d2_0+(h/2)*l3,d3_0+(h/2),d4_0+(h/2)*l3,t0+(h/2)*l3) m4=v(d1_0+(h/2),d2_0+(h/2)*m3,d3_0+(h/2),d4_0+(h/2)*m3,t0+(h/2)*m3) n4=w(d1_0+(h/2),d2_0+(h/2)*n3,d3_0+(h/2),d4_0+(h/2)*n3,t0+(h/2)*n3)
x1=d1_0+(h/6)*(k1+(2*k2)+(2*k3)+k4) x2=d2_0+(h/6)*(l1+(2*l2)+(2*l3)+l4) x3=d3_0+(h/6)*(m1+(2*m2)+(2*m3)+m4) x4=d4_0+(h/6)*(n1+(2*n2)+(2*n3)+n4)
d1_0=x1 d2_0=x2 d3_0=x3 d4_0=x4
t0=t T.append(t) Q.append(x1) U.append(x2) V.append(x3) W.append(x4)
return x1,x2,x3,x4,t
d=rkutta(q,u,v,w,0,0,0,0,0)
print(d)
plt.subplot(3,3,1) plt.plot(T,Q, "y") plt.title("t-x1 graph") plt.xlabel("t (s)") plt.ylabel("x1 (m/s)") plt.grid()
plt.subplot(3,3,3) plt.plot(T,U, "r") plt.title("t-x1' graph") plt.xlabel("t (s)") plt.ylabel("x1 (m/s2)") plt.grid()
plt.subplot(3,3,7) plt.plot(T,V, "b") plt.title("t-x2 graph") plt.xlabel("t (s)") plt.ylabel("x2 (m/s)") plt.grid()
plt.subplot(3,3,9) plt.plot(T,W, "g") plt.title("t-x2' graph") plt.xlabel("t (s)") plt.ylabel("x2 (m/s2) ") plt.grid() plt.show()
THE ERRORS I GET
Traceback (most recent call last): File "C:/Users/oguzt/Desktop/en yeni.py", line 83, in d=rkutta(q,u,v,w,0,0,0,0,0) File "C:/Users/oguzt/Desktop/en yeni.py", line 43, in rkutta n1=w(d1_0,d2_0,d3_0,d4_0,t0) File "C:/Users/oguzt/Desktop/en yeni.py", line 27, in w return (k/m_2)*d1+(c/m_2)*d2+((-k_t-k)/m_2)*d3+(-c/m_2)*d4+(k_t/m_2)*x_t TypeError: unsupported operand type(s) for *: 'float' and 'function' >>>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started