SDDSlib
Loading...
Searching...
No Matches
minc.c File Reference

Finds the minimum of a multi-parameter function with parameter constraints. More...

#include "mdb.h"

Go to the source code of this file.

Functions

double minc (double *fn, double *x, double *dx, double *dx_lim, double *xlo, double *xhi, long np, long ns_max, long p_flag)
 Finds the minimum of a multi-parameter function with constraints.
 

Detailed Description

Finds the minimum of a multi-parameter function with parameter constraints.

This file contains the implementation of the minc() function, which searches for the minimum of a multi-parameter function while respecting constraints on the parameter ranges.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file minc.c.

Function Documentation

◆ minc()

double minc ( double * fn,
double * x,
double * dx,
double * dx_lim,
double * xlo,
double * xhi,
long np,
long ns_max,
long p_flag )

Finds the minimum of a multi-parameter function with constraints.

The minc() function searches for the minimum value of a user-defined multi-parameter function, allowing constraints on the range of each parameter. It iteratively adjusts the parameters within specified limits to find the function's minimum value.

Parameters
fnPointer to the function to be minimized.
xArray of starting values for the parameters.
dxArray of step sizes for each parameter.
dx_limArray of step size limits for each parameter.
xloArray of lower bounds for the parameters.
xhiArray of upper bounds for the parameters.
npNumber of parameters.
ns_maxMaximum number of steps to take before increasing the step size.
p_flagIf >= 0, prints information during the minimization process.
Returns
The minimum value of the function.

Definition at line 40 of file minc.c.

49{
50 register long i, n_steps, pc;
51 register double f0, f1, _dx;
52 long flag, *constraint, at_upper, at_lower;
53
54 pc = 0;
55 constraint = tmalloc(sizeof(long) * np);
56 for (i = 0; i < np; i++)
57 constraint[i] = xlo[i] != xhi[i];
58
59 f0 = (*fn)(x);
60
61 do {
62 for (i = flag = 0; i < np; i++) {
63 if (fabs(_dx = dx[i]) < dx_lim[i]) {
64 flag++;
65 continue;
66 }
67 x[i] += _dx;
68 if (constraint[i]) {
69 if (x[i] < xlo[i]) {
70 x[i] = xlo[i] + 2 * (dx[i] = fabs(_dx) / 2);
71 continue;
72 }
73 if (x[i] > xhi[i]) {
74 x[i] = xhi[i] + 2 * (dx[i] = -fabs(_dx) / 2);
75 continue;
76 }
77 }
78 f1 = (*fn)(x);
79 n_steps = 0;
80 if (f1 > f0) {
81 dx[i] = _dx = -_dx;
82 x[i] += 2 * _dx;
83 if (constraint[i]) {
84 if (x[i] < xlo[i]) {
85 x[i] = xlo[i] + 2 * (dx[i] = fabs(_dx) / 2);
86 continue;
87 }
88 if (x[i] > xhi[i]) {
89 x[i] = xhi[i] + 2 * (dx[i] = -fabs(_dx) / 2);
90 continue;
91 }
92 }
93 f1 = (*fn)(x);
94 }
95 while (f1 < f0) {
96 if (n_steps++ == ns_max) {
97 n_steps = 0;
98 dx[i] = _dx = 2 * _dx;
99 }
100 f0 = f1;
101 x[i] += _dx;
102 if (constraint[i]) {
103 if (x[i] < xlo[i]) {
104 x[i] = xlo[i] + 2 * (dx[i] = fabs(_dx));
105 break;
106 }
107 if (x[i] > xhi[i]) {
108 x[i] = xhi[i] + 2 * (dx[i] = -fabs(_dx));
109 break;
110 }
111 }
112 f1 = (*fn)(x);
113 }
114 dx[i] /= 2;
115 x[i] -= _dx;
116 }
117 if (pc++ == p_flag) {
118 printf("%.16le\n", f0);
119 for (i = 0; i < np; i++)
120 printf("%.16le\t%.16le\n", x[i], dx[i]);
121 pc = 0;
122 }
123 } while (flag != np);
124 return (f0);
125}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59