{"id":15716276,"url":"https://github.com/sudipto3331/solving-ordinary-differential-equations-numerical-method-implementation-in-python","last_synced_at":"2026-05-19T05:43:14.624Z","repository":{"id":254987345,"uuid":"848184229","full_name":"sudipto3331/Solving-Ordinary-Differential-Equations-Numerical-Method-Implementation-in-Python","owner":"sudipto3331","description":"This repository contains a Python implementation for solving ordinary differential equations (ODEs) using various numerical methods, including the Euler method, Heun's method, the Midpoint method, and the Fourth Order Runge-Kutta (RK4) method. The code also provides a graphical representation of the solution.","archived":false,"fork":false,"pushed_at":"2024-08-27T09:42:26.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-30T20:44:19.877Z","etag":null,"topics":["matlab","numerical-analysis","numerical-methods","ode","plot","python3"],"latest_commit_sha":null,"homepage":"http://sudiptomondal.me/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sudipto3331.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-27T09:41:15.000Z","updated_at":"2024-10-16T09:30:18.000Z","dependencies_parsed_at":"2024-08-27T11:10:18.765Z","dependency_job_id":null,"html_url":"https://github.com/sudipto3331/Solving-Ordinary-Differential-Equations-Numerical-Method-Implementation-in-Python","commit_stats":null,"previous_names":["sudipto3331/solving-ordinary-differential-equations-numerical-method-implementation-in-python"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudipto3331%2FSolving-Ordinary-Differential-Equations-Numerical-Method-Implementation-in-Python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudipto3331%2FSolving-Ordinary-Differential-Equations-Numerical-Method-Implementation-in-Python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudipto3331%2FSolving-Ordinary-Differential-Equations-Numerical-Method-Implementation-in-Python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sudipto3331%2FSolving-Ordinary-Differential-Equations-Numerical-Method-Implementation-in-Python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sudipto3331","download_url":"https://codeload.github.com/sudipto3331/Solving-Ordinary-Differential-Equations-Numerical-Method-Implementation-in-Python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246379377,"owners_count":20767694,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["matlab","numerical-analysis","numerical-methods","ode","plot","python3"],"created_at":"2024-10-03T21:44:50.177Z","updated_at":"2025-10-10T01:03:03.691Z","avatar_url":"https://github.com/sudipto3331.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Solving Ordinary Differential Equations Numerical Method Implementation in Python\n\nThis repository contains a Python implementation for solving ordinary differential equations (ODEs) using various numerical methods, including the Euler method, Heun's method, the Midpoint method, and the Fourth Order Runge-Kutta (RK4) method. The code also provides a graphical representation of the solution.\n\n### Table of Contents\n- [Numerical Methods for ODEs Theory](#numerical-methods-for-odes-theory)\n- [Dependencies](#dependencies)\n- [Installation](#installation)\n- [Usage](#usage)\n- [Code Explanation](#code-explanation)\n- [Example](#example)\n- [Files in the Repository](#files-in-the-repository)\n- [Input Parameters](#input-parameters)\n- [Troubleshooting](#troubleshooting)\n- [Author](#author)\n\n### Numerical Methods for ODEs Theory\nThe numerical methods implemented in this code include:\n\n1. **Euler Method**: The simplest numerical method for solving ODEs, which uses the derivative to project forward.\n   \\[\n   y_{n+1} = y_n + h \\cdot f(x_n, y_n)\n   \\]\n\n2. **Heun's Method** (Improved Euler Method): A two-step method that averages slopes at the beginning and end of the interval.\n   \\[\n   k1 = f(x_n, y_n) \\\\\n   k2 = f(x_n + h, y_n + h \\cdot k1) \\\\\n   y_{n+1} = y_n + \\frac{h}{2} (k1 + k2)\n   \\]\n\n3. **Midpoint Method**: A second-order method that makes a prediction at the midpoint of the interval and then corrects it.\n   \\[\n   k1 = f(x_n, y_n) \\\\\n   k2 = f\\left(x_n + \\frac{h}{2}, y_n + \\frac{h}{2} \\cdot k1\\right) \\\\\n   y_{n+1} = y_n + h \\cdot k2\n   \\]\n\n4. **Fourth Order Runge-Kutta Method (RK4)**: A more accurate method that evaluates the slope at multiple points within the interval.\n   \\[\n   k1 = f(x_n, y_n) \\\\\n   k2 = f\\left(x_n + \\frac{h}{2}, y_n + \\frac{h}{2} \\cdot k1\\right) \\\\\n   k3 = f\\left(x_n + \\frac{h}{2}, y_n + \\frac{h}{2} \\cdot k2\\right) \\\\\n   k4 = f(x_n + h, y_n + h \\cdot k3) \\\\\n   y_{n+1} = y_n + \\frac{h}{6} (k1 + 2k2 + 2k3 + k4)\n   \\]\n\n### Dependencies\nThis implementation requires the following libraries:\n- `matplotlib`: For plotting the results.\n- `numpy`: For numerical computations.\n\n### Installation\nTo install the required libraries, you can use `pip`:\n```sh\npip install matplotlib numpy\n```\n\n### Usage\n1. Clone the repository.\n2. Run the script using Python:\n    ```sh\n    python ode_solver.py\n    ```\n3. Input the required parameters when prompted:\n    - Specify `a1` and `a2` based on the method you wish to use.\n    - Enter the order of the ODE.\n    - Specify the lower and upper limits of `x`.\n    - Enter the initial condition for `y`.\n    - Specify the step size `h`.\n\n### Code Explanation\nThe code defines the differential function `f(x, y)` and computes the solution using different methods based on user input. It then plots the results on a graph.\n\nBelow is a snippet from the code illustrating the main logic:\n\n```python\ndef f(x, y):\n   fxy = (-4 * (x**3) + 12 * (x**2) - 20 * x + 8.5)\n   return fxy\n\nx0 = float(input(\"Enter the lower range of x: \"))\nxfinal = float(input(\"Enter the higher range of x: \"))\ny0 = float(input(\"Enter the lower range of y: \"))\nh = float(input(\"Enter the step size: \"))\nn = int(((xfinal - x0) / h) + 1)\nx = np.zeros([n + 1])\ny = np.zeros([n + 1])\n\nx[0] = x0\ny[0] = y0\n\n# Method selection\nif all([a1 == 1, a2 == 0, order == 1]):\n    print(\"Euler method :\")\n    for i in range(n):\n        y[i + 1] = y[i] + h * f(x[i], y[i])\nelif all([a1 == 0.5, a2 == 0.5, order == 2]):\n    print(\"Heuns method :\")\n    for i in range(n):\n        k1 = f(x[i], y[i])\n        k2 = f(x[i] + h, y[i] + k1 * h)\n        y[i + 1] = y[i] + h / 2 * (k1 + k2)\nelif all([a1 == 0, a2 == 1, order == 2]):\n    print(\"Midpoint method :\")\n    for i in range(n):\n        k1 = f(x[i], y[i])\n        k2 = f(x[i] + h / 2, y[i] + k1 * h / 2)\n        y[i + 1] = y[i] + k2 * h\nelif order == 4:\n    print(\"Fourth order RK method :\")\n    for i in range(n):\n        k1 = f(x[i], y[i])\n        k2 = f(x[i] + h / 2, y[i] + k1 * h / 2)\n        k3 = f(x[i] + h / 2, y[i] + k2 * h / 2)\n        k4 = f(x[i] + h, y[i] + k3 * h)\n        y[i + 1] = y[i] + (h / 6) * (k1 + 2 * k2 + 2 * k3 + k4)\n\nplt.figure(1)\nplt.plot(x, y)\nplt.xlabel('Values of X')\nplt.ylabel('Values of Y')\nplt.title('Curve of the ODE')\nplt.legend(['Graphical Curve'], loc='upper right')\nplt.show()\n```\n\n### Example\nBelow is an example of how to use the script:\n\n1. **Run the script**:\n    ```sh\n    python ode_solver.py\n    ```\n\n2. **Enter the input values**:\n    ```\n    Enter the value of a2 FOR USING, euler a2=0, for heuns a2=0.5, for midpoint a2=1: 0.5\n    Enter the value of a1 FOR USING, euler a1=1, for heuns a1=0.5, for midpoint a1=0: 1\n    Enter the value of order: 2\n    Enter the lower range of x: 0\n    Enter the higher range of x: 2\n    Enter the lower range of y: 1\n    Enter the step size: 0.1\n    ```\n\n3. **Output**:\n    - The script will compute the numerical solution using the selected method and plot the results:\n    ```\n    Heuns method :\n    [array of y values]\n    ```\n\n### Files in the Repository\n- `ode_solver.py`: The main script for solving ordinary differential equations using the specified numerical methods.\n\n### Input Parameters\nThe script prompts for the following input values:\n- `a1` and `a2`: Values defining the method to be used.\n- Order of the ODE (`order`).\n- Lower range of `x` (`x0`).\n- Higher range of `x` (`xfinal`).\n- Initial condition for `y` (`y0`).\n- Step size (`h`).\n\n### Troubleshooting\n1. **Method Selection**: Ensure the parameters are correctly set for the desired method to avoid errors.\n2. **Function Definition**: The function `f(x, y)` is currently hardcoded. You may modify it to solve different ODEs as needed.\n3. **Python Version**: This script is compatible with Python 3. Ensure you have Python 3 installed.\n\n## Author\nScript created by sudipto3331.\n\n---\n\nThis documentation should guide you through understanding, installing, and using the ODE solving script. For further issues or feature requests, please open an issue in the repository. Feel free to contribute by creating issues and submitting pull requests. Happy coding!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsudipto3331%2Fsolving-ordinary-differential-equations-numerical-method-implementation-in-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsudipto3331%2Fsolving-ordinary-differential-equations-numerical-method-implementation-in-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsudipto3331%2Fsolving-ordinary-differential-equations-numerical-method-implementation-in-python/lists"}