C-Programm für die Newton-Raphson-Methode

C-Programm für die Newton-Raphson-Methode

Die Newton-Raphson-Methode ist eine numerische Methode, die zum Finden einer Wurzel einer Gleichung verwendet wird.
Die Methode erfordert die Kenntnis der Ableitung der Gleichung, deren Wurzel bestimmt werden soll. Also müssten wir das manuell in unseren Code eingeben.

Die Newton-Raphson-Methode konvergiert möglicherweise nicht immer, daher ist es ratsam, den Benutzer zu fragen um die maximale Anzahl der auszuführenden Iterationen einzugeben, falls der Algorithmus nicht gegen eine Wurzel konvergiert. Da die Methode außerdem eine Division durch die Ableitung der Funktion erfordert, sollte man eine Bedingung hinzufügen, die eine Division durch Null verhindert.

Das Folgende ist eine einfache Version des Programms, das die Wurzel findet und die verschiedenen Werte bei jeder Iteration tabelliert. Genau wie jede andere numerische Methode ist die Halbierungsmethode auch eine iterative Methode, daher wird empfohlen, die Werte bei jeder Iteration zu tabellieren.

PROGRAMM (Einfache Version):

/******************************
 ****NEWTON RAPHSON METHOD*****
  2017 (c) Manas Sharma - https://bragitoff.com       
 ******************************/
#include<stdio.h>
#include<math.h>

/*Function whose root is to be determined*/
double f(double x){
  return 3*x+sin(x)-exp(x);
}

/*Derivative of the function whose root is to be determined*/
double df(double x){
  return 3-cos(x)-exp(x);
}

int main(){
  double x,eps,x1;
  int maxSteps;
  printf("Enter the initial guess:n");
  scanf("%lf",&x1);
  printf("Enter the desired accuracy:n");
  scanf("%lf",&eps);
  printf("Enter the max. number of steps:n");
  scanf("%d",&maxSteps);
  int iter=1;
  /*Newton-Raphson Method begins that tabulates the various values at each iteration*/
  printf("____________________________________________________________________________________n");
  printf("xtf(x)ttf'(x)ttx1tt|x-x1|ttf(x1)n");
  printf("____________________________________________________________________________________n");
  do{
    x=x1;
    /* IF-Condition to prevent division by zero[To be done: Check for infinite values too]*/
    if(fabs(df(x))>=0.000000001&&df(x)!=NAN){
      /*New value of x using the NR Expression */
      x1=x-f(x)/df(x);
      printf("%d.t%lft%lft%lft%lft%lfn",iter,f(x),df(x),x1,fabs(x-x1),f(x1));
      iter++;
    }
    else{
      printf("Sorry! The slope is 0 for one of the iterations.n NR Method failed.n");
      return 0;
    }
      
  }while(fabs(x-x1)>=eps&&iter<=maxSteps);
  printf("_______________________________________________________________________________________nnOne of the roots of the given equation is:nn%lfnnn",x1);
  
}

Die bessere Version des obigen Codes verwendet eine Funktion namens 'rootNR', um die NR-Aufgabe auszuführen und die Wurzel zurückzugeben.
Diese Funktion tabelliert jedoch nicht die Werte bei jeder Iteration.
So im Folgenden Programm Ich habe auch eine andere Funktion namens 'printNR' bereitgestellt, die bei jeder Iteration sowohl die Wurzel zurückgibt als auch die verschiedenen Werte ausgibt.

PROGRAMM (bessere Version):

/******************************
 ****NEWTON RAPHSON METHOD*****
  2017 (c) Manas Sharma - https://bragitoff.com       
 ******************************/
#include<stdio.h>
#include<math.h>

/*Function whose root is to be determined*/
double f(double x){
  return x*x*x-27;
}

/*Derivative of the function whose root is to be determined*/
double df(double x){
  return 3*x*x;
}

/*Function that returns the root from Newton-Raphson Method*/
double rootNR(double f(double x),double df(double x),double x1,double eps,double maxSteps){
  double x;
  int i=1;
  do{
    x=x1;
    if(fabs(df(x))>=0.000000001){
      x1=x-f(x)/df(x);
      i++;
    }
  }while(fabs(x-x1)>=eps&&i<=maxSteps);
  return x1;
}

/*Newton-Raphson Method Function that tabulates the values at each iteration*/
double printNR(double f(double x),double df(double x),double x1,double eps,double maxSteps){
  double x;
  int iter=1;
  printf("___________________________________________________________________________________________________n");
  printf("itertxttf(x)ttf'(x)ttx1tt|x-x1|ttf(x1)n");
  printf("___________________________________________________________________________________________________n");
  do{
    x=x1;
    if(fabs(df(x))>=0.000000001){
      x1=x-f(x)/df(x);
      printf("%d.t%lft%lft%lft%lft%lft%lfn",iter,x,f(x),df(x),x1,fabs(x-x1),f(x1));
      iter++;
    }
  }while(fabs(x-x1)>=eps&&iter<=maxSteps);
  return x1;
}
main(){
  double x,eps,x1;
  int maxSteps;
  printf("Enter the initial guess:n");
  scanf("%lf",&x);
  printf("Enter the desired accuracy:n");
  scanf("%lf",&eps);
  printf("Enter the max. number of steps:n");
  scanf("%d",&maxSteps);
  printf("__________________________________________________________________________________________________nnOne of the roots of the given equation is:nn%lfnnn",printNR(f,df,x,eps,maxSteps));
  
}

AUSGABE:

Für x^3-27:

Für 3x+sin(x)-exp(x):

Verwandte Beiträge:

Newton-Raphson C++ Program
Newton-Raphson Lab Manual (Enthält Flussdiagramm und Algorithmus)