/*------------------------------------------------------------------*\ |* 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; } }
/**************************************************** * Helper functions ****************************************************/ void plotCurrentFunction() { for(double i = LEFTLIMIT; i <= RIGHTLIMIT; i = i + 0.01) { #ifdef USING_FIRST_FUNCTION graphWin.plot(i,f(i),1); #else graphWin.plot(i,g(i),1); #endif } }
void GeomGlut::initGraphicsWin( unsigned int pixelWinX, unsigned int pixelWinY, double _xMin, double _xMax, double _yMin, double _yMax ,long double (*func)(long double)) { f = func; if(_xMax-_xMin<=0) return; winFuncPixels.x = pixelWinX; winFuncPixels.y = pixelWinY; glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowPosition(0, 0); glutInitWindowSize(winFuncPixels.x, winFuncPixels.y); // at the begining, the function win pixel and the actual win pixel are identical: graphWin.setWinPixels( winFuncPixels.x, winFuncPixels.y ); minWin.x = _xMin; minWin.y = _yMin; maxWin.x = _xMax; maxWin.y = _yMax; glutCreateWindow("Algorithmes numériques: labo#4 - Part 1"); // Initialiser la couleur du fond (blanc) glClearColor(0.9f, 0.9f, 0.9f, 1.0f); glutReshapeFunc( Reshape ); glutDisplayFunc( Display ); glutMainLoop(); }
void plotAdaptedFunction(double lambda) { for(double i = LEFTLIMIT; i < RIGHTLIMIT; i += 0.01) { graphWin.plot(i, fixedPointAdapterFunction(i, lambda), 1); } }
int main(int argc, char **argv) { glutInit(&argc, argv); graphWin.initGraphicsWin( 1000, -15.1, 15.1, -4.1, 4.1 ); return(0); }
void Display() { glClear(GL_COLOR_BUFFER_BIT); graphWin.drawAxes(); mainFunction(); glFlush(); }
int main() { setlocale(LC_ALL, "frs"); const vector<pair<string,long double> > goldValues = createMapGold(); const vector<pair<string,long double> > goldInflations = createInflationChart(goldValues); graphWin.initGraphicsWin( 1400, 800, 0, goldValues.size(), 0, 3000, goldValues, goldInflations); return( 0 ); }
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 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 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 mainFunction( void ) { graphWin.drawAxes(); RectangleV1 rec(-2,2,4,2); rec.draw(); CircleV1 cercle(0,0,1,300); cercle.draw(); TriangleV1 triangle(2.0,2.0,1.0,3.0,2.0,1.0); triangle.draw(); SquareV1 square(2.0,2.0,0.5); square.draw(); }
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 Click(int button, int state, int x, int y) { double posX, posY; switch(button) { case 0: posX = ((double)x / (double)graphWin.xWinFunc()) * (graphWin.xMax() - graphWin.xMin()) + graphWin.xMin(); posY = ((1 - (double)y / (double)graphWin.yWinFunc())) * (graphWin.yMax() - graphWin.yMin()) + graphWin.yMin(); dessinerCourbe(posX, posY); break; } }
int main(int argc, char *argv[]) { cout << "Choisir une fonction a afficher : " << endl; cout << "1) x^5 + 5*x^3 + 2*x" << endl; cout << "2) x/(1-x^2)" << endl; cout << "x) Quitter" << endl << endl; cout << "Choix : "; cin >> res1; cout << "Choix de la derivee:" << endl; cout << "1) derivee premiere" << endl; cout << "2) derivee seconde" << endl; cout << "Choix : "; cin >> res2; graphWin.initGraphicsWin(1000, X_MIN, X_MAX, Y_MIN, Y_MAX); return 0; }
void GeomGlut::initGraphicsWin( unsigned int pixelWinX, double xMin, double xMax, double yMin, double yMax ) { if(xMax-xMin<=0) { cout << "xMax-xMin cannot be < or = to 0" << endl << endl; return; } winFuncPixels.x = pixelWinX; winFuncPixels.y = (unsigned int)((yMax-yMin) * (double)winFuncPixels.x/(xMax-xMin)); int argc = 1; char *argv[1] = {(char*)"Glut"}; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA | GLUT_MULTISAMPLE); glutInitWindowPosition(0, 0); cout << "System coordinate min(" << xMin << ", " << yMin << ") max(" << xMax << ", " << yMax << " give for " << winFuncPixels.x << " pixels in X, a Y pixel number of " << winFuncPixels.y << endl << endl; cout << "pixelWin(" << winFuncPixels.x << ", " << winFuncPixels.y << ")..."; glutInitWindowSize(winFuncPixels.x, winFuncPixels.y); cout << " success!" << endl << endl; // at the begining, the function win pixel and the actual win pixel are identical: graphWin.setWinPixels( winFuncPixels.x, winFuncPixels.y ); minWin.x = xMin; minWin.y = yMin; maxWin.x = xMax; maxWin.y = yMax; glutCreateWindow("Algorithmes numeriques: labo#6"); // Initialiser la couleur du fond (blanc) glClearColor(0.9f, 0.9f, 0.9f, 1.0f); glutMouseFunc( Click ); glutReshapeFunc( Reshape ); glutDisplayFunc( Display ); glutMainLoop(); }
void Reshape(int w, int h) { if (h == 0) h = 1; if (w == 0) w = 1; glViewport(0, 0, w, h); graphWin.setWinPixels( w, h ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); float xRatio = static_cast<float>(w)/static_cast<float>(graphWin.xWinFunc()); float yRatio = static_cast<float>(h)/static_cast<float>(graphWin.yWinFunc()); // Volume de clipping : (left, right, bottom, top, near, far) glOrtho(graphWin.xMin()*xRatio, graphWin.xMax()*xRatio, graphWin.yMin()*yRatio, graphWin.yMax()*yRatio, -2.0f, 2.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
int main(int argc, char **argv) { graphWin.initGraphicsWin( 1000, LEFTLIMIT, RIGHTLIMIT, MINHEIGHT, MAXHEIGHT ); mainFunction(); return(0); }
void plotLinearFunction() { graphWin.segment(LEFTLIMIT, LEFTLIMIT, RIGHTLIMIT, RIGHTLIMIT); }
void drawFunctions() { const float STEP = graphWin.findSmartStepX(); glPointSize(2.0f); //Draw selected (f(x)) for(float x=graphWin.xMin(); x<graphWin.xMax(); x+=STEP) graphWin.plot(x, f(x), 1.0f,0.5f,0.0f); //Draw f(x) = x for(float x=graphWin.xMin(); x<graphWin.xMax(); x+=STEP) graphWin.plot( x, x, 0.5f,0.5f,0.5f); //Draw g(x) for(float x=graphWin.xMin()+STEP; x<graphWin.xMax(); x+=STEP) graphWin.plot(x, g(x), 1.0f,0.5f,1.0f); clear(); printHeader(); std::cout << "x est compris dans l'ensemble [" << graphWin.xMin() << ";" << graphWin.xMax() << "[" << std::endl; std::cout << "y est compris dans l'ensemble [" << graphWin.yMin() << ";" << graphWin.yMax() << "[" << std::endl << std::endl; std::cout << "Information graphique : " << std::endl << "- Fonction choisie (f(x)) en orange" << std::endl << "- Fonction h(x) = x, en gris" << std::endl << "- Fonction g(x) = x + l*f(x), l = 1, en rose" << std::endl << "- Les axes x,y en bleu " << std::endl << "- Les vecteurs unitaires en rouge " << std::endl << "- Les solutions de la fonction en vert" << std::endl << std::endl; vector<long double> solutions = findRoot(); std::cout << "Solutions : " << std::endl; glColor3f(0.0f, 1.0f, 0.0f); glPointSize(10); //Draw solutions for(unsigned int i = 0;i < solutions.size(); ++i) { std::cout << "[" << i << "] -> " << static_cast<double>(solutions[i]) << std::endl; glBegin( GL_POINTS ); glVertex3d(solutions[i], 0, 0.0); glEnd(); } }
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 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; } }
int main(int argc, char **argv) { graphWin.initGraphicsWin(1000, LEFT, RIGHT, BOTTOM, TOP); mainFunction(); return 0; }