Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double e = 2.718281828;
- double eps;
- double deltha = 0.5;
- double goldenRatio = (1 + Math.Sqrt(5)) / 2;
- double x0, a, b, q, l, r1, r2;
- bool condition = false;
- double f(double x)
- {
- return (Math.Pow(x, 5) + x - 3);
- }
- double derf(double x)
- {
- return (5*Math.Pow(x, 4) + 1);
- }
- Console.WriteLine("*SIMPLE ITERATION METHOD*");
- Console.WriteLine("Input precision");
- Console.Write("eps = ");
- eps = double.Parse(Console.ReadLine());
- Console.WriteLine("Input start of interval");
- Console.Write("a = ");
- a = double.Parse(Console.ReadLine());
- Console.WriteLine("Input end of interval");
- Console.Write("b = ");
- b = double.Parse(Console.ReadLine());
- x0 = (a + b) / 2;
- double a1 = a, b1 = b;
- while (Math.Abs(b1 - a1) > eps)
- {
- r1 = b1 - (b1 - a1) / goldenRatio;
- r2 = a1 + (b1 - a1) / goldenRatio;
- if (Math.Abs(derf(r1)) <= Math.Abs(derf(r2)))
- a1 = r1;
- else
- b1 = r2;
- }
- l = 1/ Math.Abs(derf((a1 + b1) / 2));
- double phi(double x)
- {
- return( x - l * (Math.Pow(x, 5) + x - 3));
- }
- double derphi(double x)
- {
- return (1 - l* (5 * Math.Pow(x, 4) + 1));
- }
- a1 = a;
- b1 = b;
- while (Math.Abs(b1 - a1) > eps)
- {
- r1 = b1 - (b1 - a1) / goldenRatio;
- r2 = a1 + (b1 - a1) / goldenRatio;
- if (Math.Abs(derphi(r1)) <= Math.Abs(derphi(r2)))
- a1 = r1;
- else
- b1 = r2;
- }
- q = Math.Abs(derphi((a1 + b1) / 2));
- if ((phi(x0) - x0) < deltha * (1 - q)) condition = true;
- else
- {
- condition = false;
- Console.WriteLine("Convergence condition is not implemented");
- }
- double x1 = 0;
- int i = 0;
- double abs = Math.Abs(x1 - x0);
- if (condition)
- {
- while(abs>=eps)
- {
- i++;
- x1 = phi(x0);
- abs = Math.Abs(x1 - x0);
- x0 = x1;
- Console.WriteLine("x" + i + " = " + Math.Round(x1, 8));
- }
- Console.WriteLine
- (
- "Approximated root: {0}; " +
- "Number of iterations: {1}; " +
- "Estimated number of iterations: {2};", Math.Round(x1, 8), i, Math.Round(Math.Log((b - a) / ((1 - q) * eps)) / Math.Log(1 / q), 0) + 1
- );
- }
- Console.WriteLine();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement