Skip to content

Repository files navigation

Numerical Methods for Solving Nonlinear Equations

Python Version License Maintenance

A comprehensive Python application with tkinter GUI that implements three numerical methods for solving nonlinear equations: Bisection, Newton-Raphson, and Secant methods.

📋 Table of Contents

Features

🔢 Three Numerical Methods

  • Bisection Method: Guaranteed convergence with linear rate
  • Newton-Raphson Method: Fast quadratic convergence
  • Secant Method: Super-linear convergence without derivative

🎯 Key Capabilities

  • Solve nonlinear equations using any of the three methods
  • Compare all methods simultaneously
  • Visualize functions and convergence
  • Detailed iteration-by-iteration analysis
  • Error tracking and convergence comparison
  • Pre-defined test functions
  • Custom function input support

📊 Visualization

  • Function plotting with root markers
  • Convergence rate graphs (log scale)
  • Side-by-side method comparison charts
  • Interactive matplotlib plots

🚀 Installation

Prerequisites

  • Python 3.7 or higher
  • pip package manager

Quick Setup

  1. Clone the repository

    git clone https://github.com/HasithFernando/Numerical-Methods-GUI.git
    cd Numerical-Methods-GUI
  2. Install Required Packages

    pip install -r requirements.txt

    Or install individually:

    pip install numpy matplotlib sympy
  3. Run the Application

    python numerical_methods_gui.py

⚡ Quick Start

# Simply run the application
python numerical_methods_gui.py

# Or on Windows
py numerical_methods_gui.py

Then:

  1. Enter a function (e.g., x**3 - 6*x**2 + 11*x - 6)
  2. Select a method (Bisection, Newton-Raphson, or Secant)
  3. Set parameters
  4. Click "Solve" or "Compare All Methods"

Usage Guide

Basic Operation

  1. Enter a Function

    • Type in the function input box using Python syntax
    • Example: x**3 - 6*x**2 + 11*x - 6
    • Or select from predefined functions
  2. Select a Method

    • Choose between Bisection, Newton-Raphson, or Secant method
    • Enter method-specific parameters:
      • Bisection: Interval [a, b]
      • Newton-Raphson: Initial guess x₀
      • Secant: Two initial guesses x₀ and x₁
  3. Set Parameters

    • Tolerance (default: 1e-6)
    • Maximum iterations (default: 100)
  4. Solve or Compare

    • Click "Solve" to run the selected method
    • Click "Compare All Methods" to run all three methods simultaneously

Function Syntax

Supported operators and functions:

  • Operators: +, -, *, /, ** (power)
  • Functions: sin(x), cos(x), tan(x), exp(x), log(x), sqrt(x), abs(x)
  • Constants: pi, e
  • Variable: Always use x as the variable

Example Functions

  1. Polynomial: x**3 - 6*x**2 + 11*x - 6

    • Roots at x = 1, 2, 3
  2. Transcendental: cos(x) - x

    • Root near x ≈ 0.739
  3. Exponential: exp(x) - 3*x**2

    • Root near x ≈ 0.620
  4. Simple: x**2 - 4

    • Roots at x = ±2
  5. Mixed: sin(x) - x/2

    • Root at x = 0 and others

Application Tabs

1. Input & Solve Tab

  • Function input with predefined options
  • Method selection (radio buttons)
  • Parameter configuration
  • Solve and comparison buttons

2. Results Tab

  • Detailed solution information
  • Iteration-by-iteration table
  • Function plot with root marker
  • Convergence rate graph

3. Comparison Tab

  • Summary table of all methods
  • Detailed analysis (fastest, most accurate)
  • Bar charts comparing:
    • Number of iterations
    • Final error
    • Computation time

4. About Tab

  • Method descriptions
  • Convergence properties
  • Usage instructions
  • Feature list

Method Details

Bisection Method

  • Input: Function f(x), interval [a, b], tolerance, max iterations
  • Requirement: f(a) and f(b) must have opposite signs
  • Convergence: Linear, guaranteed
  • Output: Root, iterations, error, iteration history

Newton-Raphson Method

  • Input: Function f(x), initial guess x₀, tolerance, max iterations
  • Derivative: Automatically computed using SymPy
  • Convergence: Quadratic (when close to root)
  • Warning: May diverge with poor initial guess
  • Output: Root, iterations, error, iteration history

Secant Method

  • Input: Function f(x), two initial guesses x₀ and x₁, tolerance, max iterations
  • Advantage: No derivative needed
  • Convergence: Super-linear
  • Output: Root, iterations, error, iteration history

Output Information

Solution Results Include:

  • Estimated root value (15 decimal places)
  • Function value at root f(root)
  • Number of iterations
  • Final error
  • Computation time

Iteration Tables Show:

  • Bisection: a, b, midpoint c, f(c), error
  • Newton-Raphson: x, f(x), f'(x), x_new, error
  • Secant: x₀, x₁, f(x₁), x₂, error

Comparative Analysis Shows:

  • Method rankings by speed
  • Accuracy comparison
  • Performance metrics
  • Visual comparisons

Tips for Best Results

  1. Bisection Method

    • Ensure f(a) and f(b) have opposite signs
    • Choose a small interval containing the root
    • Works well for all continuous functions
  2. Newton-Raphson Method

    • Start with a guess close to the expected root
    • Avoid points where f'(x) = 0
    • Very fast when it converges
  3. Secant Method

    • Choose initial guesses on opposite sides of root
    • Good alternative when derivative is complex
    • More robust than Newton-Raphson
  4. General

    • Use smaller tolerance for higher accuracy
    • Increase max iterations for difficult functions
    • Compare methods to understand behavior

Troubleshooting

Common Errors

  1. "f(a) and f(b) must have opposite signs"

    • Solution: Choose different interval for bisection method
  2. "Derivative too close to zero"

    • Solution: Change initial guess for Newton-Raphson method
  3. "Invalid function"

    • Solution: Check function syntax, use ** for power, not ^
  4. "Could not compute derivative"

    • Solution: Simplify function or use Secant method instead
  5. Method doesn't converge

    • Solution: Try different initial values or increase max iterations

📁 Project Structure

numerical-methods-gui/
│
├── numerical_methods_gui.py    # Main application file
├── requirements.txt            # Python dependencies
├── README.md                   # This file
├── LICENSE                     # MIT License
├── CONTRIBUTING.md            # Contribution guidelines
└── .gitignore                 # Git ignore file

Technical Details

Dependencies

  • tkinter: GUI framework (included with Python)
  • numpy: Numerical computations
  • matplotlib: Plotting and visualization
  • sympy: Symbolic mathematics (derivative computation)

Code Structure

  • Object-oriented design with main NumericalMethodsGUI class
  • Separate methods for each numerical algorithm
  • Modular visualization functions
  • Error handling and validation

Performance

  • Real-time computation
  • Efficient plotting with matplotlib
  • Memory-optimized iteration storage
  • Fast symbolic differentiation

Academic Context

This application is designed for the coursework: "Numerical Methods for Solving Nonlinear Equations: Theory and Python Implementation"

Coursework Components Covered:

  1. ✅ Theoretical Background (implemented in code comments and About tab)
  2. ✅ Python Implementation (all three methods fully implemented)
  3. ✅ Comparative Analysis (comprehensive comparison feature)
  4. ✅ Report and Documentation (results export and detailed output)

Test Equations Included:

  1. f(x) = x³ - 6x² + 11x - 6 (roots at x = 1, 2, 3)
  2. f(x) = cos(x) - x (root near x ≈ 0.739)
  3. f(x) = eˣ - 3x² (root near x ≈ 0.620)

📝 Examples

Example 1: Finding a Simple Root

Function: x**2 - 4
Method: Bisection
Interval: [1, 3]
Result: x = 2.000000

Example 2: Transcendental Equation

Function: cos(x) - x
Method: Newton-Raphson
Initial guess: 0.5
Result: x0.739085

Example 3: Comparing All Methods

Function: exp(x) - 3*x**2
All methods tested simultaneously
Best convergence: Newton-Raphson (4 iterations)

🤝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.

Ways to Contribute:

  • 🐛 Report bugs
  • 💡 Suggest new features
  • 📖 Improve documentation
  • 🔧 Submit bug fixes
  • ✨ Add new numerical methods

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

👥 Authors & Acknowledgments

Developed by Group E:

Academic Context:

  • Course: CS303.3 - Computational Theory
  • Institution: NSBM Green University
  • Date: November 2025

🙏 Acknowledgments

  • Built for educational purposes as part of numerical methods coursework
  • Inspired by classical numerical analysis textbooks
  • Thanks to the Python scientific computing community

📞 Support

If you have questions or need help:

  • Open an issue
  • Check the wiki
  • Read the About tab in the application

🌟 Show Your Support

Give a ⭐️ if this project helped you!


Note: This application provides numerical approximations. For exact analytical solutions, use computer algebra systems like Mathematica or SymPy directly.

Made with ❤️ for learning and education

About

A Python GUI application for solving nonlinear equations using Bisection, Newton-Raphson, and Secant methods.

Topics

Resources

Contributing

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages