Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. // mo3.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <cmath>
  7. #include <tuple>
  8. #include <iomanip>
  9.  
  10. const int MAX_IT = 30; // maksymalna ilosc iteracji
  11. const double TOL_X = 10e-10; //kryterium dokladnosci wyznaczenia Xn
  12. const double TOL_F = 10e-10; //kryterium wiarygodnosci Xn jako przyblizenia pierwiastka
  13.  
  14.  
  15. double fun1(double x) {
  16. return sin(x / 4) * sin(x / 4) - x;
  17. }
  18. double fun1pic(double x) {
  19. return sin(x / 4) * sin(x / 4);
  20. }
  21. double fun1prim(double x) {
  22. // 1/2*sin(x/4)*cos(x/4)-1
  23. return sin(x / 2) / 4 - 1;
  24. }
  25.  
  26. double fun2(double x) {
  27. return tan(2 * x) - x - 1;
  28. }
  29. double fun2pic(double x) {
  30. return 2 / (cos(2 * x) * cos(2 * x));
  31. }
  32. double fun2pic2(double x) {
  33. return 1 / (2 * (x + 1) * (x + 1) + 2);
  34. }
  35. double fun2prim(double x) {
  36. return 2 / (cos(2 * x) * cos(2 * x)) - 1;
  37. }
  38.  
  39.  
  40. std::tuple<double, double> bisekcja(double(*fun)(double), double a,double b) {
  41. double rez, e, x, fx; // reziduum, estymator, x, f(x)
  42. double fa = fun(a); // f(a)
  43. double fb = fun(b); // f(b)
  44.  
  45. std::cout << std::endl << "Bisekcja" << std::endl;
  46.  
  47. if (fa * fb > 0) {
  48. std::cout << "f(a) i f(b) maja ten sam znak";
  49. return std::make_tuple(12345, 54321);
  50. }
  51.  
  52. if (fa * fb == 0) {
  53. if (fa == 0) {
  54. std::cout << "Miejsce zerowe w x = " << a << std::endl;
  55. return std::make_tuple(a, fa);
  56. }
  57. if (fb == 0) {
  58. std::cout << "Miejsce zerowe w x = " << b << std::endl;
  59. return std::make_tuple(b, fb);
  60. }
  61. }
  62.  
  63. std::cout << "IT" << std::setw(9) << "x:" << std::setw(29) << "f(x):" << std::setw(31) << "Estymator:" << std::setw(25) << "Reziduum:" << std::endl;
  64. std::cout << std::scientific << std::setprecision(16);
  65.  
  66. for (int i = 1; i <= MAX_IT; i++) {
  67. x = (a + b) / 2;
  68. fx = fun(x);
  69. e = fabs(b - a) / 2;
  70. rez = fabs(fx);
  71.  
  72. std::cout << std::setw(2) << std::right << i << ".\t" << std::right << std::setw(23) << x << " " << std::setw(23) << fx << " " << std::setw(23) << e << " " << std::setw(23) << rez << std::endl;
  73. if (e < TOL_X || rez < TOL_F) {
  74. break;
  75. }
  76. if (fa * fx <= 0) {
  77. b = x;
  78. fb = fx;
  79. }
  80. else {
  81. a = x;
  82. fa = fx;
  83. }
  84. }
  85. return std::make_tuple(x, fx);
  86. }
  87. std::tuple<double, double> picard(double(*fun)(double), double(*pic)(double), double x) {
  88. double fx, e, rez;
  89.  
  90. std::cout << std::endl << "Picard" << std::endl;
  91.  
  92. if (fabs(pic(x)) > 1) {
  93. std::cout << "Funkcja rozbiezna!" << std::endl;
  94. return std::make_tuple(12345, 54321);
  95. }
  96.  
  97. std::cout << "IT" << std::setw(9) << "x:" << std::setw(29) << "f(x):" << std::setw(31) << "Estymator:" << std::setw(25) << "Reziduum:" << std::endl;
  98. std::cout << std::scientific << std::setprecision(16);
  99.  
  100. for (int i = 1; i <= MAX_IT; i++) {
  101. fx = fun(x);
  102. e = fabs(x - fx);
  103. rez = fabs(fx);
  104.  
  105. std::cout << std::setw(2) << std::right << i << ".\t" << std::right << std::setw(23) << x << " " << std::setw(23) << fx << " " << std::setw(23) << e << " " << std::setw(23) << rez << std::endl;
  106.  
  107. if (e < TOL_X || rez < TOL_F) {
  108. break;
  109. }
  110.  
  111. x = fx;
  112. }
  113.  
  114. return std::make_tuple(x, fx);
  115. }
  116. std::tuple<double, double> newton(double(*fun)(double), double(*prim)(double), double xn) {
  117. double xn1, e, rez, fx, fpx;
  118.  
  119. std::cout << std::endl << "Newton" << std::endl;
  120. std::cout << "IT" << std::setw(9) << "x:" << std::setw(29) << "f(x):" << std::setw(31) << "Estymator:" << std::setw(25) << "Reziduum:" << std::endl;
  121. std::cout << std::scientific << std::setprecision(16);
  122.  
  123. for (int i = 1; i <= MAX_IT; i++) {
  124. fx = fun(xn);
  125. fpx = prim(xn);
  126. xn1 = xn - fx / fpx;
  127. e = fabs(xn - xn1);
  128. rez = fabs(fx);
  129.  
  130. std::cout << std::setw(2) << std::right << i << ".\t" << std::right << std::setw(23) << xn << " " << std::setw(23) << fx << " " << std::setw(23) << e << " " << std::setw(23) << rez << std::endl;
  131.  
  132. if (e < TOL_X || rez < TOL_F) {
  133. break;
  134. }
  135. xn = xn1;
  136. }
  137. return std::make_tuple(xn, fx);
  138. }
  139. std::tuple<double, double> sieczne(double(*fun)(double), double xn, double xn1) {
  140. double xn2, fxn2, e, rez;
  141. double fxn = fun(xn);
  142. double fxn1 = fun(xn1);
  143.  
  144. if ((xn - xn1 == 0) || (fxn - fxn1 == 0)) {
  145. std::cout << "Bledne xn i xn1" << std::endl;
  146. return std::make_tuple(12345, 54321);
  147. }
  148. std::cout << std::endl << "Sieczne" << std::endl;
  149. std::cout << "IT" << std::setw(9) << "x:" << std::setw(29) << "f(x):" << std::setw(31) << "Estymator:" << std::setw(25) << "Reziduum:" << std::endl;
  150.  
  151. for (int i = 1; i <= MAX_IT; i++) {
  152. fxn = fun(xn);
  153. fxn1 = fun(xn1);
  154.  
  155. if ((xn - xn1 == 0) || (fxn - fxn1 == 0)) {
  156. std::cout << "Bledne xn i xn1" << std::endl;
  157. return std::make_tuple(12345, 54321);
  158. }
  159.  
  160. xn2 = xn1 - fxn1 / ((fxn1 - fxn) / (xn1 - xn));
  161. fxn2 = fun(xn2);
  162. e = fabs(xn - xn1);
  163. rez = fabs(fxn2);
  164.  
  165. std::cout << std::setw(2) << std::right << i << ".\t" << std::right << std::setw(23) << xn2 << " " << std::setw(23) << fxn2 << " " << std::setw(23) << e << " " << std::setw(23) << rez << std::endl;
  166.  
  167. if (e < TOL_X || rez < TOL_F) {
  168. break;
  169. }
  170. xn = xn1;
  171. xn1 = xn2;
  172. }
  173. return std::make_tuple(xn2, fxn2);
  174. }
  175.  
  176.  
  177. int main()
  178. {
  179. std::cout << "Funkcja sin^2(x/4) - x = 0";
  180. std::tuple<double, double> f1_picard = picard(fun1, fun1pic, 1);
  181. std::tuple<double, double> f1_bisekcja = bisekcja(fun1, -1, 2);
  182. std::tuple<double, double> f1_newton = newton(fun1, fun1prim, 1);
  183. std::tuple<double, double> f1_sieczne = sieczne(fun1, 1, 3);
  184. std::cout << std::endl << "Obliczone x0:" << std::endl;
  185. std::cout << "Metoda Picarda:\t\t" << "x = " << std::get<0>(f1_picard) << "\tf(x) = " << std::setw(23) << std::get<1>(f1_picard) << std::endl;
  186. std::cout << "Metoda bisekcji:\t" << "x = " << std::get<0>(f1_bisekcja) << "\tf(x) = " << std::setw(23) << std::get<1>(f1_bisekcja) << std::endl;
  187. std::cout << "Metoda Newtona:\t\t" << "x = " << std::get<0>(f1_newton) << "\tf(x) = " << std::setw(23) << std::get<1>(f1_newton) << std::endl;
  188. std::cout << "Metoda siecznych:\t" << "x = " << std::get<0>(f1_sieczne) << "\tf(x) = " << std::setw(23) << std::get<1>(f1_sieczne) << std::endl;
  189.  
  190. std::cout << "Funkcja tg(2x) - x - 1 = 0";
  191. std::tuple<double, double> f2_picard = picard(fun2, fun2pic2, 1);
  192. std::tuple<double, double> f2_bisekcja = bisekcja(fun2, -1, 2);
  193. std::tuple<double, double> f2_newton = newton(fun2, fun2prim, 1);
  194. std::tuple<double, double> f2_sieczne = sieczne(fun2, 1, 3);
  195. std::cout << std::endl << "Obliczone x0:" << std::endl;
  196. std::cout << "Metoda Picarda:\t\t" << "x = " << std::get<0>(f2_picard) << "\tf(x) = " << std::setw(23) << std::get<1>(f2_picard) << std::endl;
  197. std::cout << "Metoda bisekcji:\t" << "x = " << std::get<0>(f2_bisekcja) << "\tf(x) = " << std::setw(23) << std::get<1>(f2_bisekcja) << std::endl;
  198. std::cout << "Metoda Newtona:\t\t" << "x = " << std::get<0>(f2_newton) << "\tf(x) = " << std::setw(23) << std::get<1>(f2_newton) << std::endl;
  199. std::cout << "Metoda siecznych:\t" << "x = " << std::get<0>(f2_sieczne) << "\tf(x) = " << std::setw(23) << std::get<1>(f2_sieczne) << std::endl;
  200. }
  201.  
  202. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  203. // Debug program: F5 or Debug > Start Debugging menu
  204.  
  205. // Tips for Getting Started:
  206. // 1. Use the Solution Explorer window to add/manage files
  207. // 2. Use the Team Explorer window to connect to source control
  208. // 3. Use the Output window to see build output and other messages
  209. // 4. Use the Error List window to view errors
  210. // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  211. // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement