File: qs1dsearch_example.c

package info (click to toggle)
liquid-dsp 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,216 kB
  • sloc: ansic: 115,859; sh: 3,513; makefile: 1,350; python: 274; asm: 11
file content (41 lines) | stat: -rw-r--r-- 879 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// quad-section search in one dimension
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "liquid.h"

float utility(float _v, void * _context)
{
    float v_opt = *(float*)(_context);
    float v = _v - v_opt;
    return tanhf(v)*tanhf(v);
}

int main()
{
    // create qs1dsearch object
    float v_opt  = 0.0f;
    qs1dsearch q = qs1dsearch_create(utility, &v_opt, LIQUID_OPTIM_MINIMIZE);

    //qs1dsearch_init_bounds(q, -20, 10);
    qs1dsearch_init(q, -50);

    // run search
    unsigned int i;
    for (i=0; i<32; i++) {
        qs1dsearch_step(q);
        qs1dsearch_print(q);
    }

    // print results
    printf("%3u : u(%12.8f) = %12.4e, v_opt=%12.4e (error=%12.4e)\n",
        i,
        qs1dsearch_get_opt_v(q),
        qs1dsearch_get_opt_u(q),
        v_opt,
        v_opt - qs1dsearch_get_opt_v(q));

    qs1dsearch_destroy(q);
    return 0;
}