public static void Optim2D(Graphique G) { G.ClearWhite(); DrawFnt2D(Fnt); G.DrawAxis(); double x = Utils.RND(); double y = Utils.RND(); G.Cross(x, y, Color.White); ////////////////////////////////////////////////////// double k = 0.1; double x_prev = 0; double y_prev = 0; while ((x_prev == 0 && y_prev == 0) || (Math.Abs(x_prev) - Math.Abs(x) > zero || Math.Abs(y_prev) - Math.Abs(y) > zero)) { x_prev = x; y_prev = y; x = x_prev - (k * Fnt_dx(x_prev, y_prev)); y = y_prev - (k * Fnt_dy(x_prev, y_prev)); G.Cross(x, y, Color.White); } ////////////////////////////////////////////////////// }
public static void Optim2Dmax(Graphique G) { G.ClearWhite(); DrawFnt2D(Fntmax); G.DrawAxis(); var r = new Random(); double gamma = .01; // step size multiplier double x = r.NextDouble() - 0.5; double y = r.NextDouble() - 0.5; double z = Fntmax(x, y); /////////////////////////////////////////////////////////////////// for (int i = 0; i < 1000; i++) { if (Fntmax(x, y) == Fntv1(x, y)) { x += -gamma *dFntv1x(x, y); y += -gamma *dFntv1y(x, y); } else { x += -gamma *dFntv2x(x, y); y += -gamma *dFntv2y(x, y); } G.Cross(x, y, Color.White); } /////////////////////////////////////////////////////////////////// }
public static void GradLitteral(Graphique G) { G.ClearWhite(); DrawFnt(g, Color.Red); double x = 1; double y = g(x); G.Cross(x, y, Color.Blue); /////////////////////////////////////////////////////// double k = 0.01; double y_prev = y; while (y <= y_prev) // if y is larger than the previous y, then the minima has already been found { y_prev = y; x = x - (k * g_d(x)); y = g(x); G.Cross(x, y, Color.Blue); } /////////////////////////////////////////////////////// }
public static void GradEstimation(Graphique G) { G.ClearWhite(); DrawFnt(f, Color.Red); double x = 0.7; double y = f(x); G.Cross(x, y, Color.Blue); //////////////////////////////////////////////////////////// double eps = 0.05; double k = 0.01; // Rate of steps while (Math.Abs(x) > zero) // Keep trying to find minima that is located in x=0 { x = x - (k * f_d(x, eps)); // x progression towards minima y = f(x); G.Cross(x, y, Color.Blue); } /////////////////////////////////////////////////////// }
public static void GradLitteral(Graphique G) { G.ClearWhite(); DrawFnt(g, Color.Red); double x = 1; double gamma = 0.01; // step size multiplier double precision = 0.001; // range limit double previous_step_size = x; while (previous_step_size > precision) { double prev_x = x; x += -gamma *dg(prev_x); previous_step_size = Math.Abs(x - prev_x); G.Cross(x, g(x), Color.Blue); } }
public static void Optim2D(Graphique G) { G.ClearWhite(); DrawFnt2D(Fnt); G.DrawAxis(); var r = new Random(); double gamma = .1; // step size multiplier //double precision = .1; // range limit double x = r.NextDouble() - 0.5; double y = r.NextDouble() - 0.5; double z = Fnt(x, y); for (int i = 0; i < 30; i++) { x += -gamma *dFntx(x, y); y += -gamma *dFnty(x, y); z = Fnt(x, y); G.Cross(x, y, Color.White); } }