Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace zad1
- {
- class Program
- {
- static float getFunc(float x)
- {
- //Set func there
- return x * x;
- }
- static void Main(string[] args)
- {
- bool inputFlag = true;
- float a = 0, b = 0;
- Console.WriteLine("Vvedite a");
- while (inputFlag)
- {
- if (!float.TryParse(Console.ReadLine(), out a))
- {
- Console.WriteLine("Error");
- inputFlag = true;
- }
- else
- inputFlag = false;
- }
- inputFlag = true;
- Console.WriteLine("Vvedite b");
- while (inputFlag)
- {
- if (!float.TryParse(Console.ReadLine(), out b))
- {
- Console.WriteLine("Error");
- inputFlag = true;
- }
- else
- inputFlag = false;
- }
- Console.WriteLine("Интегралы от {0} до {1}:", a, b);
- Console.WriteLine("Метод прямоугольников: {0}", rectangleMethod(a, b));
- Console.WriteLine("Метод Монте-Карло: {0}", monteCarloMethod(a, b));
- Console.WriteLine("Метод трапеций: {0}", trapeziumMethod(a, b));
- Console.WriteLine("Метод Симпсона: {0}", simpsonMethod(a, b));
- MonteCarlosMethod();
- }
- static float rectangleMethod(float a, float b)
- {
- float integral = 0;
- float step = 0.005f;
- for (float i = a; i <= b; i += step)
- {
- integral += getFunc(i);
- }
- integral *= step;
- return integral;
- }
- static float monteCarloMethod(float a, float b)
- {
- int isIn = 0;
- float maxYValue = getFunc(b), minYValue = getFunc(a);
- int n = 10000;
- Random random = new Random(DateTime.Now.Millisecond);
- Random rnd = new Random(DateTime.Now.Hour);
- for (int i = 0; i < n; i++)
- {
- float pointX = GetRandomFloat(a, b, random);
- float pointY = GetRandomFloat(minYValue, maxYValue, rnd);
- Console.WriteLine("x = {0}, y = {1}", pointX, pointY);
- if (getFunc(pointX) > pointY)
- isIn++;
- }
- return (maxYValue * b * isIn)/n;
- }
- static float trapeziumMethod(float a, float b)
- {
- float integral = 0;
- float step = 0.005f;
- for (float i = a; i <= b; i += step)
- {
- integral += 2 * getFunc(i);
- }
- integral = (integral + getFunc(a) + getFunc(b)) * step/2;
- return integral;
- }
- static float simpsonMethod(float a, float b)
- {
- int n = 1;
- float step = 0.005f;
- float integral = 0;
- float integral1 = 0, integral2 = 0;
- for (float i = a; i <= b; i += step)
- {
- if (!(n % 2 == 0))
- {
- integral1 += getFunc(i);
- }
- else
- {
- integral2 += getFunc(i);
- }
- n++;
- }
- integral = (integral + getFunc(a) + getFunc(b) + 4 * integral2 + 2*integral1) * step / 3;
- return integral;
- }
- public static float GetRandomFloat(float minimum, float maximum, Random random)
- {
- return (float) random.NextDouble() * (maximum - minimum) + minimum;
- }
- public static void MonteCarlosMethod()
- {
- double leftEdge, rightEdge, step;
- Console.WriteLine("enter left edge");
- leftEdge = double.Parse(Console.ReadLine());
- Console.WriteLine("enter right edge");
- rightEdge = double.Parse(Console.ReadLine());
- Console.Write("enter number of dots to count on\nn: ");
- int n = int.Parse(Console.ReadLine());
- double height = Math.Pow(rightEdge, 2);
- Random xRandom = new Random(DateTime.Now.Millisecond);
- Random yRandom = new Random(DateTime.Now.Hour);
- double xTemp, yTemp;
- double isInFunction = 0;
- for (int i = 0; i < n; i++)
- {
- xTemp = xRandom.NextDouble() * (rightEdge - leftEdge) + leftEdge;
- yTemp = yRandom.NextDouble() * height;
- if (yTemp < Math.Pow(xTemp, 2))
- isInFunction++;
- }
- double result = ((rightEdge - leftEdge) * height) * (isInFunction / n);
- Console.WriteLine("result: " + result);
- Console.WriteLine("press any key to continue");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement