#pragma once
#include <vector>
using namespace std;
typedef struct Point
{
int n;
long double derivative, x, y;
} POINT, *PPOINT;
class RungeKutta4
{
private:
static long double FX(
long double x,
long double y);
static long double DFDX(
long double x,
long double y,
long double yp);
public:
static void ComputeEuler(
int nSteps,
long double x0,
long double x1,
long double y0,
vector<Point>& pts);
static void ComputeRK2(
int nSteps,
long double x0,
long double x1,
long double y0,
vector<Point>& pts);
static void ComputeRK4(
int nSteps,
long double x0,
long double x1,
long double y0,
vector<Point>& pts);
static void ComputeTaylor(
int nSteps,
long double x0,
long double x1,
long double y0,
vector<Point>& pts);
};
// Translated from FORTRAN 77 source code found
// in "Elementary Numerical Analysis:
// An Algorithmic Approach" by S. D. Conte and Carl de
// Boor. Translator: James Pate Williams, Jr. (c)
// August 20, 2023
#include "RungeKutta4.h"
long double RungeKutta4::FX(
long double x, long double y)
{
long double x2 = x * x;
long double term1 = 1.0 / x2;
long double term2 = -y / x;
long double term3 = -y * y;
return term1 + term2 + term3;
}
long double RungeKutta4::DFDX(
long double x,
long double y,
long double yp)
{
long double x3 = x * x * x;
long double term1 = -2.0 / x3;
long double term2 = -yp / x;
long double term3 = y / x / x;
long double term4 = -2.0 * y * yp;
return term1 + term2 + term3 + term4;
}
void RungeKutta4::ComputeEuler(
int nSteps,
long double x0,
long double x1,
long double y0,
vector<Point>& pts)
{
Point pt0 = {};
pt0.n = 0;
pt0.x = x0;
pt0.y = y0;
double derivative = FX(x0, y0);
pt0.derivative = derivative;
pts.push_back(pt0);
if (nSteps == 1)
return;
long double h = (x1 - x0) / nSteps;
long double xn = x0;
long double yn = y0;
for (int n = 1; n <= nSteps; n++)
{
xn = x0 + n * h;
long double yn = y0 + h * FX(xn, y0);
derivative = FX(xn, y0);
pt0.n = n;
pt0.derivative = derivative;
pt0.x = xn;
pt0.y = yn;
pts.push_back(pt0);
y0 = yn;
}
}
void RungeKutta4::ComputeRK2(
int nSteps,
long double x0,
long double x1,
long double y0,
vector<Point>& pts)
{
Point pt0 = {};
pt0.n = 0;
pt0.x = x0;
pt0.y = y0;
double derivative = FX(x0, y0);
pt0.derivative = derivative;
pts.push_back(pt0);
if (nSteps == 1)
return;
long double h = (x1 - x0) / nSteps;
long double xn = x0;
long double yn = y0;
for (int n = 1; n <= nSteps; n++)
{
long double k1 = h * FX(xn, yn);
long double k2 = h * FX(xn + h, yn + k1);
yn += 0.5 * (k1 + k2);
xn = x0 + n * h;
derivative = FX(xn, yn);
pt0.n = n;
pt0.derivative = derivative;
pt0.x = xn;
pt0.y = yn;
pts.push_back(pt0);
}
}
void RungeKutta4::ComputeRK4(
int nSteps,
long double x0,
long double x1,
long double y0,
vector<Point>& pts)
{
Point pt0 = {};
pt0.n = 0;
pt0.x = x0;
pt0.y = y0;
double derivative = FX(x0, y0);
pt0.derivative = derivative;
pts.push_back(pt0);
if (nSteps == 1)
return;
long double h = (x1 - x0) / nSteps;
long double xn = x0;
long double yn = y0;
for (int n = 1; n <= nSteps; n++)
{
long double k1 = h * FX(xn, yn);
long double k2 = h * FX(xn + h / 2, yn + k1 / 2);
long double k3 = h * FX(xn + h / 2, yn + k2 / 2);
long double k4 = h * FX(xn + h, yn + k3);
xn = x0 + n * h;
yn = yn + (k1 + k2 + k3 + k4) / 6.0;
derivative = FX(xn, yn);
pt0.n = n;
pt0.derivative = derivative;
pt0.x = xn;
pt0.y = yn;
pts.push_back(pt0);
}
}
void RungeKutta4::ComputeTaylor(
int nSteps,
long double x0,
long double x1,
long double y0,
vector<Point>& pts)
{
Point pt0 = {};
pt0.n = 0;
pt0.x = x0;
pt0.y = y0;
double derivative = FX(x0, y0);
pt0.derivative = derivative;
pts.push_back(pt0);
if (nSteps == 1)
return;
long double h = (x1 - x0) / nSteps;
long double xn = x0;
long double yn = y0;
long double yp = FX(x0, y0);
for (int n = 1; n <= nSteps; n++)
{
xn = x0 + n * h;
yp = FX(xn, y0);
long double yn = y0 + h * (FX(xn, y0) + h * DFDX(xn, y0, yp) / 2);
derivative = FX(xn, y0);
pt0.n = n;
pt0.derivative = derivative;
pt0.x = xn;
pt0.y = yn;
pts.push_back(pt0);
y0 = yn;
}
}
My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.
View all posts by jamespatewilliamsjr