-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonlinear_test.cpp
More file actions
75 lines (57 loc) · 1.29 KB
/
nonlinear_test.cpp
File metadata and controls
75 lines (57 loc) · 1.29 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <iostream.h>
#include "nonlinear.h"
struct Table : Data
{
double func(Vector param, double x)
{return param[0]*exp(-sq(x/param[1])) + param[2];}
bool read_file(const char[]);
Table(){Npts = 0;}
~Table(){if (Npts != 0)delete[] x;delete[] y;}
};
int main(int Nargs, char **arg)
{
Table table;
Vector param;
Simplex simplex;
if (Nargs < 2)
{
printf("No data file specified.\n");
return 0;
}
if (table.read_file(arg[1]) == NO)
{
cout << "Unable to read data.\n";
return 0;
}
simplex.TOL = 1e-8;
param = simplex.minimize(&table);
for (int i=0; i<N; i++)
cout << param[i] << endl;
return 1;
}
bool Table::read_file(const char filename[])
{
FILE *file;
//open the data file
file = fopen(filename, "r");
if (file == NULL)
return NO;
// count the number of data points
for (Npts = 0; !feof(file); Npts++)
fscanf(file, "%*f\t%*f\n");
//allocate memory for the data
if (Npts == 0)
return NO;
x = new double[Npts];
y = new double[Npts];
if (x == NULL || y == NULL)
return NO;
// go back and actually read the data points in
rewind(file);
for (unsigned long i=0; i<Npts; i++)
fscanf(file, "%lf\t%lf\n", &x[i], &y[i]);
//close file and return
fclose(file);
return YES;
}