Advertisement
Coder_22

Lab

Mar 18th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 KB | None | 0 0
  1. // Newton-Raphson:
  2. double fun(double x) {
  3.     return 3 * x * x + 2 * x - 4;
  4. }
  5. double dfun(double x) {
  6.     return 6 * x + 2;
  7. }
  8. int main() {
  9.     int i = 0; double x = 0;
  10.     do {
  11.         double x1 = x;
  12.         x = x1 - fun(x1) / dfun(x1);
  13.         cout << "At iteration-" << ++i << " , Root = " << x << '\n';
  14.     } while (fabs(fun(x)) > 0.0001);
  15. }
  16.  
  17. // Secant Method:
  18. double fun(double x) {
  19.     return 2 * x * x + 3 * x - 7;
  20. }
  21. int main() {
  22.     int i = 1; double x1 = 5, x2 = 1;
  23.     double x3 = (fun(x2) * x1 - fun(x1) * x2) / (fun(x2) - fun(x1));
  24.     cout << fixed << "At iteration-" << i << " :\n";
  25.     cout << "Root = " << x3 << "\nFunction value = " << fun(x3) << '\n';
  26.     while (fabs((x3 - x2) / x3) > 0.0001) {
  27.         x1 = x2; x2 = x3;
  28.         x3 = (fun(x2) * x1 - fun(x1) * x2) / (fun(x2) - fun(x1));
  29.         cout << "At iteration-" << ++i << " :\n";
  30.         cout << "Root = " << x3 << "\nFunction value = " << fun(x3) << '\n';
  31.     }
  32. }
  33.  
  34. // Guess Eliminaton:
  35. float a[10][10], x[10], ratio;
  36. int i, j, k, n;
  37. cout << setprecision(3) << fixed;
  38. cout << "Enter number of unknowns:";
  39. cin >> n;
  40. cout << "Enter co of augmented matrix:" << '\n';
  41. for (i = 1;i <= n;++i) {
  42.     for (j = 1;j <= n + 1;++j) {
  43.         cout << "a[" << i << "]" << j << "] =";
  44.         cin >> a[i][j];
  45.     }
  46. }
  47. for (k = 1;k <= n - 1;k++) {
  48.     for (i = k + 1;i <= n;++i) {
  49.         ratio = a[i][k] / a[k][k];
  50.         for (j = k;j <= n + 1;++j) {
  51.             a[i][j] -= (ratio * a[k][j]);
  52.         }
  53.     }
  54. }
  55. cout << "after gauess:" << '\n';
  56. for (i = 1;i <= n;++i) {
  57.     for (j = 1;j <= n + 1;++j) {
  58.         cout << a[i][j] << " ";
  59.     }
  60.     cout << '\n';
  61. }
  62. x[n] = a[n][n + 1] / a[n][n];
  63. for (i = n - 1;i >= 1;i--)
  64. {
  65.     x[i] = a[i][n + 1];
  66.     for (j = i + 1;j <= n;++j)
  67.     {
  68.         x[i] = x[i] - a[i][j] * x[j];
  69.     }
  70.     x[i] = x[i] / a[i][i];
  71. }
  72. cout << '\n' << "Solution: " << '\n';
  73. for (i = 1;i <= n;++i)
  74. {
  75.     cout << "x[" << i << "] = " << x[i] << '\n';
  76. }
  77. return 0;
  78. }
  79.  
  80. // Jecobi Method:
  81. int n = 3;
  82. double a[n][n + 1] = { {2,1,1,5},{3,5,2,15},{2,1,4,8} }, xn[3], xo[3] = { 0,0,0 };
  83. int c = 0;
  84. while (true) {
  85.     int f = 0;
  86.     for (int i = 0;i < n;i++) {
  87.         double sum = 0;
  88.         for (int j = 0;j < n;j++) {
  89.             if (j != i) sum += (a[i][j] * xo[j]);
  90.         }
  91.         xn[i] = (a[i][n] - sum) / a[i][i];
  92.     }
  93.     for (int k = 0;k < n;k++) {
  94.         if (abs((xn[k] - xo[k])) > 0.0001) {
  95.             xo[k] = xn[k];
  96.             f++;
  97.         }
  98.     }
  99.     c++;
  100.     if (f == 0)break;
  101. }
  102. for (int i = 0;i < n;i++) {
  103.     cout << xn[i] << " " << endl;
  104. }
  105.  
  106. // Gauss Seidal
  107. int n = 3;
  108. double a[n][n + 1] = { {2,1,1,5},{3,5,2,15},{2,1,4,8} }, xo[3] = { 0,0,0 };
  109. int c = 0;
  110. while (true) {
  111.     int f = 0;
  112.     double xn;
  113.     for (int i = 0;i < n;++i) {
  114.         double sum = 0;
  115.         for (int j = 0;j < n;++j) {
  116.             if (j != i) sum += (a[i][j] * xo[j]);
  117.         }
  118.         double xn = (a[i][n] - sum) / a[i][i];
  119.         if (abs((xn - xo[i]) / xn) > 0.0001) {
  120.             xo[i] = xn;
  121.             f++;
  122.         }
  123.     }
  124.     c++;
  125.     if (f == 0)break;
  126. }
  127. for (int i = 0;i < n;++i) {
  128.     cout << xo[i] << " " << '\n';
  129. }
  130. return 0;
  131. }
  132.  
  133.  
  134. // Least Square
  135. #define f(x) 1/(1+pow(x,2))
  136. int i, n;
  137. double x, y, a, b;
  138. double sx = 0, sxx = 0, sy = 0, sxy = 0;
  139. cout << "Enter the number of values for n : ";
  140. cin >> n;
  141. for (int i = 0;i < n;++i) {
  142.     cout << "Enter the value of x and y : ";
  143.     cin >> x >> y;
  144.     sx += x;
  145.     sy += y;
  146.     sxx += (x * x);
  147.     sxy += (x * y);
  148. }
  149. double xb = sx / n;
  150. double yb = sy / n;
  151. b = (sxy - yb * sx) / (sxx - xb * sx);
  152. a = (sy - b * sx) / n;
  153. cout << "Values of a and b are : " << a << " " << b << '\n';
  154.  
  155.  
  156. // Simpson 3/8
  157. #define f(x) 1/(1+pow(x,2))
  158. double lower, upper, i = 0.0, h, k;
  159. int i, n;
  160. cout << "Enter lower limit of integration: ";
  161. cin >> lower;
  162. cout << "Enter upper limit of integration: ";
  163. cin >> upper;
  164. cout << "Enter number of sub intervals: ";
  165. cin >> n;
  166. h = (upper - lower) / n;
  167. i = f(lower) + f(upper);
  168. for (i = 1; i <= n - 1; ++i)
  169. {
  170.     k = lower + i * h;
  171.     if (i % 3 == 0) i = i + 2 * (f(k));
  172.     else i = i + 3 * (f(k));
  173. }
  174.  
  175. i = i * h * 3.0 / 8.0;
  176.  
  177. cout << '\n' << "Area is: " << i;
  178.  
  179. // Simpson 1/3
  180. #define f(x) 1/(1+pow(x,2))
  181. double lower, upper, i = 0.0, h, k;
  182. int i, n;
  183. cout << "Enter lower limit of integration: ";
  184. cin >> lower;
  185. cout << "Enter upper limit of integration: ";
  186. cin >> upper;
  187. cout << "Enter number of sub intervals: ";
  188. cin >> n;
  189. h = (upper - lower) / n;
  190.  
  191. i = f(lower) + f(upper);
  192.  
  193. for (i = 1; i <= n - 1; ++i)
  194. {
  195.     k = lower + i * h;
  196.     if (i % 2 == 0) i = i + 2 * (f(k));
  197.     else i = i + 4 * (f(k));
  198. }
  199.  
  200. i = i * h / 3;
  201.  
  202. cout << '\n' << "Area is: " << i;
  203.  
  204. /// Trapizoidal
  205. #define f(x) 1/(1+pow(x,2))
  206. double lower, upper, i = 0.0, h, k;
  207. int i, n;
  208. cout << "Enter lower limit of integration: ";
  209. cin >> lower;
  210. cout << "Enter upper limit of integration: ";
  211. cin >> upper;
  212. cout << "Enter number of sub intervals: ";
  213. cin >> n;
  214. h = (upper - lower) / n;
  215.  
  216. i = f(lower) + f(upper);
  217.  
  218. for (i = 1; i <= n - 1; ++i)
  219. {
  220.     k = lower + i * h;
  221.     i = i + 2 * (f(k));
  222. }
  223.  
  224. i = i * h / 2;
  225.  
  226. cout << '\n' << "Area is: " << i;
  227.  
  228. // Lagrange
  229. double x[100], y[100], xp, yp = 0, p;
  230. int i, j, n;
  231. cout << "Enter number of data: ";
  232. cin >> n;
  233. cout << "Enter data:" << '\n';
  234. for (i = 1;i <= n;++i)
  235. {
  236.     cout << "x[" << i << "] = ";
  237.     cin >> x[i];
  238.     cout << "y[" << i << "] = ";
  239.     cin >> y[i];
  240. }
  241. cout << "Enter interpolation point: ";
  242. cin >> xp;
  243. for (i = 1;i <= n;++i)
  244. {
  245.     p = 1;
  246.     for (j = 1;j <= n;++j)
  247.     {
  248.         if (i != j)
  249.         {
  250.             p = p * (xp - x[j]) / (x[i] - x[j]);
  251.         }
  252.     }
  253.     yp = yp + p * y[i];
  254. }
  255. cout << '\n' << "Interpolated value at " << xp << " is " << yp;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement