|
Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
Solve a multivariable first order ordinary differential equation (ODEs) using midpoint Euler method More...
#include <math.h>#include <stdio.h>#include <stdlib.h>#include <time.h>Macros | |
| #define | order 2 |
| number of dependent variables in problem | |
Functions | |
| void | problem (const double *x, double *y, double *dy) |
| Problem statement for a system with first-order differential equations. | |
| void | exact_solution (const double *x, double *y) |
| Exact solution of the problem. | |
| void | midpoint_euler_step (double dx, double *x, double *y, double *dy) |
| Compute next step approximation using the midpoint-Euler method. | |
| double | midpoint_euler (double dx, double x0, double x_max, double *y, char save_to_file) |
| Compute approximation using the midpoint-Euler method in the given limits. | |
| int | main (int argc, char *argv[]) |
| Main Function. | |
Solve a multivariable first order ordinary differential equation (ODEs) using midpoint Euler method
The ODE being solved is:
\begin{eqnarray*} \dot{u} &=& v\\ \dot{v} &=& -\omega^2 u\\ \omega &=& 1\\ [x_0, u_0, v_0] &=& [0,1,0]\qquad\ldots\text{(initial values)} \end{eqnarray*}
The exact solution for the above problem is:
\begin{eqnarray*} u(x) &=& \cos(x)\\ v(x) &=& -\sin(x)\\ \end{eqnarray*}
The computation results are stored to a text file midpoint_euler.csv and the exact soltuion results in exact.csv for comparison.
To implement Van der Pol oscillator, change the problem function to:
| void exact_solution | ( | const double * | x, |
| double * | y | ||
| ) |
Exact solution of the problem.
Used for solution comparison.
| [in] | x | independent variable |
| [in,out] | y | dependent variable |
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Main Function.
| double midpoint_euler | ( | double | dx, |
| double | x0, | ||
| double | x_max, | ||
| double * | y, | ||
| char | save_to_file | ||
| ) |
Compute approximation using the midpoint-Euler method in the given limits.
| [in] | dx | step size |
| [in] | x0 | initial value of independent variable |
| [in] | x_max | final value of independent variable |
| [in,out] | y | take \(y_n\) and compute \(y_{n+1}\) |
| [in] | save_to_file | flag to save results to a CSV file (1) or not (0) |
| void midpoint_euler_step | ( | double | dx, |
| double * | x, | ||
| double * | y, | ||
| double * | dy | ||
| ) |
Compute next step approximation using the midpoint-Euler method.
\[y_{n+1} = y_n + dx\, f\left(x_n+\frac{1}{2}dx, y_n + \frac{1}{2}dx\,f\left(x_n,y_n\right)\right)\]
| [in] | dx | step size |
| [in,out] | x | take \(x_n\) and compute \(x_{n+1}\) |
| [in,out] | y | take \(y_n\) and compute \(y_{n+1}\) |
| [in,out] | dy | compute \(y_n+\frac{1}{2}dx\,f\left(x_n,y_n\right)\) |
| void problem | ( | const double * | x, |
| double * | y, | ||
| double * | dy | ||
| ) |
Problem statement for a system with first-order differential equations.
Updates the system differential variables.
| [in] | x | independent variable(s) |
| [in,out] | y | dependent variable(s) |
| [in,out] | dy | first-derivative of dependent variable(s) |