Advertisement
frentzy

calcul numeric lab 1

Feb 21st, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #pragma warning(disable:4996)
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <string>
  7. #include <math.h>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11.  
  12. double func1(double x) {
  13.     return x*exp(x) - 1;
  14. }
  15.  
  16. double func2(double x) {
  17.     double xderiv = x;
  18.     return (xderiv + 1)*exp(x);
  19. }
  20.  
  21. void main() {
  22.     double epsilon;
  23.     double x, ITMAX;
  24.     int n = 1;
  25.     x = 0;
  26.     epsilon = 0.001;
  27.     ITMAX = 100;
  28.  
  29.     while ( (fabs(x*exp(x)-1) > epsilon ) && (n <= ITMAX)) {
  30.         x = x - (func1(x) / func2(x));
  31.         n = n + 1;
  32.     }
  33.     if (n > ITMAX) {
  34.         cout << "In" << ITMAX << " iteratii nu a fost realizata aproximarea dorita";
  35.     }
  36.     else {
  37.         cout << "Aproximarea obtinuta este: " << x;
  38.     }
  39.  
  40.     _getche();
  41. }
  42.  
  43. //#####################################################################################
  44. #pragma warning(disable:4996)
  45. #include <stdio.h>
  46. #include <conio.h>
  47. #include <stdlib.h>
  48. #include <iostream>
  49. #include <string>
  50. #include <math.h>
  51. #include <cmath>
  52.  
  53. using namespace std;
  54.  
  55. #define func
  56.  
  57. void main() {
  58.     double a=2,x=2,epsilon=0.001,ITMAX=100;
  59.     double p=2;
  60.     int n = 1;
  61.     while (fabs(pow(x,p)-a) > epsilon && n <= ITMAX) {
  62.         x = (1 / p) * ((p - 1)*x + a / (pow(x, p - 1)));
  63.         n = n + 1;
  64.     }
  65.  
  66.     if (n > ITMAX) {
  67.         cout << "In" << ITMAX << "iteratii nu a fost realizata aproximarea dorita";
  68.     }
  69.     else {
  70.         cout << "Aproximarea obtinuta este: " << x;
  71.     }
  72.  
  73.     _getche();
  74. }
  75.  
  76. //##############################################################################################
  77. #pragma warning(disable:4996)
  78. #include <stdio.h>
  79. #include <conio.h>
  80. #include <stdlib.h>
  81. #include <iostream>
  82. #include <string>
  83. #include <math.h>
  84. #include <cmath>
  85.  
  86. using namespace std;
  87.  
  88. double func(double x) {
  89.     double xx = x;
  90.     return xx*exp(xx) - 1;
  91. }
  92.  
  93. void main() {
  94.     double x,x_0, x_1, epsilon, ITMAX;
  95.     int n;
  96.  
  97.     n = 2;
  98.     epsilon = 0.001;
  99.     x = x_0 =  0;
  100.     x_1 = 1;
  101.     ITMAX = 100;
  102.     while (fabs(func(x_1)) > epsilon && n <= ITMAX) {
  103.         x = x_1 - ((func(x_1)*(x_1 - x_0)) / (func(x_1) - func(x_0)));
  104.         x_0 = x_1;
  105.         x_1 = x;
  106.         n = n + 1;
  107.     }
  108.     if (n > ITMAX) {
  109.         cout << "in" << ITMAX << "iteratii nu a fost realizata aproximarea dorita";
  110.     }
  111.     else {
  112.         cout << "Aproximarea obtinuta este:" << x;
  113.     }
  114.  
  115.     _getche();
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement