Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <math.h>
- #include <random>
- #define function(X) X / (1.2 - sin(2 * X))
- using namespace std;
- double functionA(double X)
- {
- return X / (1.2 - sin(2*X));
- }
- double functionB(double X)
- {
- return ((pow(X, 3) - pow(X, 2) + 4 * X) / (2 * pow(X, 3) + 5 * pow(X, 2) - 2 * X + 1));
- }
- double functionC(double X)
- {
- return (tan(sin(2 * X + 1)));
- }
- double functionD(double X)
- {
- return (1 / (1 + exp(sin(2 * X + 1) + 2)));
- }
- double randomFloatRange(double a, double b) {
- static std::default_random_engine e;
- static std::uniform_real_distribution<> dis(a, b);
- return dis(e);
- }
- int fibonacciNumbers(int sequenceTerm)
- {
- const int firstTerm = 1;
- const int secondTerm = 1;
- int F[sequenceTerm];
- F[0] = firstTerm;
- F[1] = secondTerm;
- for(int i = 2; i <= sequenceTerm; i++)
- {
- F[i] = F[i - 1] + F[i - 2];
- }
- return F[sequenceTerm];
- }
- int lucasNumbers(int sequenceTerm)
- {
- const int firstTerm = 2;
- const int secondTerm = 1;
- int F[sequenceTerm];
- F[0] = firstTerm;
- F[1] = secondTerm;
- for(int i = 2; i <= sequenceTerm; i++)
- {
- F[i] = F[i - 1] + F[i - 2];
- }
- return F[sequenceTerm];
- }
- void fibonnaciSearch(double initA, double initB, int iteration)
- {
- double A[iteration];
- double B[iteration];
- double pointX1[iteration];
- double pointX2[iteration];
- double functionX1;
- double functionX2;
- A[0] = initA;
- B[0] = initB;
- std::cout << "Initial values (a, b) = " << "(" << initA << ", " << initB << ")" <<std::endl;
- for(int k = 1; k <= iteration; k++)
- {
- pointX1[k - 1] = (A[k - 1]) + (((B[k - 1]) - (A[k - 1]))
- * lucasNumbers(iteration - k + 1) / lucasNumbers(iteration - k + 3));
- pointX2[k - 1] = A[k - 1] + ((B[k - 1] - A[k - 1])
- * lucasNumbers(iteration - k + 2) / lucasNumbers(iteration - k + 3));
- functionX1 = function(pointX1[k - 1]);
- functionX2 = function(pointX2[k - 1]);
- if(functionX1 <= functionX2)
- {
- A[k] = A[k - 1];
- B[k] = pointX2[k - 1];
- }
- else if(functionX1 > functionX2)
- {
- A[k] = pointX1[k - 1];
- B[k] = B[k - 1];
- }
- std::cout << "\nIteration: " << k << " Inner Points (a, b): " << "(" <<A[k] << ", " << B[k] << ")" << std::endl;
- std::cout << "MidPoint: " << (A[k] + B[k]) / 2 << std::endl;
- }
- }
- void twoRandfromX(double A, double B, int points_amount, double accuracy)
- {
- std::vector<double> points;
- std::vector<double> pointsValues;
- int iterations = 0;
- points.clear();
- double pointA = A;
- double pointB = B;
- points.push_back(pointA);
- points.push_back(pointB);
- double firstLowestValue = MAXFLOAT;
- double secondLowestValue = MAXFLOAT;
- double buffer;
- int whichA = 1;
- int whichB = 1;
- while((pointB - pointA) > accuracy) {
- for (int i = 0; i < points_amount; i++) {
- if(i >= 2)
- {
- points.push_back(randomFloatRange(pointA, pointB));
- }
- pointsValues.push_back(functionD(points[i]));
- buffer = pointsValues[i];
- if (buffer < firstLowestValue) {
- whichA = i;
- firstLowestValue = buffer;
- } else if (buffer < secondLowestValue) {
- whichB = i;
- secondLowestValue = buffer;
- }
- iterations++;
- }
- pointA = points[whichA];
- pointB = points[whichB];
- std::cout << "After " << iterations << " loops of points creation" << std::endl;
- std::cout << pointA << " " << pointB << std::endl;
- }
- }
- void xEvenlySpaced(double A, double B, int points_amount, double accuracy)
- {
- std::vector<double> points;
- std::vector<double> pointsValues;
- double firstPoint = A;
- double secondPoint = B;
- double difference;
- double space = MAXFLOAT;
- double currentPoint;
- double buffer;
- int whichA = 1;
- int whichB = 1;
- double firstLowestValue = MAXFLOAT;
- double secondLowestValue = MAXFLOAT;
- std::cout << firstPoint << " " << secondPoint << std::endl;
- difference = secondPoint - firstPoint;
- space = difference / points_amount;
- while(space > accuracy)
- {
- currentPoint = firstPoint;
- std::cout << space << std::endl;
- for (int i = 0; i < points_amount; i++)
- {
- points.push_back(currentPoint);
- //std::cout << points[i] << std::endl;
- pointsValues.push_back(functionB(points[i]));
- buffer = pointsValues[i];
- currentPoint += space;
- if(buffer < firstLowestValue)
- {
- whichA = i;
- firstLowestValue = buffer;
- }
- else if(buffer < secondLowestValue)
- {
- whichB = i;
- secondLowestValue = buffer;
- }
- //std::cout << firstLowestValue << " " << secondLowestValue << std::endl;
- }
- if(points[whichA] < points[whichB])
- {
- firstPoint = points[whichA];
- secondPoint = points[whichB];
- }else
- {
- firstPoint = points[whichB];
- secondPoint = points[whichA];
- }
- difference = secondPoint - firstPoint;
- space = difference / points_amount;
- std::cout << firstPoint << " " << secondPoint << std::endl;
- }
- }
- int main() {
- int chosenNumber = 2;
- srand(time(NULL));
- int i = 0;
- //std::cout << fibonacciNumbers(chosenNumber) << std::endl;
- //std::cout << lucasNumbers(chosenNumber) << std::endl;
- //fibonnaciSearchMAX(-1, 1, 20);
- //twoRandfromX(-1, 1, 100, 0.0001);
- xEvenlySpaced(-1, 1, 100, 0.0001);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement