From 15cf0a1dd5ca48571db6f757ae2cd6157513d78d Mon Sep 17 00:00:00 2001 From: HaoZeke Date: Tue, 31 Oct 2017 20:11:05 +0530 Subject: [PATCH] ode: Incomplete --- gui.py | 45 +++++++ modules/MRKOde.py | 44 +++++++ testUI.ui | 294 +++++++++++++++++++++++++++------------------- 3 files changed, 263 insertions(+), 120 deletions(-) create mode 100644 modules/MRKOde.py diff --git a/gui.py b/gui.py index 838a4a0..d749413 100644 --- a/gui.py +++ b/gui.py @@ -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): @@ -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(" Euler's Method
\ + 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) diff --git a/modules/MRKOde.py b/modules/MRKOde.py new file mode 100644 index 0000000..ca49660 --- /dev/null +++ b/modules/MRKOde.py @@ -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) + " : ") + diff --git a/testUI.ui b/testUI.ui index e951999..71bb6ff 100644 --- a/testUI.ui +++ b/testUI.ui @@ -6,8 +6,8 @@ 0 0 - 1017 - 662 + 1108 + 700 @@ -19,12 +19,12 @@ 10 10 - 961 - 511 + 971 + 531 - 1 + 2 @@ -40,19 +40,6 @@ - - - - Qt::Vertical - - - - 20 - 40 - - - - @@ -99,21 +86,27 @@ - + true + + <html><head/><body><p>These will be the <span style=" font-weight:600;">approximations</span> to the root for the <span style=" text-decoration: underline;">Secant</span> and <span style=" text-decoration: underline;">Reular Falsi</span> methods.</p></body></html> + Lower Bound - + true + + <html><head/><body><p>These will be the <span style=" font-weight:600;">approximations</span> to the root for the <span style=" text-decoration: underline;">Secant</span> and <span style=" text-decoration: underline;">Reular Falsi</span> methods.</p></body></html> + 10 @@ -125,21 +118,27 @@ - + true + + <html><head/><body><p>These will be the <span style=" font-weight:600;">approximations</span> to the root for the <span style=" text-decoration: underline;">Secant</span> and <span style=" text-decoration: underline;">Reular Falsi</span> methods.</p></body></html> + Upper Bound - + true + + <html><head/><body><p>These will be the <span style=" font-weight:600;">approximations</span> to the root for the <span style=" text-decoration: underline;">Secant</span> and <span style=" text-decoration: underline;">Reular Falsi</span> methods.</p></body></html> + 10 @@ -151,14 +150,14 @@ - + Method - + Bisection @@ -168,7 +167,7 @@ - + Newton Raphson @@ -178,7 +177,7 @@ - + Regula Falsi @@ -188,7 +187,7 @@ - + Secant @@ -198,7 +197,7 @@ - + Qt::Vertical @@ -211,7 +210,7 @@ - + Calculate @@ -257,6 +256,19 @@ + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -331,9 +343,6 @@ - - - @@ -341,13 +350,6 @@ - - - - Calculate - - - @@ -421,6 +423,16 @@ + + + + Calculate + + + + + + @@ -428,40 +440,63 @@ ODE Solvers - + - 20 - 4 - 340 - 411 + 10 + 10 + 931 + 471 - - - + + + - Function + Start x - - - - - + + - Start x + Method - - + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + <html><head/><body><p>Defaults to <span style=" font-weight:600;">0.025</span></p></body></html> + 10 + + + + <html><head/><body><p>Defaults to <span style=" font-weight:600;">0.025</span></p></body></html> + + + Step size + + + @@ -469,10 +504,13 @@ - - - - 10 + + + + + + + Runge Kutta Fourth Order @@ -483,73 +521,101 @@ - - - - 10 + + + + Calculate - - + + - Step size + Function - - - - 10 + + + + Runge Kutta Third Order - - + + - Decimal Places + Euler (Runge Kutta First Order) - - + + + + 10 + + + -99999999999.000000000000000 + + + 99999999999.000000000000000 + + - - + + - Method + Output: + + + Qt::AlignCenter - - + + - Euler + Runge Kutta Second Order - - + + - Improved Euler + Milne's Predictor Corrector - - - - Runge Kutta + + + + 10 + + + -99999999999.000000000000000 + + + 99999999999.000000000000000 - - - - Calculate + + + + + + + 10 + + + -99999999999.000000000000000 + + + 99999999999.000000000000000 - + Input: @@ -559,30 +625,18 @@ - - - - - - 610 - 0 - 331 - 401 - - - - - - - Output: + + + + Qt::Vertical - - Qt::AlignCenter + + + 20 + 40 + - - - - + @@ -591,8 +645,8 @@ - 360 - 540 + 370 + 570 191 32 @@ -605,17 +659,17 @@ + 0 0 - 1017 + 1108 31 -