/*------------------------------------------------------------------*\ |* Visualization functions *| \*------------------------------------------------------------------*/ void plotFunction(FunctionENUM function) { switch (function) { case FUNCTION1: for (double i = LEFT; i <= RIGHT; i += 0.001) { graphWin.plot(i, f1(i), 1); } break; case FUNCTION2: for (double i = LEFT; i <= RIGHT; i += 0.0001) { graphWin.plot(i, f2(i), 1); } break; case GOLD_FUNCTION: for (int i = 0; i < 19; i++) { graphWin.segment(map(i, 0, 19, 0, RIGHT), map(arrayGold[i], 250, 1700, 0, TOP), map(i + 1, 0, 19, 0, RIGHT), map(arrayGold[i + 1], 250, 1700, 0, TOP)); } break; default: cout << "The given function does not exist"; break; } }
void bolzanoBisectionRecursive(double a, double b, double epsilon) { if(fabs(b-a) <= epsilon) { #ifdef USING_FIRST_FUNCTION if (f(a) <= epsilon ) #else if (g(a) <= epsilon ) #endif cout << endl << ">>> " << b << " <<<<" << endl << endl; return; } double c = (a+b)/2; graphWin.segment(a,MAXHEIGHT,a,MINHEIGHT); graphWin.segment(b,MAXHEIGHT,b,MINHEIGHT); graphWin.segment(c,MAXHEIGHT,c,MINHEIGHT); cout << "Analysing interval [" << a <<", " << b << "] with middle point at : "<< c <<endl; if (c == 0) { cout << "0 is in " << c << endl; } #ifdef USING_FIRST_FUNCTION if (oppositeSigns(f(a),f(c))) #else if (oppositeSigns(g(a),g(c))) #endif { cout << " Narrowing interval to [" << a <<", " << c << "]" << endl; bolzanoBisectionRecursive(a,c,epsilon); } #ifdef USING_FIRST_FUNCTION if (oppositeSigns(f(c), f(b))) #else if (oppositeSigns(g(c), g(b))) #endif { cout << " Narrowing interval to [" << c <<", " << b << "]" << endl; bolzanoBisectionRecursive(c,b,epsilon); } }
void fixedPoint(double epsilon, double lambda, double startingPoint,bool isFirst) { double previousPoint = startingPoint; int loopCounter = 0; while (fabs(fixedPointAdapterFunction(previousPoint) - previousPoint) > epsilon && loopCounter < LOOP_LIMIT) { previousPoint=fixedPointAdapterFunction(previousPoint, lambda); if(numberLine == 2) { plotAdaptedFunction(lambda);//we plot it here to have the proper lamda drawn graphWin.segment(previousPoint, fixedPointAdapterFunction(previousPoint, lambda), fixedPointAdapterFunction(previousPoint, lambda), fixedPointAdapterFunction(previousPoint, lambda)); graphWin.segment(previousPoint, previousPoint, previousPoint, fixedPointAdapterFunction(previousPoint, lambda)); } loopCounter++; } #ifdef USING_FIRST_FUNCTION if(f(previousPoint) <= epsilon) #else if(g(previousPoint) <= epsilon) #endif { graphWin.plot(previousPoint,fixedPointAdapterFunction(previousPoint, lambda),5); cout << "x = " << previousPoint << endl; } else { cout << "x MAUVAIS = " << previousPoint << endl; } double tester = 2.0; if(lambda > tester) { numberLine++; lambda -= tester; fixedPoint(epsilon, lambda,startingPoint); } }
void plotSecondDegreeDerivatedFunction(FunctionENUM function, double h) { if (function != GOLD_FUNCTION) { for (double i = LEFT; i <= RIGHT; i += 0.001) { graphWin.plot(i, calculateSecondDegreeDerivative(function, i, h), 1); } } else { for (double i = 0; i < 18; i += 1) { double secondDegreeDerivative = calculateSecondDegreeDerivative(function, i, h); double secondDegreeDerivativePlusOne = calculateSecondDegreeDerivative(function, i + 1, h); graphWin.segment(map(i, 0, 19, 0, RIGHT), map(secondDegreeDerivative, 250, 1700, 0, TOP), map(i + 1, 0, 19, 0, RIGHT), map(secondDegreeDerivativePlusOne, 250, 1700, 0, TOP)); } } }
void plotDerivatedFunction(FunctionENUM function, DerivationMethodENUM derivationMethod, double h) { if (function != GOLD_FUNCTION) { for (double i = LEFT; i <= RIGHT; i += 0.001) { graphWin.plot(i, calculateDerivative(function, i, h, derivationMethod), 1); } } else { double leftLimit = derivationMethod == CENTRAL_DIFFERENCE ? 1 : 0; for (double i = leftLimit; i < 18; i += 1) { double derivative = calculateDerivative(function, i, h, derivationMethod); double derivativePlusOne = calculateDerivative(function, i + 1, h, derivationMethod); graphWin.segment(map(i, 0, 19, 0, RIGHT), map(derivative, 250, 1700, 0, TOP), map(i + 1, 0, 19, 0, RIGHT), map(derivativePlusOne, 250, 1700, 0, TOP)); } } }
void plotDerivationMethod(FunctionENUM function, double x, double h, DerivationMethodENUM derivativeMethod) { switch (function) { case FUNCTION1: switch (derivativeMethod) { case PROGRESSIVE_DIFFERENCE: for (double delta = 0; delta <= h; delta += h) { graphWin.plot(x + delta, f1(x + delta), 4); graphWin.segment(x + delta, 0, x + delta, f1(x + delta)); graphWin.segment(0, f1(x + delta), x + delta, f1(x + delta)); } //Segment between two points graphWin.segment(x, f1(x), x + h, f1(x + h)); break; case CENTRAL_DIFFERENCE: for (double delta = -h; delta <= h; delta += h) { //Points graphWin.plot(x + delta, f1(x + delta), 4); //Lines from X axe graphWin.segment(x + delta, 0, x + delta, f1(x + delta)); //Lines from Y axe graphWin.segment(0, f1(x + delta), x + delta, f1(x + delta)); } //Segment between two points graphWin.segment(x - h, f1(x - h), x + h, f1(x + h)); break; case FOURTH_DEGREE_POLYNOM: for (double delta = -h; delta <= h; delta += h / 2) { graphWin.segment(x + delta, 0, x + delta, f1(x + delta)); graphWin.plot(x + delta, f1(x + delta), 4); } break; default: break; } break; case FUNCTION2: switch (derivativeMethod) { case PROGRESSIVE_DIFFERENCE: for (double delta = 0; delta <= h; delta += h) { graphWin.plot(x + delta, f2(x + delta), 4); graphWin.segment(x + delta, 0, x + delta, f2(x + delta)); graphWin.segment(0, f2(x + delta), x + delta, f2(x + delta)); } //Segment between two points graphWin.segment(x, f2(x), x + h, f2(x + h)); break; case CENTRAL_DIFFERENCE: for (double delta = -h; delta <= h; delta += h) { //Points graphWin.plot(x + delta, f2(x + delta), 4); //Lines from X axe graphWin.segment(x + delta, 0, x + delta, f2(x + delta)); //Lines from Y axe graphWin.segment(0, f2(x + delta), x + delta, f2(x + delta)); } //Segment between two points graphWin.segment(x - h, f2(x - h), x + h, f2(x + h)); break; case FOURTH_DEGREE_POLYNOM: for (double delta = -h; delta <= h; delta += h / 2) { graphWin.segment(x + delta, 0, x + delta, f2(x + delta)); graphWin.plot(x + delta, f2(x + delta), 4); } break; default: break; } break; default: break; } }
std::vector<long double> findRoot() { const long double epsilon = std::numeric_limits<double>::epsilon(); const long double STEP = 2*epsilon; std::vector<long double> solutions; //FindRoot //Recherche les racines du côté x >= 0 long double oldStep = 2*epsilon; for(long double x=oldStep; x<graphWin.xMax(); x+=fabsl(g(x)-x)) { //Dessin des barres comme dans le cours graphWin.segment(x-oldStep,x-oldStep,x-oldStep,g(x-oldStep)); if(x > g(x)) { if(f == f1) graphWin.segment(x-oldStep,g(x-oldStep),x,g(x-oldStep)); else graphWin.segment(x-oldStep,x-oldStep,x,x-oldStep); } else graphWin.segment(x-oldStep,x,x,x); if(fabsl(g(x)-x) <= epsilon) { solutions.push_back(x); x+=STEP;//On ajoute suffisement de STEP pour que la fonction puisse repartir et chercher d'autres solutions } oldStep = fabsl(g(x)-x); } oldStep = 2*epsilon; //Recherche les racines du côté x < 0 for(long double x=-epsilon; x > graphWin.xMin(); x-=fabsl(g(x)-x)) { //Dessin des barres comme dans le cours graphWin.segment(x+oldStep,x+oldStep,x+oldStep,g(x+oldStep)); if(x < g(x)) { if(f == f1) graphWin.segment(x+oldStep,g(x+oldStep),x,g(x+oldStep)); else graphWin.segment(x+oldStep,x+oldStep,x,x+oldStep); } else graphWin.segment(x+oldStep,x,x,x); if(fabsl(g(x)-x) <= epsilon) { solutions.push_back(x); x-=STEP;//On ajoute le STEP initiale pour que la fonction puisse repartir et chercher d'autres solutions } oldStep = fabsl(g(x)-x); } sort(solutions.begin(),solutions.end()); return solutions; }
void plotLinearFunction() { graphWin.segment(LEFTLIMIT, LEFTLIMIT, RIGHTLIMIT, RIGHTLIMIT); }