Last active
August 29, 2015 13:55
-
-
Save tylerjw/8715238 to your computer and use it in GitHub Desktop.
Euler's Method (recursive).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Euler's Method (recursive). | |
@author Tyler Weaver | |
Compile: | |
gcc euler_r.c -o euler_r | |
Run: | |
./euler_r | |
*/ | |
#include <stdio.h> | |
#include <stdbool.h> | |
#define X 0 | |
#define Y 1 | |
double function(double x, double y); | |
double euler(double step_size, double target_x, | |
double initial_x, double initial_y, | |
double (*func)(double,double), bool debug); | |
int main() | |
{ | |
const double step_size = 0.1; | |
const double initial_value[2] = {0,1}; | |
const double target_x = 0.5; | |
double output; | |
output = euler(step_size, target_x, initial_value[X], initial_value[Y], | |
function, true); | |
printf("output = %f\r\n", output); | |
return 0; | |
} | |
double function(double x, double y) | |
{ | |
long double value = x*y + y; | |
return value; | |
} | |
double euler(double step_size, | |
double target_x, double x, double y, | |
double (*func)(double,double), bool debug) | |
{ | |
double target_y; // the output value we are looking for | |
if(x == target_x) // we have reached the end | |
return y; | |
else | |
{ | |
y = y + (step_size*func(x, y)); // calculate the new y value | |
x = x + step_size; // update x | |
if(debug) | |
printf("(%f,%f)\r\n", x,y); // output values for debuging | |
target_y = euler(step_size,target_x,x,y,func,debug); // do it again... | |
} | |
return target_y; // return the target_y value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment