Magentax

Newton Raphson

Jan 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1.        //New-Raps
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <math.h>
  6.  
  7. #define N 16
  8.  
  9. typedef unsigned char LOGIC;
  10. LOGIC Newton_Rapson(int,float [],float,float,float *,int);
  11. float Valpol(int,float [],float);
  12. float Derpol(int,float [],float);
  13.  
  14. int main(void)
  15. {
  16.   int n=2;              // gradul polinomului
  17.   float A[]={-6,-1,1};  // vectorul coeficientilor polinomului
  18.   float x0=7;           // punctul de start
  19.   float er=0.0001;      // precizia de calcul a radacinii
  20.   float rad;
  21.  
  22.   if(Newton_Rapson(n,A,x0,er,&rad,N)) printf("\nSolutia este : %f",rad);
  23.   else printf("\nEroare");
  24.  
  25.   getch();
  26.   return 1;
  27. }
  28. LOGIC Newton_Rapson(int n,float A[],float x0,float er,float *pRad,int nmax)
  29. {
  30.    float xn,xn_1;
  31.  
  32.    xn=x0;
  33.    do{
  34.       xn_1=xn;
  35.       if(Derpol(n,A,xn_1)==0) { return(0);}
  36.       xn=xn_1-(Valpol(n,A,xn_1)/Derpol(n,A,xn_1));
  37.       nmax=nmax-1;
  38.       } while(fabs(xn-xn_1)>=er && nmax!=0);
  39.  
  40.    *pRad=xn;
  41.    return(1);
  42. }
  43.  
  44. float Valpol(int n,float A[],float pc)
  45. {
  46.   int i;
  47.   float b;
  48.  
  49.   b=A[n];
  50.   for(i=n-1;i>=0;i--) b=A[i]+pc*b;
  51.  
  52.   return b;
  53. }
  54.  
  55. float Derpol(int n,float A[],float pc)
  56. {
  57.   int i;
  58.   float b[N];
  59.   float c;
  60.  
  61.   b[n]=A[n];
  62.   c=b[n];
  63.   for(i=n-1;i>=1;i--)
  64.   {
  65.    b[i]=A[i]+pc*b[i+1];
  66.    c=b[i]+pc*c;
  67.    }
  68.   return c;
  69. }
Add Comment
Please, Sign In to add comment