Skip to content

Latest commit

 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Numerical Solution of Cauchy Problem for second order ODE

This program solves the Cauchy problem for a second‑order ordinary differential equation:

y'' = f(x, y, y')
y(a) = q₀,  y'(a) = q₁

on the interval [0, 1] with arbitrary initial conditions q₀, q₁ ∈ [-5, 5]. The right‑hand side is chosen as:

f(x, y, y') = y' · cos(x) + y · x³ + tan(x)

Five numerical methods are implemented:

  • Euler's method
  • Modified Euler method (with recalculation)
  • Second‑order Runge–Kutta method
  • Fourth‑order Runge–Kutta method
  • Third‑order Adams explicit method

The step size for each method is chosen automatically to satisfy a prescribed accuracy. The errors are estimated by comparing solutions on grids with steps h, h/2 and 2h. Both absolute and relative errors are computed in three norms (L₁, L₂, L∞) for the solution y and its derivative y'.

Requirements

  • C++ compiler with C++11 support (e.g., g++, clang++)
  • GNU Make
  • Python 3 with matplotlib and numpy (for visualisation)

Quick Start


git clone <your-repo-url>
cd <project-folder>
make          # compile the program
make run      # compile and run

Alternatively, you can run manually:


make
./cauchy

The program will:

  1. Automatically choose appropriate step sizes for each method.
  2. Print four tables of errors (for y and y', comparing h with h/2 and with 2h).
  3. Generate parameters.txt with all numerical solutions.
  4. Launch create_plot.py to show two plots: one for y(x) and one for y'(x), each containing five curves.

To clean up:


make clean

Example Output

After running ./cauchy, the console shows tables similar to the following (values are illustrative):

         table 1: paragraph 6 - printing errors for y for h / 2 and h
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
|          |   abs 1   |   abs 2   |  abs inf  |   rel 1   |   rel 2   |  rel inf  |
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
|   euler   | 1.234e-02 | 1.567e-02 | 1.890e-02 | 2.345e-02 | 2.678e-02 | 2.901e-02 |
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
| mod euler | 3.456e-03 | 4.567e-03 | 5.678e-03 | 6.789e-03 | 7.890e-03 | 8.901e-03 |
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
|    rk2    | 4.567e-04 | 5.678e-04 | 6.789e-04 | 7.890e-04 | 8.901e-04 | 9.012e-04 |
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
|    rk4    | 1.234e-06 | 1.567e-06 | 1.890e-06 | 2.345e-06 | 2.678e-06 | 2.901e-06 |
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
|   adams   | 1.234e-06 | 1.567e-06 | 1.890e-06 | 2.345e-06 | 2.678e-06 | 2.901e-06 |
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

(The actual values depend on the chosen initial conditions and the automatic step selection.)

Additionally, two plots are generated: one for the solution y(x) and one for its derivative y'(x), each showing the five numerical methods.

Project Structure

.
├── Makefile
├── README.md
├── .gitignore
├── LICENSE
├── create_plot.py          # Python script for visualisation
├── parameters.txt          # generated data (ignored)
├── src/
│   ├── main.cpp
│   ├── functions.cpp
│   └── functions.h
├── docs/
│   └── task.pdf
└── build/                  # object files (ignored)

Author

Name: @g306137 Philip K.

Year: 2025

License

This project is distributed under the MIT License.

About

C++ implementation of Euler, modified Euler, RK2, RK4, and Adams explicit methods for solving y'' = f(x, y, y') with adaptive step-size selection and L₁, L₂, L∞ error estimation.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages