├── .gitattributes ├── target ├── maven-archiver │ └── pom.properties ├── maven-status │ └── maven-compiler-plugin │ │ ├── testCompile │ │ └── default-testCompile │ │ │ ├── createdFiles.lst │ │ │ └── inputFiles.lst │ │ └── compile │ │ └── default-compile │ │ ├── createdFiles.lst │ │ └── inputFiles.lst ├── RK4Logic-1.0-SNAPSHOT.jar ├── classes │ ├── com │ │ └── example │ │ │ └── App.class │ └── TNTProject │ │ └── RK4Logic.class ├── test-classes │ └── com │ │ └── example │ │ └── AppTest.class └── surefire-reports │ ├── 2024-12-14T15-04-15_165.dumpstream │ ├── com.example.AppTest.txt │ └── TEST-com.example.AppTest.xml ├── .vscode └── settings.json ├── src ├── test │ └── java │ │ └── com │ │ └── example │ │ ├── RK4-ODEsolverX_Summary.docx │ │ ├── RK4-ODEsolverX_Summary.pdf │ │ └── AppTest.java └── main │ └── java │ ├── com │ └── example │ │ └── App.java │ └── TNTProject │ ├── EulerLogic.java │ ├── AdamsMoultonLogic.java │ ├── AdamsMoultonDetailed.java │ └── RK4Logic.java ├── LICENSE └── pom.xml /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | artifactId=RK4Logic 2 | groupId=com.example 3 | version=1.0-SNAPSHOT 4 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | com\example\AppTest.class 2 | -------------------------------------------------------------------------------- /target/RK4Logic-1.0-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ArjiJethin/RK4-ODEsolverX/HEAD/target/RK4Logic-1.0-SNAPSHOT.jar -------------------------------------------------------------------------------- /target/classes/com/example/App.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ArjiJethin/RK4-ODEsolverX/HEAD/target/classes/com/example/App.class -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | com\example\App.class 2 | TNTProject\RK4Logic.class 3 | -------------------------------------------------------------------------------- /target/classes/TNTProject/RK4Logic.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ArjiJethin/RK4-ODEsolverX/HEAD/target/classes/TNTProject/RK4Logic.class -------------------------------------------------------------------------------- /target/test-classes/com/example/AppTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ArjiJethin/RK4-ODEsolverX/HEAD/target/test-classes/com/example/AppTest.class -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.configuration.updateBuildConfiguration": "interactive", 3 | "java.compile.nullAnalysis.mode": "automatic" 4 | } 5 | -------------------------------------------------------------------------------- /src/test/java/com/example/RK4-ODEsolverX_Summary.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ArjiJethin/RK4-ODEsolverX/HEAD/src/test/java/com/example/RK4-ODEsolverX_Summary.docx -------------------------------------------------------------------------------- /src/test/java/com/example/RK4-ODEsolverX_Summary.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ArjiJethin/RK4-ODEsolverX/HEAD/src/test/java/com/example/RK4-ODEsolverX_Summary.pdf -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | D:\Visual Studio Codes\Projects\TNTProject\src\test\java\com\example\AppTest.java 2 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | D:\Visual Studio Codes\Projects\TNTProject\src\main\java\com\example\App.java 2 | D:\Visual Studio Codes\Projects\TNTProject\src\main\java\TNTProject\RK4Logic.java 3 | -------------------------------------------------------------------------------- /src/main/java/com/example/App.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /target/surefire-reports/2024-12-14T15-04-15_165.dumpstream: -------------------------------------------------------------------------------- 1 | # Created at 2024-12-14T15:04:15.314 2 | Boot Manifest-JAR contains absolute paths in classpath 'D:\Visual Studio Codes\Projects\TNTProject\target\test-classes' 3 | Hint: -Djdk.net.URLClassPath.disableClassPathURLCheck=true 4 | 'other' has different root 5 | 6 | -------------------------------------------------------------------------------- /target/surefire-reports/com.example.AppTest.txt: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | Test set: com.example.AppTest 3 | ------------------------------------------------------------------------------- 4 | Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 s -- in com.example.AppTest 5 | -------------------------------------------------------------------------------- /src/test/java/com/example/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Eswar Arji 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | com.example 7 | RK4Logic 8 | 1.0-SNAPSHOT 9 | jar 10 | 11 | 12 | 13 | 14 | net.objecthunter 15 | exp4j 16 | 0.4.8 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | 3.8.1 24 | test 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.codehaus.mojo 32 | exec-maven-plugin 33 | 3.5.0 34 | 35 | 36 | 37 | java 38 | 39 | 40 | TNTProject.RK4Logic 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/main/java/TNTProject/EulerLogic.java: -------------------------------------------------------------------------------- 1 | package TNTProject; 2 | 3 | import java.util.Scanner; 4 | import net.objecthunter.exp4j.Expression; 5 | import net.objecthunter.exp4j.ExpressionBuilder; 6 | 7 | public class EulerLogic { 8 | 9 | // ! Function defining the ODE: dy/dx = f(x, y) 10 | static double f(double x, double y, Expression expression) { 11 | return expression.setVariable("x", x).setVariable("y", y).evaluate(); 12 | } 13 | 14 | // ! Euler's Method implementation 15 | static double eulerMethod(double x0, double y0, double xEnd, double h, Expression expression) { 16 | int n = (int) ((xEnd - x0) / h); 17 | double x = x0; 18 | double y = y0; 19 | 20 | // ! Loop through each step, updating y using Euler's formula 21 | for (int i = 0; i < n; i++) { 22 | y = y + h * f(x, y, expression); 23 | x = x + h; 24 | } 25 | 26 | // ? Return the final value of y at x = xEnd 27 | return y; 28 | } 29 | 30 | // ! Main method: Entry point for the program 31 | public static void main(String[] args) { 32 | Scanner scanner = new Scanner(System.in); 33 | 34 | // * Get user input for the ODE, initial values, and step size 35 | System.out.print("Enter the ODE (e.g., '1 + y^2' or 'x + y'): "); 36 | String function = scanner.nextLine(); 37 | 38 | System.out.print("Enter initial x value (x0): "); 39 | double x0 = scanner.nextDouble(); 40 | 41 | System.out.print("Enter initial y value (y0): "); 42 | double y0 = scanner.nextDouble(); 43 | 44 | System.out.print("Enter endpoint x value (xEnd): "); 45 | double xEnd = scanner.nextDouble(); 46 | 47 | System.out.print("Enter step size (h): "); 48 | double h = scanner.nextDouble(); 49 | 50 | // ! Parse the ODE using Exp4j 51 | try { 52 | Expression expression = new ExpressionBuilder(function) 53 | .variables("x", "y") 54 | .build(); 55 | 56 | // ! Solve the ODE using Euler's Method 57 | double result = eulerMethod(x0, y0, xEnd, h, expression); 58 | 59 | // ! Output the result 60 | System.out.printf("The value of y at x = %.2f is %.5f%n", xEnd, result); 61 | } catch (IllegalArgumentException e) { 62 | System.out.println("Error: Invalid expression - " + e.getMessage()); 63 | } 64 | 65 | scanner.close(); 66 | } 67 | } -------------------------------------------------------------------------------- /src/main/java/TNTProject/AdamsMoultonLogic.java: -------------------------------------------------------------------------------- 1 | package TNTProject; 2 | 3 | import java.util.Scanner; 4 | import net.objecthunter.exp4j.Expression; 5 | import net.objecthunter.exp4j.ExpressionBuilder; 6 | 7 | public class AdamsMoultonLogic { 8 | 9 | static double f(double x, double y, Expression expression) { 10 | return expression.setVariable("x", x).setVariable("y", y).evaluate(); 11 | } 12 | 13 | static double rungeKuttaStep(double x, double y, double h, Expression expression) { 14 | double k1 = h * f(x, y, expression); 15 | double k2 = h * f(x + h / 2, y + k1 / 2, expression); 16 | double k3 = h * f(x + h / 2, y + k2 / 2, expression); 17 | double k4 = h * f(x + h, y + k3, expression); 18 | return y + (k1 + 2 * k2 + 2 * k3 + k4) / 6; 19 | } 20 | 21 | static double adamsMoulton(double x0, double y0, double xEnd, double h, Expression expression) { 22 | int n = (int) ((xEnd - x0) / h); 23 | double[] x = new double[n + 1]; 24 | double[] y = new double[n + 1]; 25 | double[] f = new double[n + 1]; 26 | 27 | x[0] = x0; 28 | y[0] = y0; 29 | f[0] = f(x[0], y[0], expression); 30 | 31 | for (int i = 1; i < 4; i++) { 32 | y[i] = rungeKuttaStep(x[i - 1], y[i - 1], h, expression); 33 | x[i] = x[i - 1] + h; 34 | f[i] = f(x[i], y[i], expression); 35 | } 36 | 37 | for (int i = 3; i < n; i++) { 38 | double yPredict = y[i] + h / 24 * (55 * f[i] - 59 * f[i - 1] + 37 * f[i - 2] - 9 * f[i - 3]); 39 | double fPredict = f(x[i + 1], yPredict, expression); 40 | y[i + 1] = y[i] + h / 24 * (9 * fPredict + 19 * f[i] - 5 * f[i - 1] + f[i - 2]); 41 | 42 | x[i + 1] = x[i] + h; 43 | f[i + 1] = f(x[i + 1], y[i + 1], expression); 44 | } 45 | 46 | return y[n]; 47 | } 48 | 49 | public static void main(String[] args) { 50 | Scanner scanner = new Scanner(System.in); 51 | 52 | System.out.print("Enter the ODE (e.g., '1 + y^2' or 'x + y'): "); 53 | String function = scanner.nextLine(); 54 | 55 | System.out.print("Enter initial x value (x0): "); 56 | double x0 = scanner.nextDouble(); 57 | 58 | System.out.print("Enter initial y value (y0): "); 59 | double y0 = scanner.nextDouble(); 60 | 61 | System.out.print("Enter endpoint x value (xEnd): "); 62 | double xEnd = scanner.nextDouble(); 63 | 64 | System.out.print("Enter step size (h): "); 65 | double h = scanner.nextDouble(); 66 | 67 | try { 68 | Expression expression = new ExpressionBuilder(function) 69 | .variables("x", "y") 70 | .build(); 71 | 72 | double result = adamsMoulton(x0, y0, xEnd, h, expression); 73 | System.out.printf("The value of y at x = %.2f is %.5f%n", xEnd, result); 74 | } catch (IllegalArgumentException e) { 75 | System.out.println("Error: Invalid expression - " + e.getMessage()); 76 | } 77 | 78 | scanner.close(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/TNTProject/AdamsMoultonDetailed.java: -------------------------------------------------------------------------------- 1 | package TNTProject; 2 | 3 | import java.util.Scanner; 4 | import net.objecthunter.exp4j.Expression; 5 | import net.objecthunter.exp4j.ExpressionBuilder; 6 | 7 | public class AdamsMoultonDetailed { 8 | 9 | static double f(double x, double y, Expression expression) { 10 | return expression.setVariable("x", x).setVariable("y", y).evaluate(); 11 | } 12 | 13 | static double rungeKuttaStep(double x, double y, double h, Expression expression) { 14 | double k1 = h * f(x, y, expression); 15 | double k2 = h * f(x + h / 2, y + k1 / 2, expression); 16 | double k3 = h * f(x + h / 2, y + k2 / 2, expression); 17 | double k4 = h * f(x + h, y + k3, expression); 18 | return y + (k1 + 2 * k2 + 2 * k3 + k4) / 6; 19 | } 20 | 21 | static void adamsMoulton(double x0, double y0, double xEnd, double h, Expression expression) { 22 | int n = (int) ((xEnd - x0) / h); 23 | double[] x = new double[n + 1]; 24 | double[] y = new double[n + 1]; 25 | double[] f = new double[n + 1]; 26 | 27 | x[0] = x0; 28 | y[0] = y0; 29 | f[0] = f(x[0], y[0], expression); 30 | 31 | System.out.println("n\tx_n\tStarting y_n\tPredicted y*_n\tCorrected y_n"); 32 | System.out.printf("%d\t%.1f\t%.6f\t-\t-\n", 0, x[0], y[0]); 33 | 34 | for (int i = 1; i < 4; i++) { 35 | y[i] = rungeKuttaStep(x[i - 1], y[i - 1], h, expression); 36 | x[i] = x[i - 1] + h; 37 | f[i] = f(x[i], y[i], expression); 38 | System.out.printf("%d\t%.1f\t%.6f\t-\t-\n", i, x[i], y[i]); 39 | } 40 | 41 | for (int i = 3; i < n; i++) { 42 | double yPredict = y[i] + h / 24 * (55 * f[i] - 59 * f[i - 1] + 37 * f[i - 2] - 9 * f[i - 3]); 43 | double fPredict = f(x[i + 1], yPredict, expression); 44 | double yCorrected = y[i] + h / 24 * (9 * fPredict + 19 * f[i] - 5 * f[i - 1] + f[i - 2]); 45 | 46 | x[i + 1] = x[i] + h; 47 | f[i + 1] = f(x[i + 1], yCorrected, expression); 48 | y[i + 1] = yCorrected; 49 | 50 | System.out.printf("%d\t%.1f\t%.6f\t%.6f\t%.6f\n", i + 1, x[i + 1], y[i], yPredict, yCorrected); 51 | } 52 | } 53 | 54 | public static void main(String[] args) { 55 | Scanner scanner = new Scanner(System.in); 56 | 57 | System.out.print("Enter the ODE (e.g., '1 + y^2' or 'x + y'): "); 58 | String function = scanner.nextLine(); 59 | 60 | System.out.print("Enter initial x value (x0): "); 61 | double x0 = scanner.nextDouble(); 62 | 63 | System.out.print("Enter initial y value (y0): "); 64 | double y0 = scanner.nextDouble(); 65 | 66 | System.out.print("Enter endpoint x value (xEnd): "); 67 | double xEnd = scanner.nextDouble(); 68 | 69 | System.out.print("Enter step size (h): "); 70 | double h = scanner.nextDouble(); 71 | 72 | try { 73 | Expression expression = new ExpressionBuilder(function) 74 | .variables("x", "y") 75 | .build(); 76 | 77 | adamsMoulton(x0, y0, xEnd, h, expression); 78 | } catch (IllegalArgumentException e) { 79 | System.out.println("Error: Invalid expression - " + e.getMessage()); 80 | } 81 | 82 | scanner.close(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/TNTProject/RK4Logic.java: -------------------------------------------------------------------------------- 1 | package TNTProject; 2 | 3 | import java.util.Scanner; 4 | import net.objecthunter.exp4j.Expression; 5 | import net.objecthunter.exp4j.ExpressionBuilder; 6 | 7 | public class RK4Logic { 8 | 9 | // ! Function defining the ODE: dy/dx = f(x, y) 10 | // * This method takes the current values of x and y, along with the parsed 11 | // * expression representing the ODE, 12 | // * and evaluates the expression at those values of x and y. 13 | 14 | static double f(double x, double y, Expression expression) { 15 | // * Set the variables 'x' and 'y' in the expression, then evaluate the 16 | // * expression 17 | return expression.setVariable("x", x).setVariable("y", y).evaluate(); 18 | } 19 | 20 | // ! Runge-Kutta 4th order method implementation 21 | // * This method solves the ODE using the RK4 method by iterating through the 22 | // * steps and computing the intermediate 23 | // * values (k1, k2, k3, k4) and updating the value of y accordingly. 24 | static double rungeKutta(double x0, double y0, double xEnd, double h, Expression expression) { 25 | // *Calculate the number of steps required based on the range (x0 to xEnd) and 26 | // *step size (h) 27 | int n = (int) ((xEnd - x0) / h); 28 | double x = x0; 29 | double y = y0; 30 | 31 | // ! Loop through each step, calculating k1, k2, k3, k4, and updating y 32 | for (int i = 0; i < n; i++) { 33 | // Compute the intermediate values using the ODE 34 | double k1 = h * f(x, y, expression); 35 | double k2 = h * f(x + h / 2, y + k1 / 2, expression); 36 | double k3 = h * f(x + h / 2, y + k2 / 2, expression); 37 | double k4 = h * f(x + h, y + k3, expression); 38 | 39 | // ! Update y using the weighted average of the intermediate values 40 | y = y + (k1 + 2 * k2 + 2 * k3 + k4) / 6; 41 | 42 | // ! Increment x by the step size 43 | x = x + h; 44 | } 45 | 46 | // ? Return the final value of y at x = xEnd 47 | return y; 48 | } 49 | 50 | // ! Main method: Entry point for the program 51 | public static void main(String[] args) { 52 | // ! Create a Scanner object to get input from the user 53 | Scanner scanner = new Scanner(System.in); 54 | 55 | // *Get user input for the ODE, initial values, and step size 56 | System.out.print("Enter the ODE (e.g., '1 + y^2' or 'x + y'): "); 57 | String function = scanner.nextLine(); // * Read the ODE function from the user input 58 | 59 | System.out.print("Enter initial x value (x0): "); 60 | double x0 = scanner.nextDouble(); // *Read initial x value from the user 61 | 62 | System.out.print("Enter initial y value (y0): "); 63 | double y0 = scanner.nextDouble(); // *Read initial y value from the user 64 | 65 | System.out.print("Enter endpoint x value (xEnd): "); 66 | double xEnd = scanner.nextDouble(); // *Read the endpoint value of x from the user 67 | 68 | System.out.print("Enter step size (h): "); 69 | double h = scanner.nextDouble(); // *Read the step size from the user 70 | 71 | // ! Parse the ODE using Exp4j 72 | try { 73 | // * Use the ExpressionBuilder from exp4j to create an Expression object from 74 | // *the input function 75 | Expression expression = new ExpressionBuilder(function) 76 | .variables("x", "y") // *Define 'x' and 'y' as the variables used in the function 77 | .build(); // *Build the expression 78 | 79 | // !Solve the ODE using Runge-Kutta method 80 | double result = rungeKutta(x0, y0, xEnd, h, expression); 81 | 82 | // ! Output the result 83 | System.out.printf("The value of y at x = %.2f is %.5f%n", xEnd, result); 84 | } catch (IllegalArgumentException e) { 85 | // ! Handle errors if the input expression is invalid 86 | System.out.println("Error: Invalid expression - " + e.getMessage()); 87 | } 88 | 89 | // *Close the scanner object to prevent resource leakage 90 | scanner.close(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /target/surefire-reports/TEST-com.example.AppTest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | --------------------------------------------------------------------------------