D Am Em G C Variation
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;
}
Blog Entry © Friday, August 28, 2026, by James Pate Williams, Jr. New Cryptography Project Continued Basic RSA Functions
References: Guide to Elliptic Curve Cryptography © 2004 by Darrel Hankerson, Alfred Menezes, and Scott Vanstone, Handbook of Applied Cryptography © 1997 by A. Menezes, P. van Oorschot and S. Vanstone