
ИДЗ / МО ИДЗ вариант А
.pdfПриложение А Метод золотого сечения
decimal a0 = 0; decimal b0 = 10;
decimal alpha = 0.618m; decimal beta = 0.382m; decimal eps = 0.001m; int iterationsGR = 0;
decimal Function1(decimal x)
{
decimal y1 = 8 - Convert.ToDecimal(Math.Exp(-Math.Pow(((Convert.ToDouble(x) - 5) / 2), 2)));
return y1;
}
decimal Function2(decimal x)
{
decimal y2 = 3 * (x - 7) * (x - 5) * (x - 3); return y2;
}
decimal Function3(decimal x)
{
decimal y3 = (x / 2) + 5 * Convert.ToDecimal(Math.Sin(5 * Math.PI * Convert.ToDouble(x) + 4));
return y3;
}
decimal delta = b0 - a0; void GoldenRatio()
{
decimal x_alpha = a0 + (alpha * delta);
21
decimal x_beta = a0 + (beta * delta);
decimal f_alpha = Function1(x_alpha);
decimal f_beta = Function1(x_beta);
//decimal f_alpha = Function2(x_alpha);
//decimal f_beta = Function2(x_beta);
//decimal f_alpha = Function3(x_alpha);
//decimal f_beta = Function3(x_beta);
if (f_beta >= f_alpha)
{
a0 = x_beta;
}
else
{
b0 = x_alpha;
}
delta = delta * alpha;
if (delta < eps)
{
Console.WriteLine();
Console.WriteLine($"Отрезок [a; b]: [0; 10]");
Console.WriteLine($"x_alpha = {decimal.Round(x_alpha, 3,
MidpointRounding.AwayFromZero)}; x_beta = {decimal.Round(x_beta, 3,
MidpointRounding.AwayFromZero)}");
22
Console.WriteLine($"y_alpha = {decimal.Round(f_alpha, 3,
MidpointRounding.AwayFromZero)}; y_beta = {decimal.Round(f_beta, 3,
MidpointRounding.AwayFromZero)}");
Console.WriteLine($"Количество итераций: {iterationsGR}");
}
else
{
iterationsGR++;
Console.Write($"x_alpha = {decimal.Round(x_alpha, 3,
MidpointRounding.AwayFromZero)}; x_beta = {decimal.Round(x_beta, 3,
MidpointRounding.AwayFromZero)} ");
Console.Write($"y_alpha = {decimal.Round(f_alpha, 3,
MidpointRounding.AwayFromZero)}; y_beta = {decimal.Round(f_beta, 3,
MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iterationsGR}");
GoldenRatio();
}
}
GoldenRatio();
23
Приложение Б Метод дихотомии
decimal a0 = 0; decimal b0 = 10; decimal eps = 0.001m;
decimal kovshik = 0.0006m; int iterationsD = 0;
decimal Function1(decimal x)
{
decimal y1 = 8 - Convert.ToDecimal(Math.Exp(-Math.Pow(((Convert.ToDouble(x) - 5) / 2), 2)));
return y1;
}
decimal Function2(decimal x)
{
decimal y2 = 3 * (x - 7) * (x - 5) * (x - 3); return y2;
}
decimal Function3(decimal x)
{
decimal y3 = (x / 2) + 5 * Convert.ToDecimal(Math.Sin(5 * Math.PI * Convert.ToDouble(x) + 4));
return y3;
}
decimal delta = b0 - a0;
void Dichotomy()
{
24
decimal x1 = (a0 + b0 - kovshik) / 2;
decimal x2 = (a0 +b0 + kovshik) / 2;
decimal f1 = Function1(x1);
decimal f2 = Function1(x2);
//decimal f1 = Function2(x1);
//decimal f2 = Function2(x2);
//decimal f1 = Function3(x1);
//decimal f2 = Function3(x2);
if (f1 <= f2)
{
b0 = x2;
}
else
{
a0 = x1;
}
delta = b0 - a0;
if (delta < eps)
{
Console.WriteLine();
Console.WriteLine($"x1 = {decimal.Round(x1, 3,
MidpointRounding.AwayFromZero)} x2 = {decimal.Round(x2, 3,
MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"y1 = {decimal.Round(f1, 3,
MidpointRounding.AwayFromZero)} y2 = {decimal.Round(f2, 3,
MidpointRounding.AwayFromZero)} ");
25
Console.WriteLine($"Количество итераций: {iterationsD}");
}
else
{
iterationsD++;
Console.Write($"x1 = {decimal.Round(x1, 3,
MidpointRounding.AwayFromZero)} x2 = {decimal.Round(x2, 3,
MidpointRounding.AwayFromZero)} ");
Console.Write($"y1 = {decimal.Round(f1, 3,
MidpointRounding.AwayFromZero)} y2 = {decimal.Round(f2, 3,
MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iterationsD}");
Dichotomy();
}
}
Dichotomy();
26
Приложение В Метод Нелдера-Мида
decimal eps = 0.001m; decimal alphaNM = 1; decimal gammaNM = 2; decimal betaNM = 0.5m; int iterationsNM = 0; double t = 1;
double n = 2;
decimal Function45(decimal x, decimal y)
{
decimal z1 = 6 * Convert.ToDecimal(Math.Pow(Convert.ToDouble(x) - 1, 2)) + 3 * Convert.ToDecimal(Math.Pow(Convert.ToDouble(y) - 1, 2));
return z1;
//decimal z2 = Convert.ToDecimal(Math.Pow(Convert.ToDouble(x) - 1, 2) + Math.Pow(Convert.ToDouble(y) - 3, 2) + 50 * Math.Pow(Convert.ToDouble(y + (2 * x) - 6), 2) + 5);
//return z2;
}
decimal a = Convert.ToDecimal((t * (Math.Sqrt(n + 1) + n - 1)) / (Math.Sqrt(2) * n)); decimal b = Convert.ToDecimal((t * (Math.Sqrt(n + 1) - 1)) / (Math.Sqrt(2) * n));
decimal[] x1 = { 0, 0 }; decimal[] x2 = { a, b }; decimal[] x3 = { b, a };
void NelderMead()
27
{
decimal f1 = Function45(x1[0], x1[1]); decimal f2 = Function45(x2[0], x2[1]); decimal f3 = Function45(x3[0], x3[1]);
decimal fmax = Math.Max(f1, Math.Max(f2, f3)); decimal fmin = Math.Min(f1, Math.Min(f2, f3));
decimal[] heavy = new decimal[2]; decimal[] light = new decimal[2]; decimal[] centre = new decimal[2]; decimal[] xi = new decimal[2];
decimal Ostanov(decimal[] heavy, decimal[] light, decimal[] xi, decimal[] centre)
{
decimal ostanovka = Convert.ToDecimal(Math.Sqrt((1 / (n + 1)) * Math.Pow(Convert.ToDouble(Function45(heavy[0], heavy[1]) - Function45(centre[0], centre[1])), 2) + Math.Pow(Convert.ToDouble((Function45(light[0], light[1]) - Function45(centre[0], centre[1]))), 2) + Math.Pow(Convert.ToDouble((Function45(xi[0], xi[1]) - Function45(centre[0], centre[1]))), 2)));
return ostanovka;
}
if (fmax == f1) { heavy = x1; }
else if (fmax == f2) { heavy = x2; } else { heavy = x3; }
if (fmin == f1) { light = x1; }
else if (fmin == f2) { light = x2; } else { light = x3; }
28
xi[0] = (x1[0] + x2[0] + x3[0]) - (heavy[0] + light[0]);
xi[1] = (x1[1] + x2[1] + x3[1]) - (heavy[1] + light[1]);
centre[0] = (1 / Convert.ToDecimal(n)) * (xi[0] + light[0]);
centre[1] = (1 / Convert.ToDecimal(n)) * (xi[1] + light[1]);
decimal[] mirror = { centre[0] + alphaNM * (centre[0] - heavy[0]), centre[1] + alphaNM * (centre[1] - heavy[1]) };
if (Function45(mirror[0], mirror[1]) < Function45(light[0], light[1]))
{
decimal[] stretch = { centre[0] + gammaNM * (mirror[0] - centre[0]), centre[1] + gammaNM * (mirror[1] - centre[1]) };
if (Function45(stretch[0], stretch[1]) < Function45(light[0], light[1]))
{
heavy = stretch;
if (Ostanov(heavy, light, xi, centre) < eps)
{
Console.WriteLine("Минимум:");
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(Function45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iterationsNM}");
}
else
{
29
iterationsNM++;
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(Function45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iterationsNM}");
x1 = heavy;
x2 = light;
x3 = xi;
NelderMead();
}
}
else
{
heavy = mirror;
if (Ostanov(heavy, light, xi, centre) < eps)
{
Console.WriteLine("Минимум:");
Console.Write($"({Decimal.Round(light[0], 3,
MidpointRounding.AwayFromZero)}, {Decimal.Round(light[1], 3,
MidpointRounding.AwayFromZero)}) ");
Console.Write($"z = {Decimal.Round(Function45(light[0], light[1]), 3, MidpointRounding.AwayFromZero)} ");
Console.WriteLine($"{iterationsNM}");
}
else
{
iterationsNM++;
30