Commit 27ae34d8 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Implemented Euler method.

parent 337aed09
......@@ -20,18 +20,25 @@ occurs (overflow, NaN or such)
int Euler(double t0, double t1, double dt, double *y0, double *y1, int N,
f_ptr f, void *params) {
int i;
double slope[N];
int i, j, intervals = (t1 - t0) / dt;
double slope[N], t = t0;
if( f(t0, y0, slope, params) ) {
puts("Error calculating slope.");
return -1;
}
for( j = 0; j < N; j++ )
y1[j] = y0[j];
for( i = 0; i < intervals; i++ ) {
if( f(t, y0, slope, params) ) {
puts("Error calculating slope.");
return -1;
}
for( i = 0; i < N; i++ )
y1[i] = y0[i] + slope[i] * dt;
for( j = 0; j < N; j++ )
y1[j] = y1[j] + slope[j] * dt;
return (t1 - t0) > dt ? Euler(t0 + dt, t1, dt, y1, y1, N, f, params) : 0;
t = t0 + i * dt;
}
return 0;
}
/*
int RungeKutta2(double t0, double t1, double dt, double *y0, double *y1, int N,
......
......@@ -36,9 +36,9 @@ int f_cos(double t, double *y, double *dy, void *params) {
}
int main(void) {
double y0[1] = {.506}, y1[1];
double y0[1] = {.0}, y1[1];
if( Euler(-100.0, 10.0, 0.001, y0, y1, 1, &f_cos, NULL) ) {
if( Euler(.0, 10.0, .0001, y0, y1, 1, &f_cos, NULL) ) {
puts("FAIL");
} else {
printf("y1: %f\n", *y1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment