Commit 3f0c473f authored by Taddeüs Kroes's avatar Taddeüs Kroes

ModSim ass3: Added early implementation of Euler method.

parent 23a5b48b
...@@ -4,11 +4,11 @@ LFLAGS=-lm ...@@ -4,11 +4,11 @@ LFLAGS=-lm
all: test all: test
test: test.o methods.o test: test.c methods.c
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $^ $(CC) $(CFLAGS) $(LFLAGS) -o $@ $^
%.o: %.c #%.o: %.c
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $^ # $(CC) $(CFLAGS) $(LFLAGS) -o $@ $^
clean: clean:
rm *.o test rm *.o test
...@@ -20,15 +20,20 @@ occurs (overflow, NaN or such) ...@@ -20,15 +20,20 @@ occurs (overflow, NaN or such)
int Euler(double t0, double t1, double dt, double *y0, double *y1, int N, int Euler(double t0, double t1, double dt, double *y0, double *y1, int N,
f_ptr f, void *params) { f_ptr f, void *params) {
int i, steps = (int)((t1 - t0) / dt + .5); int i;
double slope[N];
for( i = 0; i < N; i++ ) {
if( f(t0, y0, slope, params) ) {
puts("Error calculating slope.");
return -1;
} }
return 0; for( i = 0; i < N; i++ )
} y1[i] = y0[i] + slope[i] * dt;
return (t1 - t0) > dt ? Euler(t0 + dt, t1, dt, y1, y1, N, f, params) : 0;
}
/*
int RungeKutta2(double t0, double t1, double dt, double *y0, double *y1, int N, int RungeKutta2(double t0, double t1, double dt, double *y0, double *y1, int N,
f_ptr f, void *params) { f_ptr f, void *params) {
...@@ -39,4 +44,4 @@ int RungeKutta4(double t0, double t1, double dt, double *y0, double *y1, int N, ...@@ -39,4 +44,4 @@ int RungeKutta4(double t0, double t1, double dt, double *y0, double *y1, int N,
f_ptr f, void *params) f_ptr f, void *params)
return 0; return 0;
} }*/
...@@ -2,9 +2,10 @@ typedef int (*f_ptr)(double t, double *y, double *dy, void *params); ...@@ -2,9 +2,10 @@ typedef int (*f_ptr)(double t, double *y, double *dy, void *params);
int Euler(double t0, double t1, double dt, double *y0, double *y1, int N, int Euler(double t0, double t1, double dt, double *y0, double *y1, int N,
f_ptr f, void *params); f_ptr f, void *params);
/*
int RungeKutta2(double t0, double t1, double dt, double *y0, double *y1, int N, int RungeKutta2(double t0, double t1, double dt, double *y0, double *y1, int N,
f_ptr f, void *params); f_ptr f, void *params);
int RungeKutta4(double t0, double t1, double dt, double *y0, double *y1, int N, int RungeKutta4(double t0, double t1, double dt, double *y0, double *y1, int N,
f_ptr f, void *params); f_ptr f, void *params);
*/
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <math.h>
#include "methods.h" #include "methods.h"
int f_test_1(double t, double *y, double *dy, void *params) { int f_test_1(double t, double *y, double *dy, void *params) {
*dy = 1.0; *dy = 1.0;
return 1; return 0;
} }
int f_test_2(double t, double *y, double *dy, void *params) { int f_test_3(double t, double *y, double *dy, void *params) {
double result = *y; if( isnan(*y) ) {
if( isnan(result) ) {
return -1; return -1;
} else { } else {
*dy = result; *dy = *y;
return 0; return 0;
} }
} }
int f_test_3(double t, double *y, double *dy, void *params) { int f_test_4(double t, double *y, double *dy, void *params) {
double result = *y * *y; double result = *y * *y;
if( isnan(result) ) { if( isnan(result) ) {
...@@ -30,8 +29,20 @@ int f_test_3(double t, double *y, double *dy, void *params) { ...@@ -30,8 +29,20 @@ int f_test_3(double t, double *y, double *dy, void *params) {
} }
} }
int f_cos(double t, double *y, double *dy, void *params) {
*dy = cos(t);
return 0;
}
int main(void) { int main(void) {
double y0[1] = {.506}, y1[1];
if( Euler(-100.0, 10.0, 0.001, y0, y1, 1, &f_cos, NULL) ) {
puts("FAIL");
} else {
printf("y1: %f\n", *y1);
}
return 0; return 0;
} }
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