Path: seismo!harvard!talcott!panda!sources-request From: sources-request@panda.UUCP Newsgroups: mod.sources Subject: Simplex Curve Fitting Algorithm in C Message-ID: <1430@panda.UUCP> Date: 23 Feb 86 01:57:44 GMT Sender: jpn@panda.UUCP Lines: 454 Keywords: simplex C Approved: jpn@panda.UUCP Mod.sources: Volume 4, Issue 2 Submitted by: panda!genrad!decvax!ihnp4!chinet!blm This program originally appeared in the May 1984 issue of Byte Magazine. It was originally written in Pascal by Marco Caceci and William Caceris at Florida State University. I have translated it to 'C'. This program is based upon the Simplex curve fitting algorithm. For a detailed descripstion of this program and it's workings see the above mentioned article. I acknowledge the work of Marco Caceci and William Caceris for writing the original Pascal program from which this is derived. The original authors explicitly stated ''no copy-right''. I've had some problems with accuracy. I've checked and rechecked my source code and I can't find anything that would account for it. I could very well be overlooking something, though. I suppose it could have to do with differences in the precision of the floating-point libraries of the authors Pascal compiler (Pascal/Z Version 4.0 CP/M) and my C compiler (Xenix 3.0). I welcome E-mail on this subject. Nevertheless, the differences in values returned between the original and mine are comparatively small. I hope this helps someone in Netland. ----- The preceding announcement was conceived in my own mind and is not necessarily the opinion of whom ever I happen to work for at the time. Name : Brad L. McKinley --- (503) 889-4321 USMail: M D R Professional Software, Inc., 915 SW 3rd Avenue, Ontario, OR 97914 Usenet: ihnp4!chinet!blm OR ihnp4!chinet!mdr!blm CIS : 70015,1205 Source: BDY171 "God created Arrakis to test the faithful." -- Maud'dib --- cut here ------ cut here ------ cut here ------ cut here --- : This is a shar archive. Extract with sh, not csh. : The rest of this file will extract: : Makefile : data sample data file : enter.c : f.c a typical function : first.c : main.c : new_vertex.c : order.c : report.c : sum_residual.c : echo extracting -- Makefile sed 's/^X//' > Makefile << E_O_F XBINARY = simplex X XSOURCES = main.c f.c sum_residual.c enter.c first.c new_vertex.c order.c report.c X XOBJECTS = main.o f.o sum_residual.o enter.o first.o new_vertex.o order.o report.o X XLIBRARIES = -lm -lc X XCFLAGS = -O XLDFLAGS = -n -s -x XLINTFLAGS = X X$(BINARY): $(OBJECTS) X @echo " loading $(BINARY)" X @ld -o $@ $(LDFLAGS) /lib/crt0.o $(OBJECTS) $(LIBRARIES) X Xlint: X lint $(LINTFLAGS) $(SOURCES) X X$(OBJECTS): simplex.h E_O_F echo extracting -- data sed 's/^X//' > data << E_O_F X100 X0.2 3 X0.1 1 X1e-6 1e-6 1e-6 X1.68 0.172 X3.33 0.250 X5.00 0.286 X6.67 0.303 X10.0 0.334 X20.0 0.384 E_O_F echo extracting -- simplex.h sed 's/^X//' > simplex.h << E_O_F X#include X#include X X#define M 2 X#define NVPP 2 X#define N M+1 X#define MNP 200 X#define ALPHA 1.0 X#define BETA 0.5 X#define GAMMA 2.0 X#define LW 5 X#define ROOT2 1.414214 X Xtypedef double vector[N]; Xtypedef double datarow[NVPP]; X Xextern int h[N], l[N]; Xextern int np, maxiter, niter; Xextern vector next, mean, error, maxerr, step, simp[N]; Xextern datarow data[MNP]; Xextern FILE *fpdata; X Xextern double f(); E_O_F echo extracting -- enter.c sed 's/^X//' > enter.c << E_O_F X#include "simplex.h" X Xenter(fname) Xchar *fname; X{ X register int i, j; X X printf("SIMPLEX Optimization --- 'C' Version\n\n"); X printf("Accessing file: %s\n\n", fname); X X fscanf(fpdata, "%d", &maxiter); X printf("maximum number of iterations = %d\n\n", maxiter); X X printf("Start Coordinates: "); X for (i=0 ; i f.c << E_O_F X#include "simplex.h" X Xdouble f(x, d) Xvector x; Xdatarow d; X{ X return (x[0]*d[0]/(x[1]+d[0])); X} E_O_F echo extracting -- first.c sed 's/^X//' > first.c << E_O_F X#include "simplex.h" X Xfirst() X{ X register int i, j; X X printf("Starting Simplex\n"); X for (j=0 ; j main.c << E_O_F X#include "simplex.h" X X#define until(x) while (!(x)) X Xint h[N], l[N]; Xint np, maxiter, niter; Xvector next, mean, error, maxerr, step, simp[N]; Xdatarow data[MNP]; XFILE *fpdata; X Xmain(argc, argv) Xint argc; Xchar *argv[]; X{ X register int i, j, done; X vector center, p, q; X X if (argc != 2) { X fprintf(stderr, "usage: simplex file_name\n", argv[1]); X exit(1); X } X X if ((fpdata = fopen(argv[1], "r")) == NULL) { X fprintf(stderr, "simplex: can't open %s\n", argv[1]); X exit(1); X } X X enter(argv[1]); X X /* First Vertex */ X sum_residual(simp[0]); X X /* Compute offset of Vertices */ X for (i=0 ; i maxerr[j]) X done = 0; X } X X } until(done || (niter == maxiter)); X X /* Average Each Parameter */ X for (i=0 ; i new_vertex.c << E_O_F X#include "simplex.h" X Xnew_vertex() X{ X register int i; X X printf(" --- %4d", niter); X for (i=0 ; i order.c << E_O_F X#include "simplex.h" X Xorder() X{ X register int i, j; X X for (j=0 ; j simp[h[j]][j]) X h[j] = i; X } X} E_O_F echo extracting -- report.c sed 's/^X//' > report.c << E_O_F X#include "simplex.h" X Xreport() X{ X register int i, j; X double y, dy, sigma; X X printf("\nProgram exited after %d iterations.\n\n", niter); X X printf("The final simplex is:\n"); X for (j=0 ; j sum_residual.c << E_O_F X#include "simplex.h" X X#define sqr(x) ((x) * (x)) X Xsum_residual(x) Xvector x; X{ X register int i; X X x[N-1] = 0.0; X X for (i=0 ; i