Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
ode_semi_implicit_euler.c File Reference

Solve a multivariable first order ordinary differential equation (ODEs) using semi implicit Euler method More...

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Include dependency graph for ode_semi_implicit_euler.c:

Macros

#define order   2
 

Functions

void problem (const double *x, double *y, double *dy)
 Problem statement for a system with first-order differential equations. Updates the system differential variables. More...
 
void exact_solution (const double *x, double *y)
 Exact solution of the problem. Used for solution comparison. More...
 
void semi_implicit_euler_step (double dx, double *x, double *y, double *dy)
 Compute next step approximation using the semi-implicit-Euler method. More...
 
double semi_implicit_euler (double dx, double x0, double x_max, double *y, char save_to_file)
 Compute approximation using the semi-implicit-Euler method in the given limits. More...
 
int main (int argc, char *argv[])
 

Detailed Description

Solve a multivariable first order ordinary differential equation (ODEs) using semi implicit 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 semi_implicit_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];

Van der Pol Oscillator solution

See also
ode_forward_euler.c, ode_midpoint_euler.c

Macro Definition Documentation

◆ order

#define order   2

number of dependent variables in problem

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
72 {
73  y[0] = cos(x[0]);
74  y[1] = -sin(x[0]);
75 }

◆ main()

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

Main Function

148 {
149  double X0 = 0.f; /* initial value of x0 */
150  double X_MAX = 10.F; /* upper limit of integration */
151  double Y0[] = {1.f, 0.f}; /* initial value Y = y(x = x_0) */
152  double step_size;
153 
154  if (argc == 1)
155  {
156  printf("\nEnter the step size: ");
157  scanf("%lg", &step_size);
158  }
159  else
160  // use commandline argument as independent variable step size
161  step_size = atof(argv[1]);
162 
163  // get approximate solution
164  double total_time = semi_implicit_euler(step_size, X0, X_MAX, Y0, 1);
165  printf("\tTime = %.6g ms\n", total_time);
166 
167  /* compute exact solution for comparion */
168  FILE *fp = fopen("exact.csv", "w+");
169  if (fp == NULL)
170  {
171  perror("Error! ");
172  return -1;
173  }
174  double x = X0;
175  double *y = &(Y0[0]);
176  printf("Finding exact solution\n");
177  clock_t t1 = clock();
178 
179  do
180  {
181  fprintf(fp, "%.4g,%.4g,%.4g\n", x, y[0], y[1]); // write to file
182  exact_solution(&x, y);
183  x += step_size;
184  } while (x <= X_MAX);
185 
186  clock_t t2 = clock();
187  total_time = (t2 - t1) / CLOCKS_PER_SEC;
188  printf("\tTime = %.6g ms\n", total_time);
189  fclose(fp);
190 
191  return 0;
192 }
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)
59 {
60  const double omega = 1.F; // some const for the problem
61  dy[0] = y[1]; // x dot
62  dy[1] = -omega * omega * y[0]; // y dot
63 }

◆ semi_implicit_euler()

double semi_implicit_euler ( double  dx,
double  x0,
double  x_max,
double *  y,
char  save_to_file 
)

Compute approximation using the semi-implicit-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
111 {
112  double dy[order];
113 
114  FILE *fp = NULL;
115  if (save_to_file)
116  {
117  fp = fopen("semi_implicit_euler.csv", "w+");
118  if (fp == NULL)
119  {
120  perror("Error! ");
121  return -1;
122  }
123  }
124 
125  /* start integration */
126  clock_t t1 = clock();
127  double x = x0;
128  do // iterate for each step of independent variable
129  {
130  if (save_to_file && fp)
131  fprintf(fp, "%.4g,%.4g,%.4g\n", x, y[0], y[1]); // write to file
132  semi_implicit_euler_step(dx, &x, y, dy); // perform integration
133  x += dx; // update step
134  } while (x <= x_max); // till upper limit of independent variable
135  /* end of integration */
136  clock_t t2 = clock();
137 
138  if (save_to_file && fp)
139  fclose(fp);
140 
141  return (double)(t2 - t1) / CLOCKS_PER_SEC;
142 }
Here is the call graph for this function:

◆ semi_implicit_euler_step()

void semi_implicit_euler_step ( double  dx,
double *  x,
double *  y,
double *  dy 
)

Compute next step approximation using the semi-implicit-Euler method.

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 \(y_n+\frac{1}{2}dx\,f\left(x_n,y_n\right)\)
86 {
87  int o;
88 
89  problem(x, y, dy); // update dy once
90  y[0] += dx * dy[0]; // update y0
91 
92  problem(x, y, dy); // update dy once more
93 
94  for (o = 1; o < order; o++)
95  y[o] += dx * dy[o]; // update remaining using new dy
96  *x += dx;
97 }
Here is the call graph for this function:
semi_implicit_euler
double semi_implicit_euler(double dx, double x0, double x_max, double *y, char save_to_file)
Compute approximation using the semi-implicit-Euler method in the given limits.
Definition: ode_semi_implicit_euler.c:109
exact_solution
void exact_solution(const double *x, double *y)
Exact solution of the problem. Used for solution comparison.
Definition: ode_semi_implicit_euler.c:71
problem
void problem(const double *x, double *y, double *dy)
Problem statement for a system with first-order differential equations. Updates the system differenti...
Definition: ode_semi_implicit_euler.c:58
order
#define order
Definition: ode_semi_implicit_euler.c:47
semi_implicit_euler_step
void semi_implicit_euler_step(double dx, double *x, double *y, double *dy)
Compute next step approximation using the semi-implicit-Euler method.
Definition: ode_semi_implicit_euler.c:85