Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

101 satır
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] = x[0] * x[1] + 2*x[0] - x[1] - 2;
  21. result[1] = x[0] * x[1] - x[0] + x[1] -3;
  22. }
  23. // Jacobimatrix
  24. void F_x (const hdnum::Vector<N>& x, hdnum::DenseMatrix<N>& result) const
  25. {
  26. result[0][0] = x[1] + 2;
  27. result[0][1] = x[0] - 1;
  28. result[1][0] = x[1] - 1;
  29. result[1][1] = x[0] + 1;
  30. }
  31. };
  32. int main ()
  33. {
  34. typedef double Number; // Zahlentyp
  35. typedef EllipseProblem<Number> Problem;
  36. Problem problem;
  37. hdnum::Vector<Number> u(problem.size()); // Vektor für Startpunkt bzw Lösung
  38. hdnum::Vector<Number> result(problem.size()); // Vektor für Funktionswerte
  39. // Newton
  40. hdnum::Newton newton;
  41. newton.set_maxit(20);
  42. newton.set_verbosity(10);
  43. newton.set_reduction(1e-10);
  44. newton.set_abslimit(1e-20);
  45. newton.set_linesearchsteps(2);
  46. // 1. Quadrant
  47. u[0] = 0.0; u[1] = 1.0;
  48. newton.solve(problem,u);
  49. std::cout << "Nullstelle im 1. Quadrant: " << std::setprecision(15) << u << std::endl;
  50. problem.F(u, result);
  51. std::cout << "Funktionswert: " << std::setprecision(15) << result << std::endl;
  52. // 2. Quadrant
  53. u[0] = -2.0; u[1] = 1.0;
  54. newton.solve(problem,u);
  55. std::cout << "Nullstelle im 2. Quadrant: " << std::setprecision(15) << u << std::endl;
  56. problem.F(u, result);
  57. std::cout << "Funktionswert: " << std::setprecision(15) << result << std::endl;
  58. // 3. Quadrant
  59. u[0] = -2.0; u[1] = -1.0;
  60. newton.solve(problem,u);
  61. std::cout << "Nullstelle im 3. Quadrant: " << std::setprecision(15) << u << std::endl;
  62. problem.F(u, result);
  63. std::cout << "Funktionswert: " << std::setprecision(15) << result << std::endl;
  64. // 4. Quadrant
  65. u[0] = 2.0; u[1] = -1.0;
  66. newton.solve(problem,u);
  67. std::cout << "Nullstelle im 4. Quadrant: " << std::setprecision(15) << u << std::endl;
  68. problem.F(u, result);
  69. std::cout << "Funktionswert: " << std::setprecision(15) << result << std::endl;
  70. // Relaxation
  71. hdnum::Banach banach;
  72. banach.set_maxit(5000); // set parameters
  73. banach.set_verbosity(2);
  74. banach.set_reduction(1e-15);
  75. banach.set_abslimit(1e-20);
  76. banach.set_sigma(0.375);
  77. u[0] = 2.0; u[1] = 1.0; // Startwert
  78. banach.solve(problem,u);
  79. std::cout << "Ergebnis: " << std::setprecision(15) << u << std::endl;
  80. return 0;
  81. }