python two coupled second order ODEs Runge Kutta 4th order - python

I am a beginner in python. I have a problem with 2 ODEs that are second order and they are coupled. I want to solve it with Runge Kutta 4th order. I have made 2 matrices.[A] and [B] that V' = A*C + B . But I couldn't get the answer. Can anyone help me?
This is equations
Equations
this is my code:
import numpy as np
from math import sqrt
from sympy import *
import matplotlib.pyplot as plt
x,v1, v2, v3, v4, dv1, dv2, dv3, dv4, t = symbols('x v1 v2 v3 v4 dv1 dv2 dv3 dv4 t')
# Parameters
mr =
m =
c =
k =
F =
# Equations:
A = Matrix([[0, 0, 1, 0], [0, 0, 0, 1], [-k / mr, k / mr, -c / mr, c / mr], [k / m, -k / m, c / m, -c / m]])
B = Matrix([0, 0, 0, -F])
dv1 = diff(v1, t)
dv2 = diff(v2, t)
dv3 = diff(v3, t)
dv4 = diff(v4, t)
C = Matrix([v1,v2,v3,v4])
def V(v1,v2,v3,v4,t):
V = A*C+B
return V
def rk4(f, v1, v2, v3 ,v4 , x0 , x1 , n):
k11 = [0] * (n + 1)
k21 = [0] * (n + 1)
k31 = [0] * (n + 1)
k41 = [0] * (n + 1)
k12 = [0] * (n + 1)
k22 = [0] * (n + 1)
k32 = [0] * (n + 1)
k42 = [0] * (n + 1)
k13 = [0] * (n + 1)
k23 = [0] * (n + 1)
k33 = [0] * (n + 1)
k43 = [0] * (n + 1)
k14 = [0] * (n + 1)
k24 = [0] * (n + 1)
k34 = [0] * (n + 1)
k44 = [0] * (n + 1)
h = (x1 - x0) / n
for i in range(1, n + 1):
k11 = h * f(v1, v2, v3, v4, t)
k21 = h * f(v1, v2, v3, v4, t)
k31 = h * f(v1, v2, v3, v4, t)
k41 = h * f(v1, v2, v3, v4, t)
k12 = h * f(v1 + 0.5 * h, v2 + 0.5 * h, v3 + 0.5 * h, v4 + 0.5 * h, t + 0.5 * k11)
k22 = h * f(v1 + 0.5 * h, v2 + 0.5 * h, v3 + 0.5 * h, v4 + 0.5 * h, t + 0.5 * k21)
k32 = h * f(v1 + 0.5 * h, v2 + 0.5 * h, v3 + 0.5 * h, v4 + 0.5 * h, t + 0.5 * k31)
k42 = h * f(v1 + 0.5 * h, v2 + 0.5 * h, v3 + 0.5 * h, v4 + 0.5 * h, t + 0.5 * k41)
k13 = h * f(v1 + 0.5 * h, v2 + 0.5 * h, v3 + 0.5 * h, v4 + 0.5 * h, t + 0.5 * k12)
k23 = h * f(v1 + 0.5 * h, v2 + 0.5 * h, v3 + 0.5 * h, v4 + 0.5 * h, t + 0.5 * k22)
k33 = h * f(v1 + 0.5 * h, v2 + 0.5 * h, v3 + 0.5 * h, v4 + 0.5 * h, t + 0.5 * k32)
k43 = h * f(v1 + 0.5 * h, v2 + 0.5 * h, v3 + 0.5 * h, v4 + 0.5 * h, t + 0.5 * k42)
k14 = h * f(v1 + h, v2 + h, v3 + h, v4 + h, t + k13)
k24 = h * f(v1 + h, v2 + h, v3 + h, v4 + h, t + k23)
k34 = h * f(v1 + h, v2 + h, v3 + h, v4 + h, t + k33)
k44 = h * f(v1 + h, v2 + h, v3 + h, v4 + h, t + k43)
t[i] = t + i * h
jv1[i] = v1 + (k11 + k12 + k12 + k13 + k13 + k14) / 6
jv2[i] = v2 + (k21 + k22 + k22 + k23 + k23 + k24) / 6
jv3[i] = v3 + (k31 + k32 + k32 + k33 + k33 + k34) / 6
jv4[i] = v4 + (k41 + k42 + k42 + k43 + k43 + k44) / 6
return jv1, jv2 , jv3, jv4
jv1, jv2 , jv3 , jv4 = rk4(V, 0, 0, 1, 1 , 0 , 1 , 10)
plt.plot(jv1, jv2 , jv3 , jv4 , t)
plt.grid('on')
plt.show()

Cutting away unused and contradicting variables and statements a running reduction and correction of your code is
import numpy as np
from math import sqrt
import matplotlib.pyplot as plt
# Parameters
mr = 2
m = 1
c = 5
k = 36
F = 3
# Equations:
def V(u,t):
x1, x2, v1, v2 = u
dx = x2-x1;
dv = v2-v1;
return np.array([ v1, v2, (c*dv+k*dx)/mr, -(F+c*dv+k*dx)/m ])
def rk4(f, u0, t0, tf , n):
t = np.linspace(t0, tf, n+1)
u = np.array((n+1)*[u0])
h = t[1]-t[0]
for i in range(n):
k1 = h * f(u[i], t[i])
k2 = h * f(u[i] + 0.5 * k1, t[i] + 0.5*h)
k3 = h * f(u[i] + 0.5 * k2, t[i] + 0.5*h)
k4 = h * f(u[i] + k3, t[i] + h)
u[i+1] = u[i] + (k1 + 2*(k2 + k3) + k4) / 6
return u, t
u, t = rk4(V, np.array([0., 0., 1., 1.]) , 0. , 1. , 10)
x1, x2, v1, v2 = u.T
plt.plot(t, x1, t, x2)
plt.grid('on')
plt.show()
If that is in any way a correct result is up to you to determine.

Related

Bad floating point accuracy with numpy

