/* Runge-Kutta Program * This solves initial value problems using the Runge-Kutta (Order Four) * Algorithm. It inputs The range of interval of time steps and the initial * value alpha where y(0) = alpha. * * rk1 V. 0.9 written in C by Stefan Joe-Yen, Nov 1995 */ /* Standard Includes */ #include <stdio.h> #include <stdlib.h> #include <math.h> /* Define the function being solved y' = f(t,y) and the number of time steps N */ #define f(t,y) ((float) (y - (t*t) + 1)) #define N 10 main() { float a, b, h, t, w, alpha, K1, K2, K3, K4; int i; /* Input the endpoints and initial value */ printf("please enter a, b, and alpha>"); scanf("%f %f %f", &a, &b, &alpha); /* Set up and output Initial Conditions */ h = (b - a)/N; t = a; w = alpha; printf("t = %f, w = %f\r\n", t, w); /* Use Runge-Kutta to estimate solutions at time step t and output */ for (i=1; i<=N; i++) { K1 = h * f(t,w); K2 = h * f(t + h/2,w + K1/2); K3 = h * f(t + h/2,w + K2/2); K4 = h * f(t + h, w + K3); w = w + (K1 + 2*K2 + 2*K3 + K4)/6; t = a + i * h; printf("t = %f, w = %f\r\n", t, w); } }