using System using System Collections Generic using System Linq using

 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Newton_s_method {
class Program {
public static double getFunction(double x) {
double retValue = x - 1 / (3 + Math.Sin(3.6 * x));
return retValue;
}
public static double getFirstDerivative(double x) {
double retValue = 1 + (3.6 * Math.Cos(3.6*x)) / Math.Pow(Math.Sin(3.6*x) + 3, 2.0);
return retValue;
}
public static double getSecondDerivative(double x) {
double retValue = -(12.96 * Math.Sin(3.6 * x)) / Math.Pow(Math.Sin(3.6 * x) + 3, 2.0) - (25.92 * Math.Pow(Math.Cos(3.6 * x), 2.0)) / Math.Pow(Math.Sin(3.6 * x) + 3, 3.0);
return retValue;
}
static void Main(string[] args) {
double leftRange = 0.0,
rightRange = 0.85,
eps = 0.0001;
double curApproximation = 0.0,
nextApproximation = 0.0,
difference = 0.0;
int countIter = 0;
while(true) {
curApproximation = double.Parse(Console.ReadLine());
if (curApproximation < leftRange || curApproximation > rightRange)
Console.WriteLine("Заданное Вами приближение находится вне границ отрезка [a, b]. Пожалуйста, повторите ввод:");
else if (getFunction(curApproximation) * getSecondDerivative(curApproximation) <= 0)
Console.WriteLine("Заданное Вами приближение не удовлетворяет критерию сходимости метода Ньютона к корню уравнения. Пожалуйста, повторите ввод:");
else
break;
}
do {
nextApproximation = curApproximation - getFunction(curApproximation) / getFirstDerivative(curApproximation);
difference = Math.Abs(nextApproximation - curApproximation);
curApproximation = nextApproximation;
countIter++;
} while (difference > eps);
Console.WriteLine("x = {0:f4}", nextApproximation);
Console.WriteLine("F(x) = {0:f4}", getFunction(nextApproximation));
}
}
}