-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathExample3.java
More file actions
68 lines (56 loc) · 1.69 KB
/
Example3.java
File metadata and controls
68 lines (56 loc) · 1.69 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
package symjava.examples;
import static symjava.symbolic.Symbol.*;
import symjava.relational.Eq;
import symjava.symbolic.*;
public class Example3 {
/**
* Square root of a number
* (http://en.wikipedia.org/wiki/Newton's_method)
*/
public static void example1() {
Expr[] freeVars = {x};
double num = 612;
Eq[] eq = new Eq[] {
new Eq(x*x-num, C0, freeVars)
};
double[] guess = new double[]{ 10 };
Newton.solve(eq, guess, 100, 1e-3);
}
/**
* Example from Wikipedia
* (http://en.wikipedia.org/wiki/Gauss-Newton_algorithm)
*
* Use Lagrange Multipliers and Newton method to fit a given model y=a*x/(b-x)
*
*/
public static void example2() {
//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), C0, 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};
LagrangeMultipliers lm = new LagrangeMultipliers(eq, initialGuess, data);
//Just for purpose of displaying summation expression
Eq L = lm.getEqForDisplay();
System.out.println("L("+SymPrinting.join(L.getUnknowns(),",")+")=\n "+L.lhs);
System.out.println("where data array is (X_i, Y_i), i=0..."+(data.length-1));
NewtonOptimization.solve(L, lm.getInitialGuess(), 100, 1e-4, true);
Eq L2 = lm.getEq();
System.out.println("L("+SymPrinting.join(L.getUnknowns(),",")+")=\n "+L2.lhs);
NewtonOptimization.solve(L2, lm.getInitialGuess(), 100, 1e-4, false);
}
public static void main(String[] args) {
example1();
example2();
}
}