salastro / math-expert

Incomplete easy-to-use math solver and PDF generator.
MIT License
22 stars 5 forks source link

Differential Equations #5

Open salastro opened 2 years ago

salastro commented 2 years ago

Probably won't do it, the code does not work.

diff --git a/doc.py b/doc.py
index daa7e76..374dc0d 100644
--- a/doc.py
+++ b/doc.py
@@ -1,5 +1,6 @@
+from __future__ import division
 from sympy import cos, exp, integrate, sqrt, diff, limit, Limit, oo, \
-    simplify, factor, trigsimp
+    simplify, factor, trigsimp, Derivative, dsolve, Function, symbols, Eq
 # from sympy.integrals.manualintegrate import manualintegrate, integral_steps
 from sympy.integrals.risch import NonElementaryIntegral
 from sympy.abc import x
@@ -55,6 +56,16 @@ class MathDoc(Document):
             agn.append(r'=')
             agn.append(latex(solution))

+    def ODE(self, equation):
+        solution = dsolve(equation, y)
+        print(equation)
+        print(solution)
+        with self.create(Alignat(numbering=True, escape=False)) as agn:
+            agn.append(latex(equation))
+            agn.append(r'=0')
+            agn.append(r'\Rightarrow')
+            agn.append(latex(solution))
+
     def Lim(self, equation, a=0):
         solution = limit(equation, x, a)
         with self.create(Alignat(numbering=True, escape=False)) as agn:
@@ -90,6 +101,11 @@ if __name__ == '__main__':
     doc = MathDoc()
     file_name = 'full'

+    y = Function("y")(x)
+    y_ = Derivative(y, x)
+    y__ = Derivative(y_, x)
+    y___ = Derivative(y__, x)
+
     doc.Heading(title='Integral Homework', author='SalahDin Rezk')
     doc.Inte(x/sqrt(1-3*x))
     doc.Inte(1/sqrt(1-3*x))
@@ -98,5 +114,7 @@ if __name__ == '__main__':
     doc.Diff(x**x, 2)
     doc.Diff(x**(1/x), 2)
     doc.Lim(1/x, oo)
+    from sympy import sin
+    doc.ODE(2*y_-y-4*sin(3*x))

     doc.generate_pdf(file_name, clean_tex=True)
diff --git a/main.py b/main.py
index 318f91c..b412c24 100644
--- a/main.py
+++ b/main.py
@@ -1,13 +1,10 @@
+from __future__ import division
 from doc import MathDoc
-from sympy import sin, cos, tan, exp, log, sqrt, sympify, pi, oo, \
-    symbols, asin, acos, atan  # common math
-from sympy import Function
+from sympy import sin, cos, tan, exp, log, ln, sqrt, sympify, pi, oo, \
+    symbols, asin, acos, atan, cot, csc, sec  # common math
+from sympy import Function, Derivative, Eq, dsolve
 from gui import QtWidgets, Ui_MainWindow  # gui

-# common symbols
-x, y, z, t = symbols('x y z t')
-f, g, h = symbols('f g h', cls=Function)
-

 if __name__ == "__main__":
     # Setup the ui.
@@ -17,6 +14,14 @@ if __name__ == "__main__":
     ui = Ui_MainWindow()  # Create the ui.
     ui.setupUi(MainWindow)  # Setup the ui.

+    # common symbols
+    from sympy.abc import x
+    y = Function("y")
+    y_ = Derivative(y, x, x)
+    y__ = Derivative(y_, x, x)
+    y___ = Derivative(y__, x, x)
+    # ! ValueError: -y + y_ is not a solvable differential equation in y(x)
+
     # Setup the document and buttons.
     math_doc = MathDoc()  # The document.
     # math_doc.GenPdf(ui.fileTxt.toPlainText(), ui.titleTxt.toPlainText(),
@@ -28,6 +33,8 @@ if __name__ == "__main__":
     ui.limBt.clicked.connect(lambda: math_doc.Lim(
         sympify(ui.expTxt.toPlainText().split(',')[0]),
         sympify(ui.expTxt.toPlainText().split(',')[1])))  # Limit button.
+    ui.odeBt.clicked.connect(lambda: math_doc.ODE(
+        sympify(ui.expTxt.toPlainText()), y, y__, y___, y___))  # ODE button.
     ui.simpBt.clicked.connect(lambda: math_doc.Simp(
         sympify(ui.expTxt.toPlainText())))  # simplfiy button
     ui.factBt.clicked.connect(lambda: math_doc.Fact(
@@ -35,10 +42,10 @@ if __name__ == "__main__":
     ui.plotBt.clicked.connect(lambda: math_doc.Plot(
         (ui.expTxt.toPlainText())))  # The plot button.
     ui.genPdfBt.clicked.connect(lambda: math_doc.GenPdf(ui.fileTxt.toPlainText(
-    ), ui.titleTxt.toPlainText(), ui.authorTxt.toPlainText(), clean_tex=True))
+        ), ui.titleTxt.toPlainText(), ui.authorTxt.toPlainText(), clean_tex=True))
     # The generate pdf button.
     ui.genLatexBt.clicked.connect(lambda: math_doc.GenTex(ui.fileTxt.toPlainText(
-    ), ui.titleTxt.toPlainText(), ui.authorTxt.toPlainText()))
+        ), ui.titleTxt.toPlainText(), ui.authorTxt.toPlainText()))
     # The generate tex button.

     MainWindow.show()