Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.6KB

  1. #include <iostream>
  2. #include <vector>
  3. #include "hdnum.hh"
  4. template<class N>
  5. class EllipseProblem
  6. {
  7. public:
  8. // Exportiere Größentyp
  9. typedef std::size_t size_type;
  10. // Exportiere Zahlentyp
  11. typedef N number_type;
  12. // Dimension der untenstehenden Funktion
  13. std::size_t size () const
  14. {
  15. return 2;
  16. }
  17. // Funktionsauswertung
  18. void F (const hdnum::Vector<N>& x, hdnum::Vector<N>& result) const
  19. {
  20. result[0] = pow(x[0], 2) + pow(x[1], 2) - 4;
  21. result[1] = pow(x[0], 2)/9 + pow(x[1], 2) - 1;
  22. }
  23. // Jacobimatrix
  24. void F_x (const hdnum::Vector<N>& x, hdnum::DenseMatrix<N>& result) const
  25. {
  26. result[0][0] = 2*x[0]; result[0][1] = 2*x[1];
  27. result[1][0] = 2*x[0]/9; result[1][1] = 2*x[1];
  28. }
  29. };
  30. int main ()
  31. {
  32. typedef double Number; // Zahlentyp
  33. typedef EllipseProblem<Number> Problem;
  34. Problem problem;
  35. hdnum::Vector<Number> u(problem.size()); // Vektor für Startpunkt bzw Lösung
  36. hdnum::Vector<Number> result(problem.size()); // Vektor für Funktionswerte
  37. // Newton
  38. hdnum::Newton newton;
  39. newton.set_maxit(20);
  40. newton.set_verbosity(2);
  41. newton.set_reduction(1e-10);
  42. newton.set_abslimit(1e-20);
  43. newton.set_linesearchsteps(3);
  44. // 1. Quadrant
  45. u[0] = 2.0; u[1] = 1.0;
  46. newton.solve(problem,u);
  47. std::cout << "Nullstelle im 1. Quadrant: " << std::setprecision(15) << u << std::endl;
  48. problem.F(u, result);
  49. std::cout << "Funktionswert: " << std::setprecision(15) << result << std::endl;
  50. // 2. Quadrant
  51. u[0] = -2.0; u[1] = 1.0;
  52. newton.solve(problem,u);
  53. std::cout << "Nullstelle im 2. Quadrant: " << std::setprecision(15) << u << std::endl;
  54. problem.F(u, result);
  55. std::cout << "Funktionswert: " << std::setprecision(15) << result << std::endl;
  56. // 3. Quadrant
  57. u[0] = -2.0; u[1] = -1.0;
  58. newton.solve(problem,u);
  59. std::cout << "Nullstelle im 3. Quadrant: " << std::setprecision(15) << u << std::endl;
  60. problem.F(u, result);
  61. std::cout << "Funktionswert: " << std::setprecision(15) << result << std::endl;
  62. // 4. Quadrant
  63. u[0] = 2.0; u[1] = -1.0;
  64. newton.solve(problem,u);
  65. std::cout << "Nullstelle im 4. Quadrant: " << std::setprecision(15) << u << std::endl;
  66. problem.F(u, result);
  67. std::cout << "Funktionswert: " << std::setprecision(15) << result << std::endl;
  68. // Relaxation
  69. hdnum::Banach banach;
  70. banach.set_maxit(5000); // set parameters
  71. banach.set_verbosity(2);
  72. banach.set_reduction(1e-15);
  73. banach.set_abslimit(1e-20);
  74. banach.set_sigma(0.375);
  75. u[0] = 2.0; u[1] = 1.0; // Startwert
  76. banach.solve(problem,u);
  77. std::cout << "Ergebnis: " << std::setprecision(15) << u << std::endl;
  78. return 0;
  79. }