// ThreeIntegrals.cpp
// Copyright (c) Sunday, October 11, 2026
// by James Pate Williams, Jr., BA, BS, MSwE, PhD
// Integrals due to Gauss, Cauchy, and Dirichlet
#include <chrono>
#include <iomanip>
#include <iostream>
#include <vector>
static double Gauss(double x)
{
return exp(-x * x);
}
static double Cauchy(double x)
{
return 1.0 / (x * x + 1.0);
}
static double Dirichlet(double x)
{
return sin(x) / x;
}
static double SimpsonsRule(
double a, double b, double(*fx)(double), int nSteps)
{
double h = (b - a) / nSteps;
double h2 = 2.0 * h;
double s = 0.0;
double t = 0.0;
double x = a + h;
for (int i = 1; i < nSteps; i += 2)
{
s += fx(x);
x += h2;
}
x = a + h2;
for (int i = 2; i < nSteps; i += 2)
{
t += fx(x);
x += h2;
}
return h * (fx(a) + 4 * s + 2 * t + fx(b)) / 3.0;
}
static double ComputePi1(double bound)
{
return SimpsonsRule(-bound, bound, Gauss, 16384);
}
static double ComputePi2(double bound, int nSteps)
{
return 2.0 * SimpsonsRule(0.0, bound, Cauchy, nSteps);
}
static double ComputePi3(double bound, int nSteps)
{
double zero = 1.0e-32;
double integral1 = SimpsonsRule(-bound, -zero, Dirichlet, nSteps);
double integral2 = SimpsonsRule(+zero, +bound, Dirichlet, nSteps);
return integral1 + integral2;
}
static double nFactorial(int n)
{
double factorial = 1.0;
for (int i = 1; i <= n; i++)
factorial *= i;
return factorial;
}
typedef struct tagGene {
int R; int nSteps;
} Gene, * PGene;
static double HillClimber(
int fNumber, int generations, int population, Gene& gene)
{
double pi = 4.0 * atan(1.0), pi2 = 0.0, pi3 = 0.0;
std::vector<double> fitness(population);
std::vector<std::vector<Gene>> genome;
genome.resize(population);
for (int i = 0; i < population; i++)
{
genome[i].resize(1);
Gene g = { 0 };
if (fNumber == 2)
{
g.R = rand() % 100000;
g.nSteps = rand() % 100000;
double pi2 = ComputePi2(g.R, g.nSteps);
fitness[i] = fabs(pi - pi2);
if (fitness[i] <= 1.0e-15)
{
gene = g;
return pi2;
}
genome[i][0] = g;
}
else if (fNumber == 3)
{
g.R = rand() % 10000000;
g.nSteps = rand() % 10000000;
double pi3 = ComputePi3(g.R, g.nSteps);
fitness[i] = fabs(pi - pi3);
if (fitness[i] <= 1.0e-15)
{
gene = g;
return pi3;
}
genome[i][0] = g;
}
}
for (int g = 0; g < generations; g++)
{
double childFitness = DBL_MAX;
int parent1 = rand() % population;
int parent2 = rand() % population;
int child = parent1 > parent2 ? parent1 : parent2;
Gene childGene = genome[child][0];
if (fNumber == 2)
{
pi2 = ComputePi2(childGene.R, childGene.nSteps);
childFitness = fabs(pi - pi2);
if (childFitness <= 1.0e-15)
{
gene = childGene;
return childFitness;
}
}
else if (fNumber == 3)
{
pi3 = ComputePi3(childGene.R, childGene.nSteps);
childFitness = fabs(pi - pi3);
if (childFitness <= 1.0e-15)
{
gene = childGene;
return childFitness;
}
}
double maxFitness = fitness[0];
for (int i = 1; i < population; i++)
if (fitness[i] > maxFitness)
maxFitness = fitness[i];
std::vector<int> maxIndices;
for (int i = 0; i < population; i++)
if (fitness[i] == maxFitness)
maxIndices.push_back(i);
int selection = maxIndices[rand() % maxIndices.size()];
fitness[selection] = childFitness;
genome[selection][0] = childGene;
}
double minFitness = fitness[0];
for (int i = 1; i < population; i++)
if (fitness[i] < minFitness)
minFitness = fitness[i];
std::vector<int> minIndices;
for (int i = 0; i < population; i++)
if (fitness[i] == minFitness)
minIndices.push_back(i);
int selection = minIndices[rand() % minIndices.size()];
gene = genome[selection][0];
if (fNumber == 2)
return ComputePi2(gene.R, gene.nSteps);
else if (fNumber == 3)
return ComputePi3(gene.R, gene.nSteps);
else
return 0.0;
}
int main()
{
Gene gene = { 0 };
std::cout << std::fixed << std::setprecision(12);
std::cout << "Function\t\tValue of Pi\tRuntime (mu-s)" << std::endl;
auto start0 = std::chrono::high_resolution_clock::now();
double pi0 = 4.0 * atan(1.0);
auto stop0 = std::chrono::high_resolution_clock::now();
auto duration0 = std::chrono::duration_cast<std::chrono::microseconds>(stop0 - start0);
std::cout << "Pi [4 * atan(1)] = " << pi0 << '\t' << duration0.count() << std::endl;
// Gauss Integral
auto start1 = std::chrono::high_resolution_clock::now();
double pi1 = pow(ComputePi1(6.0e2), 2.0);
auto stop1 = std::chrono::high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(stop1 - start1);
std::cout << "Gauss Integral = " << pi1 << '\t' << duration1.count() << std::endl;
// Cauchy Integral
auto start2 = std::chrono::high_resolution_clock::now();
double pi2 = HillClimber(2, 200000, 3000, gene);
auto stop2 = std::chrono::high_resolution_clock::now();
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(stop2 - start2);
std::cout << "Cauchy Integral = " << pi2 << '\t' << duration2.count() << std::endl;
// Dirichlet integral
auto start3 = std::chrono::high_resolution_clock::now();
double pi3 = HillClimber(3, 50000, 1000, gene);
auto stop3 = std::chrono::high_resolution_clock::now();
auto duration3 = std::chrono::duration_cast<std::chrono::microseconds>(stop3 - start3);
std::cout << "Dirichlet Integral = " << pi3 << '\t' << duration3.count() << std::endl;
return 0;
}
Blog Entry © Sunday, October 11, 2026, by James Pate Williams, Jr.
#pragma once
class Potential
{
public:
static double Slot1SimpsonsRule(
double a, double A, double B, int n, int nSteps,
double(*fx)(double, double, int));
static double SquareExact(
double a, double b,
double x, double y,
int N);
static void ComputeIntegrals(
double a, int N, int nSteps, std::vector<double>& c);
static double Slot1Exact(
double a, double x, double y,
int N, std::vector<double>& c);
static void SquareFixedPoint(
double a, double b,
int Nx, int Ny, int iterations,
std::vector<std::vector<double>>& U);
static void Slot1FixedPoint(
double a, double b,
int Nx, int Ny, int iterations,
std::vector<std::vector<double>>& U);
static double Disk_f(double theta);
static double Disk_a0(double theta);
static double Disk_an(double c, double theta, int n);
static double Disk_bn(double c, double theta, int n);
static double SimpsonsRule_Disk_a0(int nSteps);
static double SimpsonsRule_Disk_an(
double c, int n, int nSteps);
static double SimpsonsRule_Disk_bn(
double c, int n, int nSteps);
static double Disk_u(
double c, double r, double theta,
double a0, std::vector<double>& an,
std::vector<double>& bn, int Nt,
int nSteps);
};
#include "pch.h"
#include "Potential.h"
double Potential::Slot1SimpsonsRule(
double a, double A, double B, int n, int nSteps,
double(*fx)(double, double, int))
{
double h = (B - A) / nSteps;
double h2 = 2.0 * h;
double s = 0.0;
double t = 0.0;
double x = A + h;
for (int i = 1; i < nSteps; i += 2)
{
s += fx(a, x, n);
x += h2;
}
x = A + h2;
for (int i = 2; i < nSteps; i += 2)
{
t += fx(a, x, n);
x += h2;
}
return h * (fx(a, A, n) + 4 * s + 2 * t + fx(a, B, n)) / 3.0;
}
double Potential::SquareExact(
double a, double b,
double x, double y,
int N)
{
double pi = 4.0 * atan(1.0);
double sum = 0.0;
for (int n = 1; n <= N; n++)
{
double l2 = pi * n / a;
double s1 = sin(0.5 * n * pi) / (n * n);
double s2 = sinh(l2 * y);
double s3 = sinh(l2 * (b - y));
double s4 = sinh(l2 * b);
double s5 = sin(l2 * x);
sum += s1 * ((s2 + s3) / s4) * s5;
}
return 8.0 * sum / (pi * pi);
}
double Slot1Integrand(double a, double x, int n)
{
double pi = 4.0 * atan(1.0);
return exp(-x) * sin(n * pi * x / a);
}
void Potential::ComputeIntegrals(
double a, int N, int nSteps, std::vector<double>& c)
{
c[0] = 0.0;
for (int n = 1; n <= N; n++)
{
c[n] = 2.0 * Slot1SimpsonsRule(a, 0.0, a,
n, nSteps, Slot1Integrand) / a;
}
}
double Potential::Slot1Exact(
double a, double x, double y,
int N, std::vector<double>& c)
{
double pi = 4.0 * atan(1.0), uxy = 0.0;
for (int n = 1; n <= N; n++)
uxy += c[n] * sin(n * pi * x / a) * exp(-n * pi * y / a);
return uxy;
}
static double SquareF(double x)
{
if (x > 0.0 && x < 0.5)
return 2.0 * x;
else if (x >= 0.5 && x < 1.0)
return 2.0 - 2.0 * x;
else
return 0.0;
}
void Potential::SquareFixedPoint(
double a, double b,
int Nx, int Ny, int iterations,
std::vector<std::vector<double>>& U)
{
double deltaX = a / Nx, deltaY = b / Ny;
U.resize(Nx + 1LL);
for (int i = 0; i <= Nx; i++)
U[i].resize(Ny + 1LL);
for (int i = 0; i <= Ny; i++)
U[0][i] = 0.0;
for (int i = 1; i <= Ny; i++)
U[Nx][i] = 0.0;
for (int i = 0; i <= Nx; i++)
U[i][0] = SquareF(i * deltaX);
for (int i = 0; i <= Nx; i++)
U[i][Ny] = SquareF(i * deltaX);
for (int m = 0; m < iterations; m++)
{
for (int j = 1; j < Nx; j++)
{
for (int k = 1; k < Ny; k++)
U[j][k] = 0.25 *
(U[j + 1][k] + U[j - 1][k] +
U[j][k + 1] + U[j][k - 1]);
}
}
}
void Potential::Slot1FixedPoint(
double a, double b,
int Nx, int Ny, int iterations,
std::vector<std::vector<double>>& U)
{
double deltaX = a / Nx, deltaY = b / Ny;
U.resize(Nx + 1);
for (int i = 0; i <= Nx; i++)
U[i].resize(Ny + 1, 0.0);
for (int i = 0; i <= Nx; i++)
U[i][0] = exp(-i * deltaX);
for (int i = 0; i < iterations; i++)
{
for (int j = 1; j < Nx; j++)
{
for (int k = 1; k < Ny; k++)
U[j][k] = 0.25 *
(U[j + 1][k] + U[j - 1][k] +
U[j][k + 1] + U[j][k - 1]);
}
}
}
double Potential::Disk_f(double theta)
{
return theta * theta;
}
double Potential::Disk_a0(double theta)
{
double pi = 4.0 * atan(1.0);
return Disk_f(theta);
}
double Potential::Disk_an(double c, double theta, int n)
{
double pi = 4.0 * atan(1.0);
return Disk_f(theta) * cos(n * theta) / (pi * pow(c, n));
}
double Potential::Disk_bn(double c, double theta, int n)
{
double pi = 4.0 * atan(1.0);
return Disk_f(theta) * cos(n * theta) / (pi * pow(c, n));
}
double Potential::SimpsonsRule_Disk_a0(int nSteps)
{
double pi = 4.0 * atan(1.0);
double h = (pi + pi) / nSteps;
double h2 = 2.0 * h;
double s = 0.0;
double t = 0.0;
double theta = -pi + h;
for (int i = 1; i < nSteps; i += 2)
{
s += Disk_a0(theta);
theta += h2;
}
theta = -pi + h2;
for (int i = 2; i < nSteps; i += 2)
{
t += Disk_a0(theta);
theta += h2;
}
return h * (Disk_a0(0.0) + 4 * s + 2 * t + Disk_a0(pi)) / 3.0;
}
double Potential::SimpsonsRule_Disk_an(
double c, int n, int nSteps)
{
double pi = 4.0 * atan(1.0);
double h = (pi + pi) / nSteps;
double h2 = 2.0 * h;
double s = 0.0;
double t = 0.0;
double theta = -pi + h;
for (int i = 1; i < nSteps; i += 2)
{
s += Disk_an(c, theta, n);
theta += h2;
}
theta = -pi + h2;
for (int i = 2; i < nSteps; i += 2)
{
t += Disk_an(c, theta, n);
theta += h2;
}
double integral =
h * (Disk_an(c, 0.0, n) + 4 * s + 2 * t +
Disk_an(c, pi, n)) / 3.0;
return integral / (pi * pow(c, n));
}
double Potential::SimpsonsRule_Disk_bn(
double c, int n, int nSteps)
{
double pi = 4.0 * atan(1.0);
double h = (pi + pi) / nSteps;
double h2 = 2.0 * h;
double s = 0.0;
double t = 0.0;
double theta = -pi + h;
for (int i = 1; i < nSteps; i += 2)
{
s += Disk_bn(c, theta, n);
theta += h2;
}
theta = -pi + h2;
for (int i = 2; i < nSteps; i += 2)
{
t += Disk_bn(c, theta, n);
theta += h2;
}
double integral =
h * (Disk_bn(c, 0.0, n) + 4 * s + 2 * t +
Disk_bn(c, pi, n)) / 3.0;
return integral / (pi * pow(c, n));
}
double Potential::Disk_u(
double c, double r, double theta,
double a0, std::vector<double>& an,
std::vector<double>& bn, int Nt,
int nSteps)
{
double urt = a0;
for (int n = 1; n <= Nt; n++)
{
urt += pow(r, n) * (
an[n] * cos(n * theta) +
bn[n] * sin(n * theta));
}
return urt;
}
Blog Entry © Tuesday, October 6, 2026, by James Pate Williams, Jr. Radial Basis Function Networks Results
Blog Entry © Sunday, October 4, 2026, by James Pate Williams, Jr. Riemannian Integration Tests
// Integration1dTests.cpp
// Copyright Saturday, October 3, 2026
// James Pate Williams, Jr. BA, BS, MSwE, PhD
// One-dimensional integration tests
// Our canonical integrands are x^3 exp(x) for
// x = 0 to x = 1 and x^3 sin(x) for x = 0 to x = pi
#include <iomanip>
#include <iostream>
using namespace std;
const int steps = 1024;
static double f1(double x)
{
return x * x * x * exp(x);
}
static double f2(double x)
{
return x * x * x * sin(x);
}
static double g1()
{
return -2.0 * exp(1.0) + 6;
}
static double g2()
{
double pi = 4.0 * atan(1.0);
return -pi * pi * pi * cos(pi) + 6.0 * pi * cos(pi);
}
double TrapezoidalRule(
int n, double a, double b, double(*fx)(double))
{
double h = (b - a) / n;
double s = 0.5 * (fx(a) + fx(b));
double x = a + h;
for (int i = 1; i < n; i++)
{
s += fx(x);
x += h;
}
return h * s;
}
double SimpsonsRule(
int n, double a, double b, double(*fx)(double))
{
double h = (b - a) / n;
double h2 = 2.0 * h;
double s = 0.0;
double t = 0.0;
double x = a + h;
for (int i = 1; i < n; i += 2)
{
s += fx(x);
x += h2;
}
x = a + h2;
for (int i = 2; i < n; i += 2)
{
t += fx(x);
x += h2;
}
return h * (fx(a) + 4 * s + 2 * t + fx(b)) / 3.0;
}
static void PrintResult(double x)
{
cout << setw(15) << fixed;
cout << right << setprecision(12);
cout << setfill(' ');
cout << x;
}
static void PrintError(double x, double exact)
{
cout << '\t';
cout << setw(15) << fixed;
cout << right << setprecision(12);
cout << setfill(' ');
cout << 100.0 * fabs(x - exact) /
fabs(exact) << endl;
}
int main()
{
double gAct1 = g1();
double gAct2 = g2();
double pi = 4.0 * atan(1.0);
double trap1 = TrapezoidalRule(steps, 0.0, 1.0, f1);
double simp1 = SimpsonsRule(steps, 0.0, 1.0, f1);
double trap2 = TrapezoidalRule(steps, 0.0,pi, f2);
double simp2 = SimpsonsRule(steps, 0.0, pi, f2);
cout << "Functions are: " << endl;
cout << "f1(x) = x ^ 3 * exp(x)" << endl;
cout << "f2(x) = x ^ 3 * sin(x)" << endl;
cout << "Trapezoidal and Simpson Use 1024 Steps" << endl;
cout << " Value % Error";
cout << endl;
cout << "trap1 = ";
PrintResult(trap1);
PrintError(trap1, gAct1);
cout << "simp1 = ";
PrintResult(simp1);
PrintError(simp1, gAct1);
cout << "true1 = ";
PrintResult(gAct1);
cout << endl;
cout << "trap2 = ";
PrintResult(trap2);
PrintError(trap2, gAct2);
cout << "simp2 = ";
PrintResult(simp2);
PrintError(simp2, gAct2);
cout << "true2 = ";
PrintResult(gAct2);
cout << endl;
return 0;
}
Blog Entry © Saturday, October 3, 2026, by James Pate Williams, Jr. Planck’s Law Black Body Computations (C/C++ Code Translated from a 2021 C# App)
Blog Entry © Thursday, October 1, 2026, Classical Ballistics by James Pate Williams, Jr.
Blog Entry © Monday September 28, 2026, by James Pate Williams, Jr.
Blog Entry © Monday, September 21, 2026, by James Pate Williams, Jr. Using an Evolutionary Hill Climber for Function Optimization
// Minima.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
// Copyright (c) Sunday, September 20, 2026
// by James Pate Williams, Jr., BA, BS, MSwE, PhD
// Evolutionary hill climber (c) 1999 by Pate
#include <iostream>
#include <time.h>
#include "Algorithms.h"
int main()
{
while (true)
{
double minimum = 0.0, x0 = 0.0, y0 = 0.0;
double f1 = 0.0, x = 0.0, y = 0.0;
int evaluations = 0, maxIterations = 10000;
int generation = 0, population = 0, option = 0;
unsigned int seed = 1;
std::cout << "== Menu == \r\n";
std::cout << "1 Rastrigin Function\r\n";
std::cout << "2 Ackley Function\r\n";
std::cout << "3 Sphere Function\r\n";
std::cout << "4 Rosenbrock Function\r\n";
std::cout << "5 Beale Function\r\n";
std::cout << "6 Exit\r\n";
std::cout << "Option (1 - 6): ";
std::cin >> option;
if (option == 6)
break;
std::cout << "population = ";
std::cin >> population;
std::cout << "generation = ";
std::cin >> generation;
std::cout << "seed = ";
std::cin >> seed;
srand(seed);
x0 = y0 = 10.0;
clock_t clock0 = clock();
if (option == 1)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Rastrigin,
x, y, minimum,
evaluations,
seed);
}
else if (option == 2)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Ackley,
x, y, minimum,
evaluations,
seed);
}
else if (option == 3)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Sphere,
x, y, minimum,
evaluations,
seed);
}
else if (option == 4)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Rosenbrock,
x, y, minimum,
evaluations,
seed);
}
else if (option == 5)
{
Algorithms::HillClimber(
population,
generation,
Algorithms::Beale,
x, y, minimum,
evaluations,
seed);
}
clock_t clock1 = clock();
double runtime = (double)(clock1 - clock0) /
CLOCKS_PER_SEC;
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;
std::cout << "f = " << minimum << std::endl;
std::cout << "t = " << runtime << " seconds";
std::cout << std::endl;
}
return 0;
}
#pragma once
class Algorithms
{
public:
static double Rastrigin(double x, double y);
static double Ackley(double x, double y);
static double Sphere(double x, double y);
static double Rosenbrock(double x, double y);
static double Beale(double x, double y);
static void HillClimber(
int population,
int generation,
double(*f)(double, double),
double& x, double& y,
double& minimum,
int& evaluations,
unsigned int seed);
};
#include "Algorithms.h"
#include <vector>
double Algorithms::Rastrigin(double x, double y)
{
double A = 10.0, pi = 4.0 * atan(1.0);
return
2.0 * A +
x * x - A * cos(2.0 * pi * x) +
y * y - A * cos(2.0 * pi * y);
}
double Algorithms::Ackley(double x, double y)
{
double pi = 4.0 * atan(1.0);
double xy = sqrt(0.5 * (x * x + y * y));
double cx = cos(2.0 * pi * x);
double cy = cos(2.0 * pi * y);
double expf1 = -20.0 * exp(-0.2 * xy);
double expf2 = -exp(0.5 * (cx + cy));
return expf1 + expf2 + exp(1.0) + 20.0;
}
double Algorithms::Sphere(double x, double y)
{
return x * x + y * y;
}
double Algorithms::Rosenbrock(double x, double y)
{
return 100.0 * pow(y - x * x, 2.0) + pow(1.0 - x, 2.0);
}
double Algorithms::Beale(double x, double y)
{
return
pow(1.500 - x + x * y, 2.0) +
pow(2.250 - x + x * y * y, 2.0) +
pow(2.625 - x + x * y * y * y, 2.0);
}
void Algorithms::HillClimber(
int population,
int generation,
double(*f)(double, double),
double& x, double& y,
double& minimum,
int& evaluations,
unsigned int seed)
{
srand(seed);
std::vector<double> fitness(population);
std::vector<std::vector<double>> genes(
population, std::vector<double>(2));
for (int i = 0; i < population; i++)
{
genes[i][0] = (10.0 * rand()) / RAND_MAX;
genes[i][1] = (10.0 * rand()) / RAND_MAX;
fitness[i] = f(genes[i][0], genes[i][1]);
}
for (int i = 0; i < generation; i++)
{
int parent1 = rand() % population;
int parent2 = rand() % population;
int parent0 = parent1 > parent2 ? parent1 : parent2;
int sign = rand() % 2 == 0 ? -1 : +1;
double childGeneX = (10.0 * rand()) / RAND_MAX;
double childGeneY = (10.0 * rand()) / RAND_MAX;
double childFitness = f(childGeneX, childGeneY);
double maxFitness = fitness[0];
for (int i = 1; i < population; i++)
if (fitness[i] > maxFitness)
maxFitness = fitness[i];
std::vector<int> maxIndex;
for (int i = 0; i < population; i++)
if (fitness[i] == maxFitness)
maxIndex.push_back(i);
int replace = maxIndex[rand() % maxIndex.size()];
fitness[replace] = childFitness;
genes[replace][0] = childGeneX;
genes[replace][1] = childGeneY;
}
evaluations = generation + population;
double minFitness = fitness[0];
for (int i = 1; i < population; i++)
if (fitness[i] < minFitness)
minFitness = fitness[i];
std::vector<int> minIndex;
for (int i = 0; i < population; i++)
if (fitness[i] == minFitness)
minIndex.push_back(i);
int replace = minIndex[rand() % minIndex.size()];
minimum = fitness[replace];
x = genes[replace][0];
y = genes[replace][1];
}
Blog Entry © Monday, September 14, 2026, by James Pate Williams, Jr. Cardioid Curve Graph