I have a solution for a mathematical problem that looks like this (sorry for the formatting, that's what PyCharm is doing):
sigma = (wf_s[0] * u ** 4 + wf_s[1] * u ** 3 * v + wf_s[2] * u ** 3 + wf_s[3] * u ** 2 * v ** 2 + wf_s[
4] * u ** 2 * v + wf_s[5] * u ** 2 + wf_s[6] * u * v ** 3 + wf_s[7] * u * v ** 2 + wf_s[8] * u * v + wf_s[
9] * u + wf_s[10] * v ** 4 + wf_s[11] * v ** 3 + wf_s[12] * v ** 2 + wf_s[13] * v + wf_s[14]) / (
wf_s[15] * u ** 4 + wf_s[16] * u ** 3 * v + wf_s[17] * u ** 3 + wf_s[18] * u ** 2 * v ** 2 +
wf_s[19] * u ** 2 * v + wf_s[20] * u ** 2 + wf_s[21] * u * v ** 3 + wf_s[22] * u * v ** 2 +
wf_s[23] * u * v + wf_s[24] * u + wf_s[25] * v ** 4 + wf_s[26] * v ** 3 + wf_s[
27] * v ** 2 + wf_s[28] * v + wf_s[29])
sigma = sqrt(sigma)
x1 = (wf_x1[0] * u ** 2 * sigma + wf_x1[1] * u ** 2 + wf_x1[2] * u * v * sigma + wf_x1[3] * u * v + wf_x1[
4] * u * sigma + wf_x1[5] * u + wf_x1[6] * v ** 2 * sigma + wf_x1[7] * v ** 2 + wf_x1[8] * v * sigma +
wf_x1[9] * v + wf_x1[10] * sigma + wf_x1[11]) / (
wf_x1[12] * u ** 2 + wf_x1[13] * u * v + wf_x1[14] * u + wf_x1[15] * v ** 2 + wf_x1[16] * v +
wf_x1[17])
x2 = (wf_x2[0] * u ** 2 * sigma + wf_x2[1] * u ** 2 + wf_x2[2] * u * v * sigma + wf_x2[3] * u * v + wf_x2[
4] * u * sigma + wf_x2[5] * u + wf_x2[6] * v ** 2 * sigma + wf_x2[7] * v ** 2 + wf_x2[8] * v * sigma +
wf_x2[9] * v + wf_x2[10] * sigma + wf_x2[11]) / (
wf_x2[12] * u ** 2 + wf_x2[13] * u * v + wf_x2[14] * u + wf_x2[15] * v ** 2 + wf_x2[16] * v +
wf_x2[17])
y1 = (wf_y1[0] * u ** 2 * sigma + wf_y1[1] * u ** 2 + wf_y1[2] * u * v * sigma + wf_y1[3] * u * v + wf_y1[
4] * u * sigma + wf_y1[5] * u + wf_y1[6] * v ** 2 * sigma + wf_y1[7] * v ** 2 + wf_y1[8] * v * sigma +
wf_y1[9] * v + wf_y1[10] * sigma + wf_y1[11]) / (
wf_y1[12] * u ** 2 + wf_y1[13] * u * v + wf_y1[14] * u + wf_y1[15] * v ** 2 + wf_y1[16] * v +
wf_y1[17])
y2 = (wf_y2[0] * u ** 2 * sigma + wf_y2[1] * u ** 2 + wf_y2[2] * u * v * sigma + wf_y2[3] * u * v + wf_y2[
4] * u * sigma + wf_y2[5] * u + wf_y2[6] * v ** 2 * sigma + wf_y2[7] * v ** 2 + wf_y2[8] * v * sigma +
wf_y2[9] * v + wf_y2[10] * sigma + wf_y2[11]) / (
wf_y2[12] * u ** 2 + wf_y2[13] * u * v + wf_y2[14] * u + wf_y2[15] * v ** 2 + wf_y2[16] * v +
wf_y2[17])
This calculation is running relatively slow in Python so I wanted to increase the speed using numpy. The result looks like this:
u2 = u ** 2
u3 = u ** 3
u4 = u ** 4
v2 = v ** 2
v3 = v ** 3
v4 = v ** 4
uv_s = np.array((u4, u3 * v, u3, u2 * v2, u2 * v, u2, u * v3, u * v2, u * v, u, v4, v3, v2, v, 1))
sigma = np.dot(wf_s[:15], uv_s) / np.dot(wf_s[15:], uv_s)
sigma = sqrt(sigma)
uv_xy_n = np.array((u2 * sigma, u2, u * v * sigma, u * v, u * sigma, u, v2 * sigma, v2, v * sigma, v, sigma, 1))
uv_xy_d = np.array((u2, u * v, u, v2, v, 1))
x12 = np.dot(wf_x1[:12], uv_xy_n) / np.dot(wf_x1[12:], uv_xy_d)
x22 = np.dot(wf_x2[:12], uv_xy_n) / np.dot(wf_x2[12:], uv_xy_d)
y12 = np.dot(wf_y1[:12], uv_xy_n) / np.dot(wf_y1[12:], uv_xy_d)
y22 = np.dot(wf_y2[:12], uv_xy_n) / np.dot(wf_y2[12:], uv_xy_d)
In theory this works fine and runs 3 times faster than my first method but the accuracy of the results is horrible. In my case, x and y are in the hundreds and the second method produces results that differ up to 0.2 from the real result. This accuracy is of no use in my case. Plotting the results of the first method results in a perfect plot as expected. Plotting the results of the second method results in a super noisy plot.
Why is the accuracy of the numpy version so bad and how can I improve it?

Troubles with glDrawArrays

I'm trying to rework program on glDrawArray . If i use justglVertex3feverything is fine and if i useglDrawArrays` for the same values of vertexes i get some random lines in addition to the figure.
with glDrawArrays
ususal glVertex3f as it should look like
And in 50% i get an error
pgl.glDrawArrays(pgl.GL_TRIANGLE_STRIP, 0, array_size)
OSError: exception: access violation reading 0x0CF52000
code (pastbin)
import pyglet
import pyglet.gl as pgl
from pyglet.window import key
import math
import json
import time
win = pyglet.window.Window(1300, 1000, resizable=True)
global_time = time.time()
frame_counter = 0
WINDOW = 1000
tr = 96
tr_temp = 90
INCREMENT = 5
transparant = False
xRotation = -70
zRotation = -30
yRotation = 180
zoom = 1
x = -180
y = 70
z = 0
light = False
param = 0
t_prev = -5
start_loop = False
run_l = False
Texture = False
texture_obj = None
lamp1 = False
lamp2 = False
lamp3 = False
data = {}
with open('PycharmProjects/untitled/data.json') as fp:
data = json.load(fp)
transparant = data['transparant']
xRotation = data['xRotation']
yRotation = data['yRotation']
zRotation = data['zRotation']
zoom = data['zoom']
x = data['x']
y = data['y']
z = data['z']
light = data['light']
param = data['param']
t_prev = data['t_prev']
start_loop = data['start_loop']
run_l = data['run_l']
Texture = data['Texture']
lamp1 = data['lamp1']
lamp2 = data['lamp2']
lamp3 = data['lamp3']
tr = data['tr']
def writeToJSONFile(path, filename, data):
filePathName = path + '.json'
with open(filePathName, 'w') as fp:
json.dump(data,fp)
def run_loop(dt):
on_draw()
def draw_figure():
global texture_obj
angle = 0
t = 0
x = y = z = 0
a = 50
b = 100
array_size = 0
array = []
if Texture:
if texture_obj == None:
texture_obj = pyglet.image.load("PycharmProjects/untitled/test1_texture.bmp").get_texture()
pgl.glBindTexture(pgl.GL_TEXTURE_2D, texture_obj.id)
pgl.glEnable(pgl.GL_TEXTURE_2D)
# pgl.glTexParameterf(pgl.GL_TEXTURE_2D, pgl.GL_TEXTURE_WRAP_T, pgl.GL_REPEAT)
# pgl.glTexParameterf(pgl.GL_TEXTURE_2D, pgl.GL_TEXTURE_MAG_FILTER, pgl.GL_NEAREST)
# #
#
# pgl.glTexEnvi(pgl.GL_TEXTURE_ENV, pgl.GL_TEXTURE_ENV_MODE, pgl.GL_REPLACE)
else:
pgl.glDisable(pgl.GL_TEXTURE_2D)
if not transparant:
pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
else:
pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_LINE)
pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
for i in range(0, tr + 1):
pgl.glColor3ub(0, 0, 0)
if i >= 1:
t1 = a1
t2 = a2
t3 = a3
u = math.cos(i * 2 * math.pi / tr)
v = math.sin(i * 2 * math.pi / tr)
a1 = x + a * u
a2 = y + b * v
a3 = z + 8
if Texture:
pgl.glTexCoord2f(u * 0.5 + 0.5, v * 0.5 + 0.5)
pgl.glVertex3f(a1, a2, a3)
if i >= 1:
pgl.glNormal3f((b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
(b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1))
b1 = x
b2 = y
b3 = z + 8
if Texture:
pgl.glTexCoord2f(0.5, 0.5)
pgl.glVertex3f(b1, b2, b3)
pgl.glEnd()
for j in range(1, 27):
pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
pgl.glColor3ub(0, 0, 0)
x1 = a * math.cos(0)
y1 = b * math.sin(0)
a1 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
a2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
a3 = z + j * 8
if Texture:
pgl.glTexCoord2f(0.0, 0.0)
a01 = a1
a02 = a2
a03 = a3
print(a1,a2,a3)
array.extend([a1,a2,a3])
array_size += 3
# pgl.glVertex3f(a1, a2, a3)
b1 = x1 * math.cos((t + 16) * math.pi / 360) + y1 * math.sin((t + 16) * math.pi / 360)
b2 = - x1 * math.sin((t + 16) * math.pi / 360) + y1 * math.cos((t + 16) * math.pi / 360)
b3 = z + j * 8 + 8
u = math.cos((t + 16) * math.pi / 360) + math.sin((t + 16) * math.pi / 360)
v = (j - 1) / 26
if Texture:
pgl.glTexCoord2f(0.0, 0.0)
array.extend([b1,b2,b3])
print(b1,b2,b3)
array_size += 3
# pgl.glVertex3f(b1, b2, b3)
b01 = b1
b02 = b2
b03 = b3
for i in range(1, tr):
pgl.glColor3ub(0, 0, 0)
x1 = a * math.cos(i * 2 * math.pi / tr)
y1 = b * math.sin(i * 2 * math.pi / tr)
t1 = a1
t2 = a2
t3 = a3
a1 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
a2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
a3 = z + j * 8
pgl.glNormal3f((b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
(b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1))
v = math.cos(t * math.pi / 360) + math.sin(t * math.pi / 360)
if Texture:
pgl.glTexCoord2f((i - 1) / tr, (26 - j) / 26)
array.extend([a1,a2,a3])
print(a1,a2,a3)
array_size += 3
# pgl.glVertex3f(a1, a2, a3)
x1 = a * math.cos(i * 2 * math.pi / tr)
y1 = b * math.sin(i * 2 * math.pi / tr)
b1 = x1 * math.cos((t + 16) * math.pi / 360) + y1 * math.sin((t + 16) * math.pi / 360)
b2 = - x1 * math.sin((t + 16) * math.pi / 360) + y1 * math.cos((t + 16) * math.pi / 360)
b3 = z + j * 8 + 8
v = math.cos((t + 16) * math.pi / 360) + math.sin((t + 16) * math.pi / 360)
if Texture:
pgl.glTexCoord2f((i - 1) / tr, (25 - j) / 26)
array.extend([b1,b2,b3])
print(b1,b2,b3)
array_size += 3
# pgl.glVertex3f(b1, b2, b3)
print(a01,a02,a03)
array.extend([a01,a02,a03])
array_size += 3
# pgl.glVertex3f(a01, a02, a03)
print(b01,b02,b03)
array.extend([b01,b02,b03])
array_size += 3
# pgl.glVertex3f(b01, b02, b03)
t = t + 16
pgl.glEnd()
pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
t = 416
z = z + 144
angle = angle + t
for i in range(0, tr + 1):
pgl.glColor3ub(0, 0, 0)
u = math.cos(i * 2 * math.pi / tr)
v = math.sin(i * 2 * math.pi / tr)
x1 = x + a * u
y1 = y + b * v
x2 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
y2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
if i >= 1:
t1 = a1
t2 = a2
t3 = a3
a1 = x2
a2 = y2
a3 = z + 72
if Texture:
pgl.glTexCoord2f(u * 0.5 + 0.5, v * 0.5 + 0.5)
pgl.glVertex3f(a1, a2, a3)
if i >= 1:
pgl.glNormal3f((b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
(b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1))
b1 = x
b2 = y
b3 = z + 72
if Texture:
pgl.glTexCoord2f(0.5, 0.5)
pgl.glVertex3f(b1, b2, b3)
angle = angle + i
pgl.glEnd()
pgl.glEnableClientState(pgl.GL_VERTEX_ARRAY)
data = (pgl.GLfloat * len(array))(*array)
pgl.glVertexPointer(3, pgl.GL_FLOAT, 0, data)
pgl.glDrawArrays(pgl.GL_TRIANGLE_STRIP, 0, array_size)
pgl.glDisableClientState(pgl.GL_VERTEX_ARRAY)
#win.event
def on_draw():
global x, y, z, start_loop, run_l, param, t_prev, zRotation,frame_counter
start_time = time.time()
pgl.glClearColor(0.0, 0.0, 0.0, 0.0)
pgl.glEnable(pgl.GL_DEPTH_TEST)
if light:
pgl.glEnable(pgl.GL_LIGHTING)
pgl.glLightModelf(pgl.GL_LIGHT_MODEL_TWO_SIDE, pgl.GL_FALSE)
pgl.glEnable(pgl.GL_NORMALIZE)
else:
pgl.glDisable(pgl.GL_LIGHTING)
pgl.glDisable(pgl.GL_NORMALIZE)
pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)
pgl.glLoadIdentity()
pgl.glViewport(0, 0, 1300, 1000)
pgl.glMatrixMode(pgl.GL_PROJECTION)
pgl.glLoadIdentity()
pgl.glOrtho(-1300 / 4, 1300 / 4, -1000 / 4, 1000 / 4, -400, 600)
pgl.glMatrixMode(pgl.GL_MODELVIEW)
pgl.glLoadIdentity()
if light:
material_diffuse = [1.0, 1.0, 1.0, 1.0]
pgl.glMaterialfv(pgl.GL_FRONT_AND_BACK, pgl.GL_DIFFUSE,
(pgl.GLfloat * len(material_diffuse))(*material_diffuse))
if lamp1:
light5_diffuse = [1.0, 0.0, 0.0];
light5_position = [0, 0, -1.0, 0];
pgl.glEnable(pgl.GL_LIGHT5);
pgl.glLightfv(pgl.GL_LIGHT5, pgl.GL_DIFFUSE, (pgl.GLfloat * len(light5_diffuse))(*light5_diffuse));
pgl.glLightfv(pgl.GL_LIGHT5, pgl.GL_POSITION, (pgl.GLfloat * len(light5_position))(*light5_position));
pgl.glLightf(pgl.GL_LIGHT5, pgl.GL_CONSTANT_ATTENUATION, 0.0);
pgl.glLightf(pgl.GL_LIGHT5, pgl.GL_LINEAR_ATTENUATION, 0.4);
pgl.glLightf(pgl.GL_LIGHT5, pgl.GL_QUADRATIC_ATTENUATION, 0.8);
else:
pgl.glDisable(pgl.GL_LIGHT5);
if lamp2:
light6_diffuse = [0.0, 1.0, 0.0];
light6_position = [1, 0.5, -1.0, 0];
pgl.glEnable(pgl.GL_LIGHT6);
pgl.glLightfv(pgl.GL_LIGHT6, pgl.GL_DIFFUSE, (pgl.GLfloat * len(light6_diffuse))(*light6_diffuse));
pgl.glLightfv(pgl.GL_LIGHT6, pgl.GL_POSITION, (pgl.GLfloat * len(light6_position))(*light6_position));
pgl.glLightf(pgl.GL_LIGHT6, pgl.GL_CONSTANT_ATTENUATION, 0.0);
pgl.glLightf(pgl.GL_LIGHT6, pgl.GL_LINEAR_ATTENUATION, 0.4);
pgl.glLightf(pgl.GL_LIGHT6, pgl.GL_QUADRATIC_ATTENUATION, 0.8);
else:
pgl.glDisable(pgl.GL_LIGHT6);
if lamp3:
light7_diffuse = [0.0, 0.0, 1.0];
light7_position = [0.5 * math.cos(4 * math.pi / 3), 0.5 * math.sin(4 * math.pi / 3), 1.0, 0];
pgl.glEnable(pgl.GL_LIGHT7);
pgl.glLightfv(pgl.GL_LIGHT7, pgl.GL_DIFFUSE, (pgl.GLfloat * len(light7_diffuse))(*light7_diffuse));
pgl.glLightfv(pgl.GL_LIGHT7, pgl.GL_POSITION, (pgl.GLfloat * len(light7_position))(*light7_position));
pgl.glLightf(pgl.GL_LIGHT7, pgl.GL_CONSTANT_ATTENUATION, 0.0);
pgl.glLightf(pgl.GL_LIGHT7, pgl.GL_LINEAR_ATTENUATION, 0.4);
pgl.glLightf(pgl.GL_LIGHT7, pgl.GL_QUADRATIC_ATTENUATION, 0.8);
else:
pgl.glDisable(pgl.GL_LIGHT7);
pgl.glPolygonMode(pgl.GL_FRONT_AND_BACK, pgl.GL_FILL)
pgl.glPushMatrix()
pgl.glTranslatef(x, y, z)
pgl.glRotatef(xRotation, 1, 0, 0)
pgl.glRotatef(yRotation, 0, 1, 0)
pgl.glRotatef(zRotation, 0, 0, 1)
pgl.glScalef(zoom, zoom, zoom)
draw_figure()
if start_loop:
start_loop = False
run_l = True
x = ((1 - (param / 100)) ** 3) * (-180) + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
(param / 100) ** 2) * (
1 - (param / 100)) * (0) + ((param / 100) ** 3) * (200)
y = ((1 - (param / 100)) ** 3) * 70 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (140) + 3 * (
(param / 100) ** 2) * (
1 - (param / 100)) * (140) + ((param / 100) ** 3) * (
70)
z = ((1 - (param / 100)) ** 3) * 0 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
(param / 100) ** 2) * (
1 - (param / 100)) * (100) + ((param / 100) ** 3) * (
0)
elif run_l:
if param < 100 and param > t_prev:
zRotation += INCREMENT
param = param + 1
t_prev = t_prev + 1
if (param == 100):
t_prev = 105
x = ((1 - (param / 100)) ** 3) * (-180) + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
(param / 100) ** 2) * (1 - (param / 100)) * (0) + ((param / 100) ** 3) * (200)
y = ((1 - (param / 100)) ** 3) * 70 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (140) + 3 * (
(param / 100) ** 2) * (1 - (param / 100)) * (140) + ((param / 100) ** 3) * (
70)
z = ((1 - (param / 100)) ** 3) * 0 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
(param / 100) ** 2) * (1 - (param / 100)) * (100) + ((param / 100) ** 3) * (
0)
if param > 0 and param < t_prev:
zRotation -= INCREMENT
param = param - 1
t_prev = t_prev - 1
if (param == 0):
t_prev = -5
x = ((1 - (param / 100)) ** 3) * (-180) + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
(param / 100) ** 2) * (1 - (param / 100)) * (0) + ((param / 100) ** 3) * (200)
y = ((1 - (param / 100)) ** 3) * 70 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (140) + 3 * (
(param / 100) ** 2) * (1 - (param / 100)) * (140) + ((param / 100) ** 3) * (
70)
z = ((1 - (param / 100)) ** 3) * 0 + 3 * (param / 100) * ((1 - (param / 100)) ** 2) * (100) + 3 * (
(param / 100) ** 2) * (1 - (param / 100)) * (100) + ((param / 100) ** 3) * (
0)
frame_counter +=1
# print("FPS: ", 1.0 / (time.time() - start_time))
pgl.glPopMatrix()
#win.event
def on_text_motion(motion):
global xRotation, yRotation, INCREMENT
if motion == key.UP:
xRotation -= INCREMENT
elif motion == key.DOWN:
xRotation += INCREMENT
elif motion == key.LEFT:
yRotation -= INCREMENT
elif motion == key.RIGHT:
yRotation += INCREMENT
#win.event
def on_key_press(symbol, modifiers):
global transparant, data, tr, x, y, z, INCREMENT, zRotation, light, t, start_loop, run_l, Texture, tr_temp
global xRotation, yRotation, zRotation,zoom, t_prev, texture_obj, lamp1, lamp2, lamp3, global_time, frame_counter
if symbol == key.T and not transparant:
transparant = True
elif symbol == key.T and transparant:
transparant = False
elif symbol == key.W:
x += 2.5
elif symbol == key.S:
x -= 2.5
elif symbol == key.A:
y -= 2.5
elif symbol == key.D:
y += 2.5
elif symbol == key.Q:
z -= 2.5
elif symbol == key.E:
z += 2.5
elif symbol == key.L:
light = not light
lamp1 = True
lamp2 = True
lamp3 = True
elif symbol == key.Z:
zRotation += INCREMENT
elif symbol == key.X:
zRotation -= INCREMENT
elif symbol == key.ESCAPE:
path = 'PycharmProjects/untitled/data'
fileName = 'data'
data['transparant'] = transparant
data['xRotation'] = xRotation
data['yRotation'] = yRotation
data['zRotation'] = zRotation
data['zoom'] = zoom
data['x'] = x
data['y'] = y
data['z'] = z
data['light'] = light
data['param'] = param
data['t_prev'] = t_prev
data['start_loop'] = start_loop
data['run_l'] = run_l
data['Texture'] = Texture
data['lamp1'] = lamp1
data['lamp2'] = lamp2
data['lamp3'] = lamp3
data['tr'] = tr
data['tr_temp'] = tr_temp
writeToJSONFile(path, fileName, data)
pyglet.app.exit()
elif symbol == key.P:
tr += 5
elif symbol == key.O:
if tr > 6:
tr -= 5
elif symbol == key.SPACE:
if not run_l:
tr_temp = tr
tr = 6
start_loop = True
else:
run_l = False
tr = tr_temp
elif symbol == key.M:
print("pressed")
Texture = not Texture
elif symbol == key._1:
lamp1 = not lamp1
elif symbol == key._2:
lamp2 = not lamp2
elif symbol == key._3:
lamp3 = not lamp3
elif symbol == key.F:
print("FPS: ", frame_counter / (time.time() - global_time))
frame_counter = 0
global_time = time.time()
#win.event
def on_mouse_scroll(x, y, scroll_x, scroll_y):
global zoom
if scroll_y < 0:
zoom += 0.1
elif scroll_y > 0 and zoom - 0.1 > 0:
zoom -= 0.1
#win.event
def on_mouse_press(x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
print(x, y)
pyglet.clock.schedule_interval(run_loop, 0.1)
pyglet.app.run()
(for the first launch turn off reading from json file, then after first closing using esc the programme will read saved parameters)
Troubles in draw_figure
The major issue is, that GL_TRIANGLE_STRIP can not be connected arbitrarily:
1 3 5
+-----+-----+
| \ | \ |
| \ | \ |
+-----+-----+
0 2 4
7 9 11
+-----+-----+
| \ | \ |
| \ | \ |
+-----+-----+
6 8 10
If the above strips are join directly, then this would generate the triangles 4 - 5 - 6 and 5 - 6 -7, which are not wanted.
But this can be avoided by simple trick. You've to add duplicate vertices. Note if 2 points of triangle are equal, the area of the triangle is zero.
If the vertices 5 and 6 would be add twice, then the strips are concatenated by the triangles 4 - 5 - 5, 5 - 5 - 6, 5 - 6 - 6 and 6 - 6 - 7. Each of this triangles has no size.
I recommend to store the vertex coordinates and attributes to lists:
vertex = []
color = []
normal = []
textcoord = []
Define a function which extends the lists by coordinates and attributes:
def add_vertex(v, c, t, n):
global vertex, color, normal, textcoord
vertex.extend(v)
color.extend(c)
textcoord.extend(t)
normal.extend(n)
Define a function which duplicates the last coordinates and attributes in the list, which can be used for the concatenation of triangle strips:
def repeat_vertex():
add_vertex(vertex[-3:], color[-3:], textcoord[-2:], normal[-3:])
Define another function, which constructs the shape (figure). The function has to define all the attributes (color, texture coordinates and normal vector), regardless if they are used later:
def construct_figure():
angle = 0
t = 0
x = y = z = 0
a = 50
b = 100
for i in range(0, tr + 1):
if i >= 1:
t1, t2, t3 = a1, a2, a3
u, v = math.cos(i * 2 * math.pi / tr), math.sin(i * 2 * math.pi / tr)
a1, a2, a3 = x + a * u, y + b * v, z + 8
add_vertex([a1, a2, a3],
[u * 0.5 + 0.5, 0, v * 0.5 + 0.5],
[u * 0.5 + 0.5, v * 0.5 + 0.5],
[0, 0, -1])
b1, b2, b3 = x, y, z + 8
add_vertex([b1, b2, b3], [0.5, 0, 0.5], [0.5, 0.5], normal[-3:])
repeat_vertex()
for j in range(1, 27):
x1, y1 = a * math.cos(0), b * math.sin(0)
a1 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
a2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
a3 = z + j * 8
a01, a02, a03 = a1, a2, a3
add_vertex([a1, a2, a3], [0, 0, 0], [0, 0], normal[-3:])
repeat_vertex()
b1 = x1 * math.cos((t + 16) * math.pi / 360) + y1 * math.sin((t + 16) * math.pi / 360)
b2 = - x1 * math.sin((t + 16) * math.pi / 360) + y1 * math.cos((t + 16) * math.pi / 360)
b3 = z + j * 8 + 8
u = math.cos((t + 16) * math.pi / 360) + math.sin((t + 16) * math.pi / 360)
v = (j - 1) / 26
add_vertex([b1, b2, b3], color[-3:], [0, 0], normal[-3:])
b01, b02, b03 = b1, b2, b3
for i in range(1, tr):
x1 = a * math.cos(i * 2 * math.pi / tr)
y1 = b * math.sin(i * 2 * math.pi / tr)
t1, t2, t3 = a1, a2, a3
a1 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
a2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
a3 = z + j * 8
v = math.cos(t * math.pi / 360) + math.sin(t * math.pi / 360)
add_vertex([a1, a2, a3],
[math.cos(i * 2 * math.pi / tr)*0.5 + 0.5, j/27, math.sin(i * 2 * math.pi / tr)*0.5 + 0.5],
[(i - 1) / tr, (26 - j) / 26],
[(b2 - t2) * (a3 - b3) - (b3 - t3) * (a2 - b2), (b3 - t3) * (a1 - b1) - (b1 - t1) * (a3 - b3),
(b1 - t1) * (a2 - b2) - (b2 - t2) * (a1 - b1)])
x1 = a * math.cos(i * 2 * math.pi / tr)
y1 = b * math.sin(i * 2 * math.pi / tr)
b1 = x1 * math.cos((t + 16) * math.pi / 360) + y1 * math.sin((t + 16) * math.pi / 360)
b2 = - x1 * math.sin((t + 16) * math.pi / 360) + y1 * math.cos((t + 16) * math.pi / 360)
b3 = z + j * 8 + 8
v = math.cos((t + 16) * math.pi / 360) + math.sin((t + 16) * math.pi / 360)
add_vertex([b1, b2, b3], color[-3:], [(i - 1) / tr, (25 - j) / 26], normal[-3:])
add_vertex([a01, a02, a03], color[-3:], textcoord[-2:], normal[-3:])
add_vertex([b01, b02, b03], color[-3:], textcoord[-2:], normal[-3:])
t = t + 16
repeat_vertex()
t = 416
z = z + 144
angle = angle + t
for i in range(0, tr + 1):
u, v = math.cos(i * 2 * math.pi / tr), math.sin(i * 2 * math.pi / tr)
x1, y1 = x + a * u, y + b * v
x2 = x1 * math.cos(t * math.pi / 360) + y1 * math.sin(t * math.pi / 360)
y2 = - x1 * math.sin(t * math.pi / 360) + y1 * math.cos(t * math.pi / 360)
if i >= 1:
t1, t2, t3 = a1, a2, a3
a1, a2, a3 = x2, y2, z + 72
add_vertex([a1, a2, a3],
[u * 0.5 + 0.5, 1, v * 0.5 + 0.5],
[u * 0.5 + 0.5, v * 0.5 + 0.5],
[0, 0, 1])
if i == 0:
repeat_vertex()
b1, b2, b3 = x, y, z +72
add_vertex([b1, b2, b3], [0.5, 1, 0.5], [0.5, 0.5], normal[-3:])
angle = angle + i
repeat_vertex()
Now the shape can be either drawn by glBegin/glEnd sequnces or glDrawArrays:
draw_array = True # False
def draw_figure():
# [...]
if not draw_array:
pgl.glBegin(pgl.GL_TRIANGLE_STRIP)
for i in range(len(vertex)//3):
if Texture:
pgl.glTexCoord2f(textcoord[i*2], textcoord[i*2+1])
else:
pgl.glColor3f(color[i*3], color[i*3+1], color[i*3+2])
pgl.glNormal3f(normal[i*3], normal[i*3+1], normal[i*3+2])
pgl.glVertex3f(vertex[i*3], vertex[i*3+1], vertex[i*3+2])
pgl.glEnd()
else:
pgl.glEnableClientState(pgl.GL_VERTEX_ARRAY)
data = (pgl.GLfloat * len(vertex))(*vertex)
pgl.glVertexPointer(3, pgl.GL_FLOAT, 0, data)
pgl.glDrawArrays(pgl.GL_TRIANGLE_STRIP, 0, len(vertex) // 3)
pgl.glDisableClientState(pgl.GL_VERTEX_ARRAY)
The shape can be constructed at the begin of the application:
construct_figure()
pyglet.clock.schedule_interval(run_loop, 0.1)
pyglet.app.run()

Solving an ODE System in Python with Runge Kutta 4th Order , unexpected ERROR

I have a problem while solving a physical Problem in Python.
It is an ODE System. ODE System
At certain time points some dosis has to be injected to raise the value A.
I have to solve this with the Runge Kutta Method of 4th Order.
I thought i got it but an Calculation Error apperes every time.
RuntimeWarning: overflow encountered
After a short calc time the values get up to infinity....
Another problem is that in the 3th loop Tr gets a neg value what physicaly cant be.
Here's my code:
a = 10.
v = 8.
x = 0.8
p = 1.02
u1 = 0.2
u2 = 0.1
ur = 0.25
c = 0.0001
N_0 = a
T1_0 = 0.001
T2_0 = 0.0001
Tr_0 = 0.001
A_0 = 0.
t = 0.
h = 1.
while (t <= int(np.amax(time))):
for i in range(len(time)):
if t == time[i]:
A_0 = A_0 + dosis[i]
print('INITAL VALUE: ',' t =', t,' A =', A_0,' N =', N_0,' T1 =', T1_0,' T2 =', T2_0,' Tr =', Tr_0)
a1 = -(A_0) * (T1_0 + T2_0 + Tr_0)
b1 = -(N_0) + a - (N_0 * A_0 * ((T1_0 / (1. + u2 * T2_0)) + c))-(p * N_0 * A_0 * (T2_0 + c)) - (x * N_0 * A_0 *(Tr_0 + c))
c1 = -(T1_0) + ((v * N_0 * A_0) / (1. + ur * Tr_0)) * ((T1_0 / (1. + u2 * T2_0)) + c)
d1 = -(T2_0) + (p * ((v * N_0 * A_0) / (1. + ur * Tr_0)) * ((T2_0 + c) / (1. + u1 * (T1_0 / (1. + u2 * T2_0)))))
e1 = -(Tr_0) + (x * v * N_0 * A_0 * (Tr_0 + c))
A_1 = A_0 + (h / 2.) * a1
N_1 = N_0 + (h / 2.) * b1
T1_1 = T1_0 + (h / 2.) * c1
T2_1 = T2_0 + (h / 2.) * d1
Tr_1 = Tr_0 + (h / 2.) * e1
a2 = -(A_1) * (T1_1 + T2_1 + Tr_1)
b2 = -(N_1) + a - (N_1 * A_1 * ((T1_1 / (1. + u2 * T2_1)) + c))-(p * N_1 * A_1 * (T2_1 + c)) - (x * N_1 * A_1 *(Tr_1 + c))
c2 = -(T1_1) + ((v * N_1 * A_1) / (1. + ur * Tr_1)) * ((T1_1 / (1. + u2 * T2_1)) + c)
d2 = -(T2_1) + (p * ((v * N_1 * A_1) / (1. + ur * Tr_1)) * ((T2_1 + c) / (1. + u1 * (T1_1 / (1. + u2 * T2_1)))))
e2 = -(Tr_1) + (x * v * N_1 * A_1 * (Tr_1 + c))
A_2 = A_0 + (h / 2.) * a2
N_2 = N_0 + (h / 2.) * b2
T1_2 = T1_0 + (h / 2.) * c2
T2_2 = T2_0 + (h / 2.) * d2
Tr_2 = Tr_0 + (h / 2.) * e2
a3 = -(A_2) * (T1_2 + T2_2 + Tr_2)
b3 = -(N_2) + a - (N_2 * A_2 * ((T1_2 / (1. + u2 * T2_2)) + c))-(p * N_2 * A_2 * (T2_2 + c)) - (x * N_2 * A_2 *(Tr_2 + c))
c3 = -(T1_2) + ((v * N_2 * A_2) / (1. + ur * Tr_2)) * ((T1_2 / (1. + u2 * T2_2)) + c)
d3 = -(T2_2) + (p * ((v * N_2 * A_2) / (1. + ur * Tr_2)) * ((T2_2 + c) / (1. + u1 * (T1_2 / (1. + u2 * T2_2)))))
e3 = -(Tr_2) + (x * v * N_2 * A_2 * (Tr_2 + c))
A_3 = A_0 + h * a3
N_3 = N_0 + h * b3
T1_3 = T1_0 + h * c3
T2_3 = T2_0 + h * d3
Tr_3 = Tr_0 + h * e3
a4 = -(A_3) * (T1_3 + T2_3 + Tr_3)
b4 = -(N_3) + a - (N_3 * A_3 * ((T1_3 / (1. + u2 * T2_3)) + c))-(p * N_3 * A_3 * (T2_3 + c)) - (x * N_3 * A_3 *(Tr_3 + c))
c4 = -(T1_3) + ((v * N_3 * A_3) / (1. + ur * Tr_3)) * ((T1_3 / (1. + u2 * T2_3)) + c)
d4 = -(T2_3) + (p * ((v * N_3 * A_3) / (1. + ur * Tr_3)) * ((T2_3 + c) / (1. + u1 * (T1_3 / (1. + u2 * T2_3)))))
e4 = -(Tr_3) + (x * v * N_3 * A_3 * (Tr_3 + c))
t = t + h
A_0 = A_0 + (h / 6.) * (a1 + (2. * a2) + (2. * a3) + a4)
N_0 = N_0 + (h / 6.) * (b1 + (2. * b2) + (2. * b3) + b4)
T1_0 = T1_0 + (h / 6.) * (c1 + (2. * c2) + (2. * c3) + c4)
T2_0 = T2_0 + (h / 6.) * (d1 + (2. * d2) + (2. * d3) + d4)
Tr_0 = Tr_0 + (h / 6.) * (e1 + (2. * e2) + (2. * e3) + e4)
And here is the current output:
INITAL VALUE: t = 0.0 A = 0.0 N = 10.0 T1 = 0.001 T2 = 0.0001 Tr = 0.001
INITAL VALUE: t = 1.0 A = 0.1 N = 10.0 T1 = 0.0003750000000000001 T2 = 3.7500000000000003e-05 Tr = 0.0003750000000000001
INITAL VALUE: t = 2.0 A = 0.2975779058326524 N = 9.9799116753568 T1 = 0.09021366809218588 T2 = 0.029730294675718548 Tr = 0.04000932816912045
INITAL VALUE: t = 3.0 A = 0.20021726558830752 N = 9.428758582263455 T1 = 4.6119100465532386 T2 = 3.8633993244370335 Tr = -7.920033484370073
INITAL VALUE: t = 4.0 A = 40682939.2164535 N = 737615666179.137 T1 = -32.736202523709856 T2 = 265493.88168389443 Tr = -5887693774039.941
INITAL VALUE: t = 5.0 A = inf N = nan T1 = -inf T2 = inf Tr = -inf
INITAL VALUE: t = 6.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
INITAL VALUE: t = 7.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
INITAL VALUE: t = 8.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
INITAL VALUE: t = 9.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
INITAL VALUE: t = 10.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
I hope anyone has a idea what could help me
Thanks for your Time!!!
I could fix the Problem finally.
But I have to put the Step size to h=0.001 so that it doesnt become an error.
This takes the programm to 3 hours of computing...
can I pimp the while loop to make it a bit faster?

IndexError: index 666 is out of bounds for axis 1 with size 501

import math
import numpy as np
def ExplicitMethod(S0, K, r, q, T, Sigma, M, N, Option):
M = int(M)
N = int(N)
dt = T / N
K = float(K)
Smax = 2 * K
dS = Smax / N
FGrid = np.zeros(shape=(N+1, M+1))
if Option == 'Call':
FGrid[-1, :] = np.maximum(np.arange(0, M+1) * dS - K, 0)
elif Option == 'Put':
FGrid[-1, :] = np.maximum(K - np.arange(0, M+1) * dS, 0)
A = np.zeros(shape=(M+1, M+1))
A[0,0] = 1
A[-1,-1] = 1
for j in range(1, M-1):
A[j, (j-1,j, j+1)] = \
[0.5 * dt * (Sigma ** 2 * j ** 2 - (r - q) * j ), \
1 - dt * (Sigma ** 2 * j ** 2 + r), \
0.5 * dt * (Sigma **2 * j ** 2 + (r - q) * j)]
for i in range(N):
Fhat = FGrid [i, ]
FGrid[i, :] = np.dot(A , Fhat)
Fhat[0] = 0
Fhat[-1] = Smax - K * math.exp(-r * (N - i) * dt)
k = math.floor(S0 / dS)
V = FGrid[0, k] + (FGrid[0, k] - FGrid[0, k]) / dS * [(S0 - k * dS)]
print (V)
ExplicitMethod (50, 30, 0.1, 0.05, 2, 0.3, 400, 800, Option = 'Call')
And I got this error:
IndexError Traceback (most recent call
last) in ()
37 print (V)
38
---> 39 ExplicitMethod (50, 30, 0.1, 0.05, 2, 0.3, 400, 800, Option = 'Call')
in ExplicitMethod(S0, K, r, q, T,
Sigma, M, N, Option)
33 k = math.floor(S0 / dS)
34
---> 35 V = FGrid[0, k] + (FGrid[0, k] - FGrid[0, k]) / dS * [(S0 - k * dS)]
36
37 print (V)
IndexError: index 666 is out of bounds for axis 1 with size 401
And FYI I'm coding Option Pricing Explicit Method:
Please help, thank you!
You have initialized FGrid to be (801,401). The tells us that, for some reason, k=666
k = math.floor(S0 / dS)
V = FGrid[0, k] + (FGrid[0, k] - FGrid[0, k]) / dS * [(S0 - k * dS)]
You need to refine how k is set. Maybe the math is wrong. At the very least you need to ensure that it does not get above 400.
In [200]: S0=50; K=30; N=800
In [201]: dS = 2*K/N
In [202]: dS
Out[202]: 0.075
In [203]: S0/dS
Out[203]: 666.6666666666667
There's your 666.

Plotting Two Solutions with the Same Drive Strength on One Graph

I am trying to plot two solutions on the same graph while only changing the initial condition of these solutions. I want the initial conditions to be one value of y = 0 and another value of y = -pi/2. I am not sure how I get them to be on the same graph without copying and pasting everything over again and creating a new array.
import numpy as np
import matplotlib.pyplot as plt
t = 0.0
y = 0.0
u = 0.0
F = 1.073
Wd = 2*np.pi
w0 = 1.5*Wd
b = w0/4
ts =[]
ys =[]
h= 0.001
while (t <= 30):
m1 = u
k1 = (-w0**2)*np.sin(y) + u*(-2*b) + F*(w0**2)*np.cos(Wd*t)
m2 = u + (h / 2.) * k1
t_2 = t + (h / 2.)
y_2 = y +(h / 2.) * m1
u_2 = m2
k2 = (-w0**2)*np.sin(y_2) + u_2*(-2*b) + F*(w0**2)*np.cos(Wd*t_2)
m3 = u + (h / 2.) * k2
t_3 = t + (h / 2.)
y_3 = y + (h / 2.) * m2
u_3 = m3
k3 = (-w0**2)*np.sin(y_3) + u_3*(-2*b) + F*(w0**2)*np.cos(Wd*t_3)
m4 = u + h * k3
t_4 = t + h
y_4 = y + h * m3
u_4 = m4
k4 = (-w0**2)*np.sin(y_4) + u_4*(-2*b) + F*(w0**2)*np.cos(Wd*t_4)
t = t + h
y = y + (h / 6.) * (m1 + (2 * m2) + (2 * m3) + m4)
u = u + (h / 6.) * (k1 + (2 * k2) + (2 * k3) + k4)
ts.append(t)
ys.append(y)
plt.plot(ts,ys)
plt.xlabel('$t$',fontsize=18)
plt.ylabel('$\phi$',fontsize=18)
plt.axhline(y=0, color = 'black')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
def plotit(y):
t = 0.0
#y = 0.0
u = 0.0
F = 1.073
Wd = 2*np.pi
w0 = 1.5*Wd
b = w0/4
ts =[]
ys =[]
h= 0.001
while (t <= 30):
m1 = u
k1 = (-w0**2)*np.sin(y) + u*(-2*b) + F*(w0**2)*np.cos(Wd*t)
m2 = u + (h / 2.) * k1
t_2 = t + (h / 2.)
y_2 = y +(h / 2.) * m1
u_2 = m2
k2 = (-w0**2)*np.sin(y_2) + u_2*(-2*b) + F*(w0**2)*np.cos(Wd*t_2)
m3 = u + (h / 2.) * k2
t_3 = t + (h / 2.)
y_3 = y + (h / 2.) * m2
u_3 = m3
k3 = (-w0**2)*np.sin(y_3) + u_3*(-2*b) + F*(w0**2)*np.cos(Wd*t_3)
m4 = u + h * k3
t_4 = t + h
y_4 = y + h * m3
u_4 = m4
k4 = (-w0**2)*np.sin(y_4) + u_4*(-2*b) + F*(w0**2)*np.cos(Wd*t_4)
t = t + h
y = y + (h / 6.) * (m1 + (2 * m2) + (2 * m3) + m4)
u = u + (h / 6.) * (k1 + (2 * k2) + (2 * k3) + k4)
ts.append(t)
ys.append(y)
plt.plot(ts,ys)
plt.xlabel('$t$',fontsize=18)
plt.ylabel('$\phi$',fontsize=18)
plt.axhline(y=0, color = 'black')
plotit(y=0)
plotit(y=-np.pi/2.0)
plt.show()

Categories

Resources