Advertisement
MaPV

2VP_3_beseq_newton

May 31st, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <conio.h>             
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. double func (double x){
  10.     return (x*x+4*cos(x)-4);
  11.     //return (2*x);
  12. }
  13. double der_func (double x){
  14.     return(2*x-4*sin(x));
  15. }
  16.  
  17.  
  18. double bisequcie (double a,double b,double eps){
  19.     double y;
  20.     double xl=a;
  21.     double xr=b;
  22.  
  23.         while (abs(xr-xl)>eps){ //точность
  24.         y=(xl+xr)/2;
  25.         if (func(y)==0) return y;  //проверка на сходимость при начальном дроблении
  26.         else if ((func(xl)<0 && func(y)>0) || (func(xl)>0 && func(y)<0)) //условие разных знаков на концах интервала
  27.         //else if (func ((xl + xr) / 2) * func (xl) < func ((xl + xr) / 2)* func (xr))
  28.         xr=y; else xl=y; //сокращаем интервал
  29.         }
  30.     return (xl+xr)/2;
  31.     }
  32.  
  33. double NEWTON (double x0,double eps){
  34. double x_prev=x0-eps;
  35. double x=x0;
  36. double buffer=0;
  37. while (abs (x - x_prev) * 2 > eps){
  38. x_prev=x;
  39. x=x-(func(x)/der_func(x));
  40. }
  41. return x;
  42. }
  43.  
  44.  
  45. int main (){
  46.     setlocale (LC_ALL,"Russian");
  47.     cout.precision (8);
  48.  
  49.     cout << "Решение по бисекциям : " << bisequcie (-7,7,0.00000001) <<endl;
  50.     cout<< "Решение по Ньютону : "<<NEWTON(5,0.00000001);
  51.     system ("pause");
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement