forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.java
More file actions
80 lines (67 loc) · 1.82 KB
/
Example2.java
File metadata and controls
80 lines (67 loc) · 1.82 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
76
77
78
79
80
package symjava.examples;
import symjava.relational.Eq;
import symjava.symbolic.Symbol;
import static symjava.symbolic.Symbol.*;
public class Example2 {
/**
* Example from Wikipedia
* (http://en.wikipedia.org/wiki/Gauss-Newton_algorithm)
*
* Use Gauss-Newton algorithm to fit a given model y=a*x/(b-x)
*
*/
public static void example1() {
//Model y=a*x/(b-x), Unknown parameters: a, b
Symbol[] freeVars = {x};
Symbol[] params = {a, b};
Eq eq = new Eq(y, a*x/(b+x), freeVars, params);
//Data for (x,y)
double[][] data = {
{0.038,0.050},
{0.194,0.127},
{0.425,0.094},
{0.626,0.2122},
{1.253,0.2729},
{2.500,0.2665},
{3.740,0.3317}
};
double[] initialGuess = {0.9, 0.2};
//Here we go ...
GaussNewton.solve(eq, initialGuess, data, 100, 1e-4);
}
/**
* Example from Apache Commons Math
* (http://commons.apache.org/proper/commons-math/userguide/optimization.html)
*
* "We are looking to find the best parameters [a, b, c] for the quadratic function
*
* f(x) = a x2 + b x + c.
*
* The data set below was generated using [a = 8, b = 10, c = 16]. A random number
* between zero and one was added to each y value calculated. "
*
*/
public static void example2() {
Symbol[] freeVars = {x};
Symbol[] params = {a, b, c};
Eq eq = new Eq(y, a*x*x + b*x + c, freeVars, params);
double[][] data = {
{1 , 34.234064369},
{2 , 68.2681162306108},
{3 , 118.615899084602},
{4 , 184.138197238557},
{5 , 266.599877916276},
{6 , 364.147735251579},
{7 , 478.019226091914},
{8 , 608.140949270688},
{9 , 754.598868667148},
{10, 916.128818085883},
};
double[] initialGuess = {1, 1, 1};
GaussNewton.solve(eq, initialGuess, data, 100, 1e-4);
}
public static void main(String[] args) {
example1();
example2();
}
}