Advertisement
Guest User

Lab1 pop

a guest
Feb 24th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. newton 1================================================
  2. #include <iostream>
  3. #include <math.h>
  4. #include<conio.h>
  5. using namespace std;
  6.  
  7. float f(float x) {
  8. return x * exp(x) - 1;
  9.  
  10. }
  11.  
  12. float g(float x) {
  13.  
  14. return exp(x)*(x + 1);
  15. }
  16.  
  17.  
  18. void main() {
  19. const float eps =1e-5;
  20. const int ITMAX = 100;
  21. float x = 0;
  22. int n = 0;
  23.  
  24.  
  25.  
  26. while ((fabs(f(x)) > eps) && n<=ITMAX) {
  27.  
  28. x = (x - f(x) / g(x));
  29.  
  30. n = n + 1;
  31.  
  32. }
  33. if (n > ITMAX)
  34. cout << "Numarul maxim de iteratii depasit";
  35. else {
  36. cout << "Valoarea aproximativa a solutiei este" << x << endl;
  37. cout << "Numarul de iteratii este:" << n;
  38. }
  39.  
  40. _getch();
  41.  
  42. }
  43.  
  44. NEWTON 2===================================================================
  45. #include <iostream>
  46. #include <math.h>
  47. #include<conio.h>
  48. #include<stdio.h>
  49.  
  50.  
  51. using namespace std;
  52.  
  53. void newton_radical(double x, int itmax, double eps, int p, double a) {
  54. int n = 1;
  55. while ((fabs(pow(x, p) - a) > eps) && (n <= itmax)) {
  56. x = (1.0 / p) * ((x * (p - 1)) + (a / (pow(x, p - 1))));
  57. n = n + 1;
  58. }
  59. if (n > itmax) {
  60. printf("In %d nu a fost realizata aproximarea dorita", itmax);
  61. printf("\n");
  62. }
  63. else {
  64. printf("Aproximarea obtinuta este %lf", x);
  65. printf("\n");
  66. }
  67.  
  68. }
  69.  
  70. void main() {
  71. double a;
  72. int p;
  73. double x;
  74. double eps;
  75. int itmax;
  76. printf("Introduceti Aproximatia initiala: ");
  77. scanf_s("%lf", &x);
  78. printf("\n");
  79. printf("Precizia de eroare: ");
  80. scanf_s("%lf", &eps);
  81. printf("\n");
  82. printf(" Introduceti numarul de iteratii maxime: ");
  83. scanf_s("%d", &itmax);
  84. printf("\n");
  85. printf("Introduceti numarul real: ");
  86. scanf_s("%lf", &a);
  87. printf("\n");
  88. printf(" Introduceti ordinul radicalului: ");
  89. scanf_s("%d", &p);
  90. printf("\n");
  91. newton_radical(x, itmax, eps, p, a);
  92. _getch();
  93. }
  94.  
  95. NEWTON 3=============================================================================
  96. #include <iostream>
  97. #include <math.h>
  98. #include<conio.h>
  99. #include<stdio.h>
  100.  
  101.  
  102. using namespace std;
  103.  
  104. double fun(double x) {
  105. return((x*exp(x)) - 1);
  106. }
  107.  
  108.  
  109. void secanta(double x0, double x1, int itmax, double eps) {
  110. int n = 2;
  111. double x;
  112. while ((fabs(fun(x1) > eps) && (n <= itmax))) {
  113. x = x1 - ((fun(x1) * (x1 - x0)) / (fun(x1) - fun(x0)));
  114. x0 = x1;
  115. x1 = x;
  116. n = n + 1;
  117. }
  118. if (n > itmax) {
  119. printf("In %d nu a fost realizata aproximarea dorita", itmax);
  120. printf("\n");
  121. }
  122. else {
  123. printf("Aproximarea obtinuta este %lf", x);
  124. printf("\n");
  125. }
  126.  
  127. }
  128.  
  129. void main() {
  130. double x0, x1;
  131. double eps;
  132. int itmax;
  133. printf("Aproximatia initiala x0= ");
  134. scanf_s("%lf", &x0);
  135. printf("\n");
  136. printf("Aproximatia initiala x1= ");
  137. scanf_s("%lf", &x1);
  138. printf("\n");
  139. printf("Diferenta de eroare: ");
  140. scanf_s("%lf", &eps);
  141. printf("\n");
  142. printf("Numarul de iteratii maxime: ");
  143. scanf_s("%d", &itmax);
  144. printf("\n");
  145. secanta(x0, x1, itmax, eps);
  146. _getch();
  147. }
  148.  
  149. NEWTON 4=============================================
  150. #include <iostream>
  151. #include <math.h>
  152. #include<conio.h>
  153. #include<stdio.h>
  154.  
  155.  
  156. using namespace std;
  157.  
  158. double fun(double x) {
  159. return exp(-x);
  160. }
  161.  
  162.  
  163. void aprox_succes(double x, int itmax, double eps) {
  164. int n = 1;
  165. while ((fabs(x - fun(x)) > eps) && (n <= itmax)) {
  166. x = fun(x);
  167. n = n + 1;
  168. }
  169. if (n > itmax) {
  170. printf("In %d nu a fost realizata aproximarea dorita", itmax);
  171. printf("\n");
  172. }
  173. else {
  174. printf("Aproximarea obtinuta este %lf", x);
  175. printf("\n");
  176. printf("Numarul de iteratii efectuate: %d", n);
  177. }
  178.  
  179. }
  180. void main() {
  181. double x;
  182. double eps;
  183. int itmax;
  184. printf("Aproximatia initiala x= ");
  185. scanf_s("%lf", &x);
  186. printf("\n");
  187. printf("Diferenta de eroare: ");
  188. scanf_s("%lf", &eps);
  189. printf("\n");
  190. printf("Numarul de iteratii maxime: ");
  191. scanf_s("%d", &itmax);
  192. printf("\n");
  193. aprox_succes(x, itmax, eps);
  194. _getch();
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement