Skip to content

Commit

Permalink
ode: Incomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoZeke committed Oct 31, 2017
1 parent 85633b6 commit 15cf0a1
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 120 deletions.
45 changes: 45 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self):
self.btnQuit.clicked.connect(qApp.quit)
self.btnCalcRoot.clicked.connect(self.calcRootMenu)
self.btnCalcLAS.clicked.connect(self.calcLASMenu)
self.btnCalcODE.clicked.connect(self.calcODEMenu)


def calcRootMenu(self):
Expand Down Expand Up @@ -185,6 +186,50 @@ def calcLASGS(self):
self.outTextLA.append("\nThe X vector is \n" + str(x.reshape((-1,1))))
self.outTextLA.append("\nObtained in \n" + str(itr) + " iterations.")

def calcODEMenu(self):
try:
if self.inpEu.isChecked() == true:
self.calcODERK1()
if self.inpRK2.isChecked() == true:
self.calcODERK2()
if self.inpRK3.isChecked() == true:
self.calcODERK3()
if self.inpRK4.isChecked() == true:
self.calcODERK4()
if self.inpMP.isChecked() == true:
self.calcODEMP()
if self.btnGrpRF.checkedId() == -1:
QMessageBox.warning(self, "User Warning","Choose a method.")
except Exception as e:
raise
else:
pass
finally:
pass

def calcODERK1(self):
# Needs to be modularized
f = self.funcInpODE.text()
if not f:
QMessageBox.warning(self, "User Warning","Enter an equation.")
else:
startX = self.startX.value()
startY = self.startY.value()
endX = self.endX.value()
if self.stepSize.text():
stepSize = float(self.stepSize.text())
MRKOde.rk1(f,startX,startY,endX,h=stepSize)
else:
self.outTextRoot.append("<b> Euler's Method </b><br> \
For the function " + self.funcInpRoot.text())
MRKOde.rk1(f,startX,startY,endX)
self.outTextRoot.append("The root is approximately " + repr(x))
if niter < 100:
self.outTextRoot.append("After " + str(niter) + " iterations.")
else:
self.outTextRoot.append("After the maximum allowed iterations.")
self.outTextRoot.append("At the approximate root, the function is " \
+ repr(xval))

if __name__ == "__main__":
app = QApplication(sys.argv)
Expand Down
44 changes: 44 additions & 0 deletions modules/MRKOde.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-


import math
from numpy import sign
from sympy import *
from PyQt5.QtWidgets import (QApplication, QWidget, qApp, QDesktopWidget,
QMessageBox, QPushButton, QToolTip, QMainWindow)

def rk1(f,startX,startY,endX,h=0.025):
# Variable Definition
sym_x = Symbol('x')
sym_y = Symbol('y')

# Conversion attempt
try:
fxy = S(f)
except:
sys.exit('Unable to convert function to symbolic expression.')

# Differentiation attempt
try:
dfdx = diff(fxy, sym_x)
except:
print('Unable to differntiate.')

itr = 0
x = startX
y = startY
while startX < endX:
itr += itr
h = min(h,endX-startX)
x = x + h
if itr == 1:
k1 = h * dfdx.evalf(subs={sym_x: startX, sym_y: startY})
else:
k1 = h * dfdx.evalf(subs={sym_x: x, sym_y: y})
y = y + k1
self.outTextODE.append("\n Iteration" + str(itr) + " : ")
self.outTextODE.append("\nx is " + str(x) + " : ")
self.outTextODE.append("\nk1 is " + str(k1) + " : ")
self.outTextODE.append("\ny is " + str(y) + " : ")

Loading

0 comments on commit 15cf0a1

Please sign in to comment.