Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
Solve a multivariable first order ordinary differential equation (ODEs) using forward 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 | forward_euler_step (const double dx, const double *x, double *y, double *dy) |
Compute next step approximation using the forward-Euler method. | |
double | forward_euler (double dx, double x0, double x_max, double *y, char save_to_file) |
Compute approximation using the forward-Euler method in the given limits. | |
int | main (int argc, char *argv[]) |
Main Function. | |
Solve a multivariable first order ordinary differential equation (ODEs) using forward 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 forward_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 |
double forward_euler | ( | double | dx, |
double | x0, | ||
double | x_max, | ||
double * | y, | ||
char | save_to_file | ||
) |
Compute approximation using the forward-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 forward_euler_step | ( | const double | dx, |
const double * | x, | ||
double * | y, | ||
double * | dy | ||
) |
Compute next step approximation using the forward-Euler method.
\[y_{n+1}=y_n + dx\cdot f\left(x_n,y_n\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 \(f\left(x_n,y_n\right)\) |
int main | ( | int | argc, |
char * | argv[] | ||
) |
Main Function.
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) |