Skip to content

Instantly share code, notes, and snippets.

@tylerjw
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerjw/8717988 to your computer and use it in GitHub Desktop.
Save tylerjw/8717988 to your computer and use it in GitHub Desktop.
MATH450 Homework Wk4
/**
Euler's Method (recursive).
@author Tyler Weaver
Compile:
gcc euler_r.c -o euler_r
Run:
./euler_r
Homework 4.8
MATH 450
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define X 0
#define Y 1
double func_Dy(double x, double y); // y' = (y + x)^2
double func_y(double x); // y = tan(x) - x
double euler(double step_size, double target_x,
double initial_x, double initial_y,
double (*func)(double,double),
double (*actual)(double),
bool debug);
int main()
{
const double step_size = 0.1;
const double initial_value[2] = {0,0};
const double target_x = 2.0;
double output;
output = euler(step_size, target_x, initial_value[X], initial_value[Y],
func_Dy, func_y, true);
//printf("output = %f\r\n", output);
return 0;
}
double func_Dy(double x, double y)
{
double value = pow(x+y,2);
return value;
}
double func_y(double x)
{
double y = tan(x) - x;
return y;
}
double euler(double step_size,
double target_x, double x, double y,
double (*func)(double,double),
double (*actual)(double), bool debug)
{
static bool first = true;
double target_y; // the output value we are looking for
double exact, error;
if(first && debug) // first time through, print the table header
{
printf("x_n, y_n, exact, error\r\n");
printf("------|--------|--------|------\r\n");
first = false;
}
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
exact = actual(x);
error = fabsf(exact - y);
if(debug)
printf("%3.1f, %7.3f, %7.3f, %7.3f\r\n", x,y,exact,error); // output values for debuging
target_y = euler(step_size,target_x,x,y,func,actual,debug); // do it again...
}
first = true; // reset the first flag
return target_y; // return the target_y value
}
/**
Improved Euler's Method (recursive).
@author Tyler Weaver
Compile:
gcc imp_euler.c -o imp_euler
Run:
./imp_euler
Homework 4.9
MATH450
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define X 0
#define Y 1
double func_Dy(double x, double y); // y' = (y + x)^2
double func_y(double x); // y = tan(x) - x
double improved_euler(double step_size, double target_x,
double initial_x, double initial_y,
double (*func)(double,double),
double (*actual)(double),
bool debug);
int main()
{
const double step_size = 0.1;
const double initial_value[2] = {0,0};
const double target_x = 1.5;
double output;
output = improved_euler(step_size, target_x, initial_value[X], initial_value[Y],
func_Dy, func_y, true);
//printf("output = %f\r\n", output);
return 0;
}
double func_Dy(double x, double y)
{
double value = pow(x+y,2);
return value;
}
double func_y(double x)
{
double y = tan(x) - x;
return y;
}
double improved_euler(double step_size,
double target_x, double x, double y,
double (*func)(double,double),
double (*actual)(double), bool debug)
{
static bool first = true;
double target_y; // the output value we are looking for
double exact, error;
double predictor;
if(first && debug) // first time through, print the table header
{
printf("x_n, y_n, exact, error\r\n");
printf("------|--------|--------|------\r\n");
first = false;
}
if(x >= target_x) // we have reached the end
return y;
else
{
predictor = y + (step_size*func(x, y)); // calculate the predictor
// now the corrector
y = y + 0.5*step_size*(func(x,y) + func(x+step_size,predictor));
x = x + step_size; // update x
exact = actual(x);
error = fabsf(exact - y);
if(debug)
printf("%3.1f, %7.3f, %7.3f, %7.3f\r\n", x,y,exact,error); // output values for debuging
target_y = improved_euler(step_size,target_x,x,y,func,actual,debug); // do it again...
}
first = true; // reset the first flag
return target_y; // return the target_y value
}
/**
Runge-Kutta Method (recursive).
@author Tyler Weaver
Compile:
gcc runge-kutta.c -o runge-kutta
Run:
./runge-kutta
Homework 4.10
MATH450
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define X 0
#define Y 1
double func_Dy(double x, double y); // y' = (y + x)^2
double func_y(double x); // y = tan(x) - x
double runge_kutta(double h, double target_x,
double initial_x, double initial_y,
double (*func)(double,double),
double (*actual)(double),
bool debug);
int main()
{
const double h = 0.1;
const double initial_value[2] = {0,0};
const double target_x = 1.5;
double output;
output = runge_kutta(h, target_x, initial_value[X], initial_value[Y],
func_Dy, func_y, true);
//printf("output = %f\r\n", output);
return 0;
}
double func_Dy(double x, double y)
{
double value = pow(x+y,2);
return value;
}
double func_y(double x)
{
double y = tan(x) - x;
return y;
}
double runge_kutta(double h,
double target_x, double x, double y,
double (*func)(double,double),
double (*actual)(double), bool debug)
{
static bool first = true;
double target_y; // the output value we are looking for
double exact, error;
double k[4];
if(first && debug) // first time through, print the table header
{
printf("Runge-Kutta method\r\n");
printf("x_n, y_n, exact, error\r\n");
printf("------|--------|--------|------\r\n");
first = false;
}
if(x >= target_x) // we have reached the end
return y;
else
{
k[0] = h*func(x,y); // k1
k[1] = h*func(x + .5*h, y + .5*k[0]); // k2
k[2] = h*func(x + .5*h, y + .5*k[1]); // k3
k[3] = h*func(x + h, y + k[2]); // k4
x = x + h; // update x
y = y + (k[0] + 2*k[1] + 2*k[2] + k[3])/6.0;
exact = actual(x);
error = fabsf(exact - y);
if(debug)
printf("%3.1f, %7.3f, %7.3f, %7.3f\r\n", x,y,exact,error); // output values for debuging
target_y = runge_kutta(h,target_x,x,y,func,actual,debug); // do it again...
}
first = true; // reset the first flag
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