ode_exp_euler - x[i] = XMIN + h * i; y[0] = Y0; /* initial...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* ode_exp_euler.c * Example code to solve dy/dx=y using Euler's method. */ #include <stdio.h> #define N 1 /* number of ODEs */ #define NSTEP 10 /* number of integration steps */ #define XMIN 0.0 /* starting point for integration */ #define XMAX 1.0 /* stopping point for integration */ #define Y0 1.0 /* initial value */ void derivs(float xin, float yin[], float dydx[]); void euler(float xin, float yin[], float yout[], float h); int main() { int i; float h = (XMAX - XMIN)/(1.0*NSTEP); /* stepsize for integration */ float yin[N], yout[N]; /* values of y before and after a step, for a given x */ float x[NSTEP+1], y[NSTEP+1]; for (i = 0; i <= NSTEP; i++) /* Define array of x values */
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: x[i] = XMIN + h * i; y[0] = Y0; /* initial value */ printf(&quot;%f %f\n&quot;, x[0], y[0]); for (i = 0; i &lt; NSTEP; i++) /* loop over x values */ { yin[0] = y[i]; euler(x[i], yin, yout, h); /* do one integration step */ y[i+1] = yout[0]; printf(&quot;%f %f\n&quot;, x[i+1], y[i+1]); } return 0; } void derivs(float xin, float yin, float dydx) { /* evaluate RHS of ODE */ dydx[0] = yin[0]; return; } void euler(float xin, float yin, float yout, float h) { /* do one Euler step of size h */ int i; float dydx[N]; derivs(xin, yin, dydx); /* evaluate dy/dx */ for (i = 0; i &lt; N; i++) yout[i] = yin[i] + dydx[i] * h; /* Euler step */ return; }...
View Full Document

Page1 / 2

ode_exp_euler - x[i] = XMIN + h * i; y[0] = Y0; /* initial...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online