Advertisement
Guest User

Mehedi

a guest
Jun 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. #include<conio.h>
  4. using namespace std;
  5.  
  6. #define f(x) (x*x)+x-2
  7. #define E 0.00001
  8. int main()
  9. {
  10. float x1,x2,f0,f1,f2,x0,root;
  11. int i=1;
  12. cout<<"Enter two limits: ";
  13. cin>>x1>>x2;
  14. f1=f(x1);
  15. f2=f(x2);
  16. if(f1*f2>0)
  17. {
  18. cout<<"x1 &x2 do not bracket any root";
  19. goto stop;
  20. }
  21. while (1)
  22. {
  23. x0=x1-((f1*(x2-x1))/(f2-f1));
  24. f0=f(x0);
  25. if(f1*f0<0)
  26. {
  27. x2=x0;
  28. f2=f0;
  29. }
  30.  
  31. else{
  32. x1=x0;
  33. f1=f0;
  34. }
  35. cout.precision(6);
  36. cout<<"\nIteration " <<i <<"\t"<<fixed<<x1<<"\t\t"<<fixed<<x2;
  37.  
  38. if (fabs((x2-x1)/x2)<E)
  39. {
  40. root = (x1+x2)*0.5;
  41. cout.precision(6);
  42. cout<<"\n\nFinal iteration "<<i+1<<"\tRoot = "<<fixed<<root;
  43. goto stop;
  44.  
  45. }
  46. else
  47. i++;
  48. }
  49.  
  50. stop:
  51. getch();
  52.  
  53. return 0;
  54. }
  55.  
  56. newton
  57.  
  58. #include<iostream>
  59. #include<math.h>
  60. using namespace std;
  61.  
  62. #define E 0.000001
  63. #define MAX 20
  64. #define F(x) (x*x)+x-2
  65. #define FD(x) 2*x+1
  66.  
  67. int main()
  68. {
  69. float x0,xn,fx,fdx,fv;
  70. int i=1;
  71. cout<<"Input initial value of x"<<endl;
  72. cin>>x0;
  73. cout<<"SOLUTION BY NEWTON-RAPHSON METHOD"<<endl;
  74. m3014:
  75. fx=F(x0);
  76. fdx = FD(x0);
  77. xn=x0-fx/fdx;
  78. fv = F(xn);
  79.  
  80. if(fabs((xn-x0)/xn)<E)
  81. {
  82. cout.precision(7);
  83. cout<<"Root = "<<fixed<<xn<<endl;
  84. cout.precision(7);
  85. cout<<"Function value = "<<fixed<<fv<<endl;
  86. cout<<"Number of iteration = "<<i<<endl;
  87. }
  88. else{
  89. x0 = xn;
  90. i++;
  91. if(i<MAX)
  92. goto m3014;
  93. else{
  94. cout<<"SOLUTION DOES NOT CONVERGE"<<endl;
  95. cout<<"IN "<<MAX<<" ITERATIOMS"<<endl;
  96. }
  97. }
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement