// Arup Guha
// 3/14/2012

// Modified on 3/19/2012 to work for Runge-Kutta Order Four

#include <stdio.h>

#define N 10

double f(double t, double y);

int main() {

    double w[N+1];

    // Given in the problem.
    double a = 0, b = 2;
    w[0] = 0.5;

    double h = (b - a)/N;
    int i;

    // Do Euler's Method here.
    for (i=0; i<N; i++) {
        double ti = a+i*h;
        double tnext = a+(i+1)*h;

        double k1 = h*f(ti, w[i]);
        double k2 = h*f(ti+h/2,w[i]+k1/2);
        double k3 = h*f(ti+h/2,w[i]+k2/2);
        double k4 = h*f(tnext, w[i]+k3);
        w[i+1] = w[i] + (k1+2*k2+2*k3+k4)/6;
    }

    // Print out the results.
    for (i=0; i<=N; i++) {
        printf("%lf\t%.7lf\n", a+i*h, w[i]);
    }

    return 0;
}

// Example #1 (ppg 266-7)from the textbook.
double f(double t, double y) {
    return y - t*t + 1;
}