// Cardioid.cpp : Defines the entry point for the application.
// Copyright (c) Sunday, September 13, 2026
// by James Pate Williams, Jr. BA, BS, MSwE, PhD
// https://en.wikipedia.org/wiki/Cardioid
#include "framework.h"
#include "Cardioid.h"
#include <float.h>
#include <stdio.h>
#include <vector>
#define MAX_LOADSTRING 100
typedef struct tagPoint2d
{
double x, y;
} Point2d, * PPoint2d;
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
std::vector<Point2d> points;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_CARDIOID, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CARDIOID));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CARDIOID));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_CARDIOID);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
static void FindMinMax(
double& xMin, double& xMax,
double& yMin, double& yMax)
{
// uses global 2D double points structure
xMin = yMin = DBL_MAX;
xMax = yMax = DBL_MIN;
for (size_t i = 0; i < points.size(); i++)
{
Point2d pt = points[i];
double x = pt.x;
double y = pt.y;
if (x < xMin)
xMin = x;
if (x > xMax)
xMax = x;
if (y < yMin)
yMin = y;
if (y > yMax)
yMax = y;
}
}
static void DrawFormattedText(HDC hdc, char text[], RECT rect)
{
// Draw the text with formatting options
DrawTextA(hdc, text, -1, &rect, DT_SINGLELINE | DT_NOCLIP);
}
static void CreateCurve(double a, int n)
{
double pi = 4.0 * atan(1.0), phi = 0.0;
while (phi < 2.0 * pi)
{
double phi1 = 1.0 - cos(phi);
double y = 2.0 * a * phi1 * cos(phi);
double x = 2.0 * a * phi1 * sin(phi);
Point2d pt = { 0, 0 };
pt.x = x;
pt.y = y;
points.push_back(pt);
phi += 0.001;
}
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
CreateCurve(1.0, 256);
double h = 0, pi = 0, plm = 0, theta = 0;
double xMax = 0, xMin = 0, yMax = 0, yMin = 0;
FindMinMax(xMin, xMax, yMin, yMax);
float xSpan = (float)(xMax - xMin);
float ySpan = (float)(yMax - yMin);
RECT rect = { };
GetClientRect(hWnd, &rect);
float width = (float)(rect.right - rect.left + 1);
float height = (float)(rect.bottom - rect.top - 32 + 1);
float sx0 = 2.0f * width / 16.0f;
float sx1 = 14.0f * width / 16.0f;
float sy0 = 2.0f * height / 16.0f;
float sy1 = 14.0f * height / 16.0f;
float deltaX = xSpan / 8.0f;
float deltaY = ySpan / 8.0f;
float xSlope = (sx1 - sx0) / xSpan;
float xInter = (float)(sx0 - xSlope * xMin);
float ySlope = (sy0 - sy1) / ySpan;
float yInter = (float)(sy0 - ySlope * yMax);
float px = 0, py = 0, sx = 0, sy = 0;
PAINTSTRUCT ps;
POINT wPt = { };
HDC hdc = BeginPaint(hWnd, &ps);
int i = 0;
float x = (float)xMin;
float y = (float)yMax;
px = x;
py = y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
MoveToEx(hdc, (int)sx, (int)sy0, &wPt);
char buffer[128] = { };
while (i <= 8)
{
sx = xSlope * x + xInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy0, &wPt);
LineTo(hdc, (int)sx, (int)sy1);
sprintf_s(buffer, "%5.4lf", x);
SIZE size = { };
GetTextExtentPoint32A(
hdc,
buffer,
(int)strlen(buffer),
&size);
RECT textRect = { };
textRect.left = (long)(sx - size.cx / 2.0f);
textRect.right = (long)(sx + size.cx / 2.0f);
textRect.top = (long)sy1;
textRect.bottom = (long)(sy1 + size.cy / 2.0f);
DrawFormattedText(hdc, buffer, textRect);
x += deltaX;
i++;
}
i = 0;
y = (float)yMin;
while (i <= 8)
{
sy = ySlope * y + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx0, (int)sy, &wPt);
LineTo(hdc, (int)sx, (int)sy);
if (i != 0)
{
sprintf_s(buffer, "%+5.3lf", y);
SIZE size = { };
GetTextExtentPoint32A(
hdc,
buffer,
(int)strlen(buffer),
&size);
RECT textRect = { };
textRect.left = (long)(sx0 - size.cx - size.cx / 5.0f);
textRect.right = (long)(sx0 - size.cx / 2.0f);
textRect.top = (long)(sy - size.cy / 2.0f);
textRect.bottom = (long)(sy + size.cy / 2.0f);
DrawFormattedText(hdc, buffer, textRect);
}
y += deltaY;
i++;
}
HGDIOBJ bPenNew = NULL;
HGDIOBJ hPenOld = NULL;
bPenNew = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
hPenOld = SelectObject(hdc, bPenNew);
px = (float)points[0].x;
py = (float)points[0].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
wPt.x = wPt.y = 0;
MoveToEx(hdc, (int)sx, (int)sy, &wPt);
for (size_t j = 1; j < points.size(); j++)
{
px = (float)points[j].x;
py = (float)points[j].y;
sx = xSlope * px + xInter;
sy = ySlope * py + yInter;
LineTo(hdc, (int)sx, (int)sy);
}
SelectObject(hdc, hPenOld);
DeleteObject(bPenNew);
return (INT_PTR)FALSE;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
Blog Entry © Monday – Thursday, September 7 – 10, 2026, by James Pate Williams, Jr. Solutions of Three Nonhomogeneous Second Order Linear Ordinary Differential Equation Boundary Value Problems and a Series Solution of the Bessel Function of the First Kind of Integer Eigenvalue (Order) of Zero
// FiniteDifference.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
// Copyright (c) Wednesday, August 9, 2026
// by James Pate Williams, Jr., BA, BS, MSwE, PhD
// Reference: "Elementary Numerical Analysis an Algorithmic Approach
// Third Edition" © 1980 by S. D. Conte and Carl de Boor
#include <math.h>
#include <stdio.h>
#define MAX_ROWS 8192
double ac[MAX_ROWS];
double fv[MAX_ROWS];
double gv[MAX_ROWS];
double qv[MAX_ROWS];
double xv[MAX_ROWS];
double yv[MAX_ROWS];
double ta[MAX_ROWS];
double tb[MAX_ROWS];
double td[MAX_ROWS];
double tc[MAX_ROWS];
double tx[MAX_ROWS];
int SolveTridiagonal(int n)
{
for (int k = 2; k <= n; k++)
{
if (td[k - 1] == 0)
return 0;
double m = ta[k] / td[k - 1];
td[k] -= m * tc[k - 1];
tb[k] -= m * tb[k - 1];
}
if (td[n] == 0)
return 0;
tx[n] = tb[n] / td[n];
for (int k = n - 1; k >= 1; k--)
tx[k] = (tb[k] - tc[k] * tx[k + 1]) / td[k];
return 1;
}
double f1(double x)
{
return 0.0;
}
double g1(double x)
{
return -1.0;
}
double q1(double x)
{
return 0.0;
}
double f2(double x)
{
return 0.0;
}
double g2(double x)
{
return 1.0;
}
double q2(double x)
{
return 0.0;
}
double f3(double x)
{
return x;
}
double g3(double x)
{
return 1.0;
}
double q3(double x)
{
return 2.0 * x;
}
double f4(double x)
{
return 2.0;
}
double g4(double x)
{
return 1.0;
}
double q4(double x)
{
return x;
}
double f5(double x)
{
return 1.0 / x;
}
double g5(double x)
{
return 1.0;
}
double q5(double x)
{
return 0.0;
}
double FD_Solution(
double a,
double b,
double ya,
double yb,
double (*f)(double),
double (*g)(double),
double (*q)(double),
int N)
{
double h = (b - a) / N;
for (int i = 1; i <= N - 1; i++)
{
xv[i] = a + i * h;
fv[i] = f(xv[i]);
gv[i] = g(xv[i]);
qv[i] = q(xv[i]);
}
tb[1] = h * h * qv[1] - (1.0 - 0.5 * h * fv[1]) * ya;
for (int i = 2; i <= N - 1; i++)
tb[i] = h * h * qv[i];
for (int i = 1; i <= N - 1; i++)
td[i] = -2.0 + h * h * gv[i];
for (int i = 1; i <= N - 2; i++)
tc[i] = 1.0 + 0.5 * h * fv[i];
for (int i = 2; i <= N - 1; i++)
ta[i] = 1.0 - 0.5 * h * fv[i];
tb[N - 1] = h * h * qv[N - 1] - (1.0 + 0.5 * h * fv[N - 1]) * yb;
return SolveTridiagonal(N - 1);
}
double IS_Solution(
double a,
double b,
double ya,
double yb,
double x)
{
double A[13] = { 0 };
A[0] = 1.0;
A[1] = 0.0;
A[2] = -0.25;
A[3] = 0.0;
A[4] = A[0] / 64.0;
A[5] = 0.0;
A[6] = -A[0] / (36.0 * 64.0);
A[7] = 0.0;
A[8] = A[0] / (64.0 * 36.0 * 64.0);
A[9] = 0.0;
A[10] = -A[8] / 100.0;
A[11] = 0.0;
A[12] = -A[10] / 144.0;
double s = A[12];
for (int i = 11; i >= 0; i--)
s = s * x + A[i];
return s;
}
int main()
{
double h = 0.05, x0 = 0.0, x1 = 1.0, y0 = 0, y1 = 1.0;
int N = (int)((x1 - x0) / h);
double fd1 = FD_Solution(
x0, x1, y0, y1, f1, g1, q1, N);
FILE* file = 0;
int errno = fopen_s(&file, "Chapter9.txt", "w");
if (errno != 0)
return -1;
fprintf_s(file, "Example 9.1\r\n");
fprintf_s(file, " x\tapproximate\texact\t\tpercent error\r\n");
for (int i = 1; i < N; i++)
{
double exact = sinh(xv[i]) / sinh(1.0);
double error = 100.0 * (fabs(tx[i] - exact) / fabs(exact));
fprintf_s(file, "%3.2lf\t%11.10lf\t%11.10lf\t%11.10lf\r\n",
xv[i], tx[i], exact, error);
}
h = 0.25;
x0 = 0.0, x1 = 1.0, y0 = 0, y1 = 1.0;
N = (int)((x1 - x0) / h);
double fd2 = FD_Solution(
x0, x1, y0, y1, f2, g2, q2, N);
fprintf_s(file, "Exercise 9.1-1\r\n");
fprintf_s(file, " x\tapprox\r\n");
for (int i = 1; i < N; i++)
fprintf_s(file, "%3.2lf\t%5.4lf\r\n", xv[i], tx[i]);
h = 0.1;
x0 = 0.0, x1 = 1.0, y0 = 1.0, y1 = 0.0;
N = (int)((x1 - x0) / h);
double fd3 = FD_Solution(
x0, x1, y0, y1, f3, g3, q3, N);
fprintf_s(file, "Exercise 9.1-3\r\n");
fprintf_s(file, " x\t approximate\r\n");
for (int i = 1; i < N; i++)
fprintf_s(file, "%3.2lf\t%11.10lf\r\n", xv[i], tx[i]);
h = 1.0 / 16.0;
x0 = 0.0, x1 = 1.0, y0 = 0.0, y1 = 1.0;
N = (int)((x1 - x0) / h);
double fd4 = FD_Solution(
x0, x1, y0, y1, f4, g4, q4, N);
fprintf_s(file, "Exercise 9.1-4\r\n");
fprintf_s(file, " x\t approximate\r\n");
for (int i = 1; i < N; i++)
fprintf_s(file, "%3.2lf\t%11.10lf\r\n", xv[i], tx[i]);
h = 1.0 / 16.0;
x0 = 0.0, x1 = 2.0, y0 = 1.0, y1 = 2.238907555e-01;
N = (int)((x1 - x0) / h);
double fd5 = FD_Solution(
x0, x1, y0, y1, f5, g5, q5, N);
fprintf_s(file, "Bessel Equation of Order Zero\r\n");
fprintf_s(file, "Approximate Finite Difference Solution\r\n");
fprintf_s(file, " x\t approximate\r\n");
for (int i = 1; i < N; i++)
fprintf_s(file, "%3.2lf\t%11.10lf\r\n", xv[i], tx[i]);
fprintf_s(file, "Bessel Equation of Order Zero\r\n");
fprintf_s(file, "Approximate Infinite Series Solution\r\n");
fprintf_s(file, " x\t approximate\r\n");
for (int i = 1; i < N; i++)
{
double x = x0 + i * h;
fprintf_s(file, "%3.2lf\t%11.10lf\r\n", x,
IS_Solution(x0, x1, y0, y1, x));
}
return 0;
}