int_sine_rk2 - { yin[0] = y[i]; rk2(x[i], yin, yout, h); /*...

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

View Full Document Right Arrow Icon
/* int_sine_rk2.c * Example code to integrate sin(x) from zero to pi by solving an ODE. */ #include <stdio.h> #include <math.h> #define N 1 /* number of ODEs */ #define NSTEP 10 /* number of integration steps */ #define XMIN 0.0 /* starting point for integration */ #define XMAX 3.14159265 /* stopping point for integration */ #define Y0 0.0 /* initial value */ void rk2(float xin, float yin[], float yout[], float h); void derivs(float xin, float yin[], float dydx[]); int main() { int i=0; float h = (XMAX - XMIN)/(1.*NSTEP); /* stepsize for integration */ float yin[N], yout[N]; float x[NSTEP+1]; float y[NSTEP+1]; for (i = 0; i <= NSTEP; i++) /* Define array of x values */ x[i] = XMIN + h*i; y[0]=Y0; /* initial value */ for (i = 0; i < NSTEP; i++) /* do integration */
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: { yin[0] = y[i]; rk2(x[i], yin, yout, h); /* do one integration step */ y[i+1] = yout[0]; } printf(&quot;Estimate of integral = %f\n&quot;, y[NSTEP]); return 0; } void derivs(float xin, float yin, float dydx) { /* evaluate RHS of ODE */ dydx[0] = sin(xin); return; } void rk2(float xin, float yin, float yout, float h) { /* Second order Runge-Kutta scheme */ int i; float k1[N], k2[N], yt[N], dydx[N]; /* N is the number of ODEs */ /* Evaluate k1 */ derivs(xin, yin, dydx); for (i = 0; i &lt; N; i++) { k1[i] = h*dydx[i]; yt[i] = yin[i] + 0.5*k1[i]; } /* Evaluate k2, then update the dependent variable */ derivs(xin + 0.5*h, yt, dydx); for (i = 0; i &lt; N; i++) { k2[i] = h*dydx[i]; yout[i] = yin[i] + k2[i]; } return; }...
View Full Document

Page1 / 2

int_sine_rk2 - { yin[0] = y[i]; rk2(x[i], yin, yout, h); /*...

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