diff --git a/InstallerSamples/Algorithms/NumericAlgorithms/Bisect.pas b/InstallerSamples/Algorithms/NumericAlgorithms/Bisect.pas new file mode 100644 index 000000000..1a799b518 --- /dev/null +++ b/InstallerSamples/Algorithms/NumericAlgorithms/Bisect.pas @@ -0,0 +1,27 @@ +function f(x: real) := exp(x) - 4; // Короткое определение функции + +function BisectionMethod(a, b, eps: real): real; +begin + var fa := f(a); // Вычисляем f(a) один раз в начале + while abs(b - a) > eps do + begin + var c := (a + b) / 2; + var fc := f(c); // Вычисляем функцию только один раз на итерацию + if fc = 0 then + break; // Выход из цикла, если найден точный корень + if fa * fc < 0 then + b := c + else + (a, fa) := (c, fc); // Кортежное присваивание + end; + Result := (a + b) / 2; // Возвращаем среднюю точку как приближенный корень +end; + +begin + var (a, b) := (0.0, 3.0); // Множественное присваивание для a и b + var eps := 0.000001; + + var root := BisectionMethod(a, b, eps); + + Println('Корень уравнения:', root); +end. \ No newline at end of file diff --git a/InstallerSamples/Algorithms/NumericAlgorithms/Newton.pas b/InstallerSamples/Algorithms/NumericAlgorithms/Newton.pas new file mode 100644 index 000000000..45863dc31 --- /dev/null +++ b/InstallerSamples/Algorithms/NumericAlgorithms/Newton.pas @@ -0,0 +1,18 @@ +function f(x: real) := exp(x) - 4; // Функция f(x) = exp(x) - 4 +function df(x: real) := exp(x); // Производная f'(x) = exp(x) + +function NewtonMethod(x, eps: real): real; +begin + while abs(f(x)) > eps do + x := x - f(x) / df(x); + Result := x; +end; + +begin + var x := 0.5; // Начальное приближение + var eps := 0.00001; // Заданная точность + + var root := NewtonMethod(x, eps); + + Println('Корень уравнения: ', root); +end. \ No newline at end of file