Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
ode_forward_euler.c File Reference

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>
Include dependency graph for ode_forward_euler.c:

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.
 

Detailed Description

Solve a multivariable first order ordinary differential equation (ODEs) using forward Euler method

Authors
Krishna Vedala

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. Implementation solution

To implement Van der Pol oscillator, change the problem function to:

const double mu = 2.0;
dy[0] = y[1];
dy[1] = mu * (1.f - y[0] * y[0]) * y[1] - y[0];
See also
ode_midpoint_euler.c, ode_semi_implicit_euler.c

Function Documentation

◆ exact_solution()

void exact_solution ( const double *  x,
double *  y 
)

Exact solution of the problem.

Used for solution comparison.

Parameters
[in]xindependent variable
[in,out]ydependent variable
69{
70 y[0] = cos(x[0]);
71 y[1] = -sin(x[0]);
72}

◆ forward_euler()

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.

Parameters
[in]dxstep size
[in]x0initial value of independent variable
[in]x_maxfinal value of independent variable
[in,out]ytake \(y_n\) and compute \(y_{n+1}\)
[in]save_to_fileflag to save results to a CSV file (1) or not (0)
Returns
time taken for computation in seconds
101{
102 double dy[order];
103
104 FILE *fp = NULL;
105 if (save_to_file)
106 {
107 fp = fopen("forward_euler.csv", "w+");
108 if (fp == NULL)
109 {
110 perror("Error! ");
111 return -1;
112 }
113 }
114
115 /* start integration */
116 clock_t t1 = clock();
117 double x = x0;
118 do // iterate for each step of independent variable
119 {
120 if (save_to_file && fp)
121 fprintf(fp, "%.4g,%.4g,%.4g\n", x, y[0], y[1]); // write to file
122 forward_euler_step(dx, &x, y, dy); // perform integration
123 x += dx; // update step
124 } while (x <= x_max); // till upper limit of independent variable
125 /* end of integration */
126 clock_t t2 = clock();
127
128 if (save_to_file && fp)
129 fclose(fp);
130
131 return (double)(t2 - t1) / CLOCKS_PER_SEC;
132}
#define order
number of dependent variables in problem
Definition ode_forward_euler.c:44
void forward_euler_step(const double dx, const double *x, double *y, double *dy)
Compute next step approximation using the forward-Euler method.
Definition ode_forward_euler.c:82
Here is the call graph for this function:

◆ forward_euler_step()

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)\]

Parameters
[in]dxstep size
[in,out]xtake \(x_n\) and compute \(x_{n+1}\)
[in,out]ytake \(y_n\) and compute \(y_{n+1}\)
[in,out]dycompute \(f\left(x_n,y_n\right)\)
83{
84 int o;
85 problem(x, y, dy);
86 for (o = 0; o < order; o++) y[o] += dx * dy[o];
87}
void problem(const double *x, double *y, double *dy)
Problem statement for a system with first-order differential equations.
Definition ode_forward_euler.c:55
Here is the call graph for this function:

◆ main()

int main ( int  argc,
char *  argv[] 
)

Main Function.

138{
139 double X0 = 0.f; /* initial value of x0 */
140 double X_MAX = 10.F; /* upper limit of integration */
141 double Y0[] = {1.f, 0.f}; /* initial value Y = y(x = x_0) */
142 double step_size;
143
144 if (argc == 1)
145 {
146 printf("\nEnter the step size: ");
147 scanf("%lg", &step_size);
148 }
149 else
150 // use commandline argument as independent variable step size
151 step_size = atof(argv[1]);
152
153 // get approximate solution
154 double total_time = forward_euler(step_size, X0, X_MAX, Y0, 1);
155 printf("\tTime = %.6g ms\n", total_time);
156
157 /* compute exact solution for comparion */
158 FILE *fp = fopen("exact.csv", "w+");
159 if (fp == NULL)
160 {
161 perror("Error! ");
162 return -1;
163 }
164 double x = X0;
165 double *y = &(Y0[0]);
166 printf("Finding exact solution\n");
167 clock_t t1 = clock();
168
169 do
170 {
171 fprintf(fp, "%.4g,%.4g,%.4g\n", x, y[0], y[1]); // write to file
172 exact_solution(&x, y);
173 x += step_size;
174 } while (x <= X_MAX);
175
176 clock_t t2 = clock();
177 total_time = (t2 - t1) / CLOCKS_PER_SEC;
178 printf("\tTime = %.6g ms\n", total_time);
179 fclose(fp);
180
181 return 0;
182}
void exact_solution(const double *x, double *y)
Exact solution of the problem.
Definition ode_forward_euler.c:68
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.
Definition ode_forward_euler.c:99
Here is the call graph for this function:

◆ problem()

void problem ( const double *  x,
double *  y,
double *  dy 
)

Problem statement for a system with first-order differential equations.

Updates the system differential variables.

Note
This function can be updated to and ode of any order.
Parameters
[in]xindependent variable(s)
[in,out]ydependent variable(s)
[in,out]dyfirst-derivative of dependent variable(s)
56{
57 const double omega = 1.F; // some const for the problem
58 dy[0] = y[1]; // x dot
59 dy[1] = -omega * omega * y[0]; // y dot
60}