Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1.  
  2. //#include "stdafx.h"
  3. #define _USE_MATH_DEFINES
  4. #include <math.h>
  5. #include <iostream>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. void falseposition();
  12. void newtonraphson();
  13. void nonlinear();
  14.  
  15.  
  16.  
  17. int main()
  18. {
  19. cout << "choose the method" << endl;
  20. cout << "enter 1 for false position" << endl;
  21. cout << "enter 2 for modified" << endl;
  22. cout << "enter 3 for nonlinear" << endl;
  23.  
  24. int choice;
  25. cin >> choice;
  26.  
  27. if (choice == 1) falseposition();
  28. if (choice == 2) newtonraphson();
  29. if (choice == 3) nonlinear();
  30.  
  31. switch(choice){
  32. case 1: falseposition(); break;
  33. case 2: newtonraphson(); break;
  34. case 3: nonlinear(); break;
  35. default: falseposition();
  36. }
  37.  
  38. return 0;
  39. }
  40.  
  41. void nonlinear(){
  42. double u, v, dux, duy, dvx, dvy, xi, yi, xr, yr;;
  43.  
  44. cout << "enter xi" << endl;
  45. cin >> xi;
  46. cout << "enter yi" << endl;
  47. cin >> yi;
  48.  
  49. for (int i = 1; i < 1000; i++)
  50. {
  51. u = 0.45 * cos(xi) - 0.28 * cos(yi) - 0.275;
  52. v = 0.45 * sin(xi) - 0.28 * sin(yi) - 0.13;
  53. dux = -0.45 * sin(xi);
  54. duy = 0.28 * sin(yi);
  55. dvx = 0.45 * cos(xi);
  56. dvy = -0.28 * cos(yi);
  57. xr = xi - ((u * dvy - v * duy) / (dux * dvy - duy * dvx));
  58. yr = yi - ((v * dux - u * dvx) / (dux * dvy - duy * dvx));
  59.  
  60. xi = xr; yi = yr;
  61. }
  62.  
  63. cout << "xr = " << xr << endl;
  64. cout << "yr = " << yr << endl;
  65.  
  66. }
  67.  
  68. void falseposition(){
  69. double tmpx, tmpy,xl, xu, fxl, fxu, xr, fxr, d = 0, error;
  70. int i;
  71. cout << "enter lower interval" << endl;
  72. cin >> tmpx;
  73. cout << "enter upper interval" << endl;
  74. cin >> tmpy;
  75. if (tmpx < tmpy) {
  76. xl = tmpx;
  77. xu = tmpy;
  78. }
  79. else {
  80. xl = tmpy;
  81. xu = tmpx;
  82. }
  83.  
  84. for (i = 0; i < 10; i++){
  85.  
  86. fxl = ((sin(2.0 * M_PI * xl / 16) * (cos(2 * M_PI * 12 * 48 / 16))) + pow(M_E, -xl)) - 0.8;
  87. fxu = ((sin(2.0 * M_PI * xu / 16) * (cos(2 * M_PI * 12 * 48 / 16))) + pow(M_E,-xu)) - 0.8;
  88.  
  89. if ((fxl * fxu) < 0){
  90. xr = xu - (fxu * (xl - xu) / (fxl - fxu));
  91.  
  92. fxr = ((sin(2.0 * M_PI * xr / 16) * (cos(2.0 * M_PI * 12 * 48 / 16))) + pow(M_E, -xr)) - 0.8;
  93. error = (xr - d) / xr;
  94. d = xr;
  95.  
  96. if ((fxl * fxr) < 0){
  97. xu = xr;
  98. }
  99. else if ((fxl * fxr) > 0){
  100. xl = xr;
  101. }
  102. else if(fxr==0) {
  103. cout << "xr =" << xr << endl;
  104. cout << "error = " << error * 100 << "%" << endl;
  105. break;
  106. }
  107. }
  108. else {
  109.  
  110. cout << "error!" << endl;
  111. break;
  112. }
  113. }
  114. if (i == 10) {
  115. cout << "xr = " << xr << endl;
  116. cout << "error = " << error * 100 << "%" << endl;
  117. }
  118.  
  119. }
  120. void newtonraphson(){
  121. double fx, fpx, fdpx, x, xr,fxr;
  122. cout<< "Enter the guess value" <<endl;
  123. cin >> x;
  124.  
  125. for (int i = 1; i < 1000; i++) {
  126. fx = pow((pow(M_E, x) + x - 20), 3);
  127. fpx = (pow((pow(M_E, x) + x - 20), 2)) * (3 * pow(M_E, x) + 3);
  128. fdpx = (pow(M_E, x) + x - 20) * (pow(M_E, x) + 1) * ((3 * pow(M_E, x) + 3)) + (pow((pow(M_E, x) + x - 20), 2)) * (3 * pow(M_E, x));
  129. xr = x - ((fx * fpx) / (fpx * fpx - fx * fdpx));
  130. x = xr;
  131. }
  132.  
  133.  
  134. cout << "xr = " << xr << endl;
  135. cout<< (fx > -0.000000001 && fx < 0.00000000001)?"converges":"diverges";
  136.  
  137.  
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement