Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please fix the code below. The Golden Section Search method needs to run until s

ID: 3606072 • Letter: P

Question

Please fix the code below. The Golden Section Search method needs to run until sqrt[(i_1 - x_1)^2 + (i_2 - x_2)^2)] is less than or equal 10^6.

  1 import java.util.*; //a = 4.99999485000546*Math.pow(10,-7)
2
3 public class School{
4
5 public static void main(String []args){
6
7 double p=(0.50)*(3-Math.pow(5,0.5)), b_0 = 10000,a_0=0, a_1=a_0+(p)*(b_0-a_0),a_2 =a_0+(p)*(b_0-a_0) ,b_1=a_0+(1-p)*(b_0-a_0), b_2=a_0+(1-p)*(b_0-a_0), N=1;
8 double a = 4.99999485000546*Math.pow(10,-7), i_1 = 1, i_2 = 0, g_1 = -2, g_2 = 200;
9 double x_1 = i_1 - g_1 * a; //Some variant of this will be used to update x_1 & x_2
10 double x_2 = i_2 - g_2 * a , Iteration_No = 1, exp = 1;
11
12 //Step_Size_Update(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp,Iteration_No);
13 GoldenSectionSearch(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
14 }
15
16
17 // public static void Step_Size_Update(double a,double a_0,double a_1,double a_2,double b_0,double b_1,double b_2,double p,double N,double i_1,double i_2,double g_1,double g_2,double x_1,double x_2,double exp, double Iteration_No){
18
19 //while(Iteration_No<=3){
20
21 //System.out.println("# of Iterations: " + Iteration_No);
22 //System.out.println("Alpha = " + a);
23 //GoldenSectionSearch(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
24
25 /* a = 0.50*(a_1 + b_1);
26 g_1 = i_1 - a*(400*i_1*(i_1*i_1 - i_2) + 2*(i_1 -1)) ; //Some variant of this will be used to update g_1 & g_2
27 g_2 = i_2 - a*(200*(i_2 - i_1*i_1));
28 i_1 = x_1;
29 i_2 = x_2;
30
31
32 System.out.println();
33 Iteration_No++;*/
34
35 // }
36
37 //}
38
39 //Ask Dr. Stewart: How do I choose an alpha value from the interval given by Golden Section Search?
40 //Meanwhile, I will write the program using the midpoint within the interval
41 //100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1)
42 //Substitute for x_1: i_1 - g_1 * a
43 //Substitute for x_2: i_2 - g_2 * a
44
45 public class GoldenSectionSearch{
46
47 // double a,double a_0,double a_1,double a_2,double b_0,double b_1,double b_2,double p,double N,double i_1,double i_2,double g_1,double g_2,double x_1,double x_2, double exp
48
49 //while(Math.pow(1-p,exp)>=(0.50)*(Math.pow(10,-6))){
50 do {
51 double f_a_1 = 100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1);
52 double f_b_1 = 100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1);
53
54 if(f_a_1 < f_b_1){
55
56 b_0=b_1;
57 b_1=a_1;
58 a_1= a_0 + (p)*(b_0 - a_0);
59
60 //System.out.println("If: " + "a_0 = " + a_0 + " " + "b_0 = " + b_0);
61 } else {
62
63
64 a_0=a_1;
65 a_1=a_0+(p)*(b_0 - a_0);
66
67 //System.out.println("Else: " + "a_0 = " + a_0 + " " + "b_0 = " + b_0);
68
69
70
71 }
72
73 }
74
75 double a = 0.50*(a_1 + b_1);
76 double g_1 = i_1 - a*(400*i_1*(i_1*i_1 - i_2) + 2*(i_1 -1)) ; //Some variant of this will be used to update g_1 & g_2
77 double g_2 = i_2 - a*(200*(i_2 - i_1*i_1));
78 double i_1 = x_1;
79 double i_2 = x_2;
80
81 while(Math.pow(1-p,exp)>=(0.50)*(Math.pow(10,-6))){
82
83 PostGolden(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
84
85 exp++;
86
87 }
88
89 //System.out.println("Alpha equals: " + a);
90
91
92
93 public static double PostGolden(double a,double a_0,double a_1,double a_2,double b_0,double b_1,double b_2,double p,double N,double i_1,double i_2,double g_1,double g_2,double x_1,double x_2, double exp){
94
95
96
97 do{
98
99 x_1 = i_1 - g_1 * a; //Some variant of this will be used to update x_1 & x_2
100 double x_2 = i_2 - g_2 * a;
101
102
103 g_1 = i_1 - a*(400*i_1*(i_1*i_1 - i_2) + 2*(i_1 -1)) ; //Some variant of this will be used to update g_1 & g_2
104 g_2 = i_2 - a*(200*(i_2 - i_1*i_1));
105 double i_1 = x_1;
106 double i_2 = x_2;
107
108 //double [] Guess_Minus_Gradient_Product = new double [] {x_1, x_2};
109 //System.out.println(Arrays.toString(Guess_Minus_Gradient_Product));
110
111 exp++;
112
113 } while(exp<=10);
114
115
116 if (a>=0.50*Math.pow(10,-6)){
117
118
119 GoldenSectionSearch(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
120
121
122 } else{
123
124 double f_x = 100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1);
125 System.out.println("f_x = " + f_x);
126 System.out.println("x_1 = " + x_1 + " " + "x_2 = " + x_2);
127
128 }
129
130
131
132 return a;
133 }
134
135
136
137 }
138 }
139
140
141
142
143
144 // return a_0;
145
146 // }
147
148 /* public static double PostGolden(double a,double a_0,double a_1,double a_2,double b_0,double b_1,double b_2,double p,double N,double i_1,double i_2,double g_1,double g_2,double x_1,double x_2, double exp){
149
150
151
152 do{
153
154 x_1 = i_1 - g_1 * a; //Some variant of this will be used to update x_1 & x_2
155 double x_2 = i_2 - g_2 * a;
156
157
158 g_1 = i_1 - a*(400*i_1*(i_1*i_1 - i_2) + 2*(i_1 -1)) ; //Some variant of this will be used to update g_1 & g_2
159 g_2 = i_2 - a*(200*(i_2 - i_1*i_1));
160 double i_1 = x_1;
161 double i_2 = x_2;
162
163 //double [] Guess_Minus_Gradient_Product = new double [] {x_1, x_2};
164 //System.out.println(Arrays.toString(Guess_Minus_Gradient_Product));
165
166 exp++;
167
168 }while(exp<=10);
169 if (a>=0.50*Math.pow(10,-6)){
170 GoldenSectionSearch(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
171 }else{
172
173 double f_x = 100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1);
174 System.out.println("f_x = " + f_x);
175 System.out.println("x_1 = " + x_1 + " " + "x_2 = " + x_2);
176
177 }
178
179
180
181 return a;
182 }*/
183
184
185 //}

Explanation / Answer

import java.util.*; //a = 4.99999485000546*Math.pow(10,-7)
2
3 public class School{
4
5 public static void main(String []args){
6
7 double p=(0.50)*(3-Math.pow(5,0.5)), b_0 = 10000,a_0=0, a_1=a_0+(p)*(b_0-a_0),a_2 =a_0+(p)*(b_0-a_0) ,b_1=a_0+(1-p)*(b_0-a_0), b_2=a_0+(1-p)*(b_0-a_0), N=1;
8 double a = 4.99999485000546*Math.pow(10,-7), i_1 = 1, i_2 = 0, g_1 = -2, g_2 = 200;
9 double x_1 = i_1 - g_1 * a; //Some variant of this will be used to update x_1 & x_2
10 double x_2 = i_2 - g_2 * a , Iteration_No = 1, exp = 1;
11
12 //Step_Size_Update(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp,Iteration_No);
13 GoldenSectionSearch(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
14 }
15
16
17 // public static void Step_Size_Update(double a,double a_0,double a_1,double a_2,double b_0,double b_1,double b_2,double p,double N,double i_1,double i_2,double g_1,double g_2,double x_1,double x_2,double exp, double Iteration_No){
18
19 //while(Iteration_No<=3){
20
21 //System.out.println("# of Iterations: " + Iteration_No);
22 //System.out.println("Alpha = " + a);
23 //GoldenSectionSearch(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
24
25 /* a = 0.50*(a_1 + b_1);
26 g_1 = i_1 - a*(400*i_1*(i_1*i_1 - i_2) + 2*(i_1 -1)) ; //Some variant of this will be used to update g_1 & g_2
27 g_2 = i_2 - a*(200*(i_2 - i_1*i_1));
28 i_1 = x_1;
29 i_2 = x_2;
30
31
32 System.out.println();
33 Iteration_No++;*/
34
35 // }
36
37 //}
38
39 //Ask Dr. Stewart: How do I choose an alpha value from the interval given by Golden Section Search?
40 //Meanwhile, I will write the program using the midpoint within the interval
41 //100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1)
42 //Substitute for x_1: i_1 - g_1 * a
43 //Substitute for x_2: i_2 - g_2 * a
44
45 public class GoldenSectionSearch{
46
47 // double a,double a_0,double a_1,double a_2,double b_0,double b_1,double b_2,double p,double N,double i_1,double i_2,double g_1,double g_2,double x_1,double x_2, double exp
48
49 //while(Math.pow(1-p,exp)>=(0.50)*(Math.pow(10,-6))){
50 do {
51 double f_a_1 = 100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1);
52 double f_b_1 = 100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1);
53
54 if(f_a_1 < f_b_1){
55
56 b_0=b_1;
57 b_1=a_1;
58 a_1= a_0 + (p)*(b_0 - a_0);
59
60 //System.out.println("If: " + "a_0 = " + a_0 + " " + "b_0 = " + b_0);
61 } else {
62
63
64 a_0=a_1;
65 a_1=a_0+(p)*(b_0 - a_0);
66
67 //System.out.println("Else: " + "a_0 = " + a_0 + " " + "b_0 = " + b_0);
68
69
70
71 }
72
73 }
74
75 double a = 0.50*(a_1 + b_1);
76 double g_1 = i_1 - a*(400*i_1*(i_1*i_1 - i_2) + 2*(i_1 -1)) ; //Some variant of this will be used to update g_1 & g_2
77 double g_2 = i_2 - a*(200*(i_2 - i_1*i_1));
78 double i_1 = x_1;
79 double i_2 = x_2;
80
81 while(Math.pow(1-p,exp)>=(0.50)*(Math.pow(10,-6))){
82
83 PostGolden(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
84
85 exp++;
86
87 }
88
89 //System.out.println("Alpha equals: " + a);
90
91
92
93 public static double PostGolden(double a,double a_0,double a_1,double a_2,double b_0,double b_1,double b_2,double p,double N,double i_1,double i_2,double g_1,double g_2,double x_1,double x_2, double exp){
94
95
96
97 do{
98
99 x_1 = i_1 - g_1 * a; //Some variant of this will be used to update x_1 & x_2
100 double x_2 = i_2 - g_2 * a;
101
102
103 g_1 = i_1 - a*(400*i_1*(i_1*i_1 - i_2) + 2*(i_1 -1)) ; //Some variant of this will be used to update g_1 & g_2
104 g_2 = i_2 - a*(200*(i_2 - i_1*i_1));
105 double i_1 = x_1;
106 double i_2 = x_2;
107
108 //double [] Guess_Minus_Gradient_Product = new double [] {x_1, x_2};
109 //System.out.println(Arrays.toString(Guess_Minus_Gradient_Product));
110
111 exp++;
112
113 } while(exp<=10);
114
115
116 if (a>=0.50*Math.pow(10,-6)){
117
118
119 GoldenSectionSearch(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
120
121
122 } else{
123
124 double f_x = 100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1);
125 System.out.println("f_x = " + f_x);
126 System.out.println("x_1 = " + x_1 + " " + "x_2 = " + x_2);
127
128 }
129
130
131
132 return a;
133 }
134
135
136
137 }
138 }
139
140
141
142
143
144 // return a_0;
145
146 // }
147
148 /* public static double PostGolden(double a,double a_0,double a_1,double a_2,double b_0,double b_1,double b_2,double p,double N,double i_1,double i_2,double g_1,double g_2,double x_1,double x_2, double exp){
149
150
151
152 do{
153
154 x_1 = i_1 - g_1 * a; //Some variant of this will be used to update x_1 & x_2
155 double x_2 = i_2 - g_2 * a;
156
157
158 g_1 = i_1 - a*(400*i_1*(i_1*i_1 - i_2) + 2*(i_1 -1)) ; //Some variant of this will be used to update g_1 & g_2
159 g_2 = i_2 - a*(200*(i_2 - i_1*i_1));
160 double i_1 = x_1;
161 double i_2 = x_2;
162
163 //double [] Guess_Minus_Gradient_Product = new double [] {x_1, x_2};
164 //System.out.println(Arrays.toString(Guess_Minus_Gradient_Product));
165
166 exp++;
167
168 }while(exp<=10);
169 if (a>=0.50*Math.pow(10,-6)){
170 GoldenSectionSearch(a,a_0,a_1,a_2,b_0,b_1,b_2,p,N,i_1,i_2,g_1,g_2,x_1,x_2,exp);
171 }else{
172
173 double f_x = 100*(x_2 - x_1*x_1)*(x_2 - x_1*x_1)+ (1-x_1)*(1-x_1);
174 System.out.println("f_x = " + f_x);
175 System.out.println("x_1 = " + x_1 + " " + "x_2 = " + x_2);
176
177 }
178
179
180
181 return a;
182 }*/
183
184
185 //}