Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // Thanasis Tsounakis 2021 NEWTON-RAPHSON METHOD
- #include <stdlib.h>
- #include <math.h>
- double f(double x){ // f(x) function
- return x + exp(-10 * pow(x,2)) * cos(x);
- }
- double df(double x){ // f'(x) derivative function of f(x)
- return 1.0 - exp(-10 * pow(x,2)) * ( sin(x) + 20 * x * cos(x));
- }
- double ddf(double x){ // f''(x) derivative function of f(x)
- return exp(-10 * pow(x,2)) * (400 * pow(x,2) * cos(x) + 40 * x * sin(x) - 21 * cos(x));
- }
- int main(){
- double x, x0 = -0.7; // variable x and initial value of x (x_0)
- double error;
- int k = -10, i = 0;
- printf("i:\tx:\t\tf(x):\t\tError:\n");
- do{
- i++;
- x = x0 - 2 * (f(x0) * df(x0) / (2*pow(df(x0),2) - f(x0)*ddf(x0)));
- error = fabs(x - x0);
- printf("%3d\t%14.10f\t%14.10f\t%14.10f\n", i, x, f(x), error); // print the data of the calculations
- x0 = x;
- } while(error >= 0.5 * pow(10.0,k)); // do until error is smaller than 10^-10
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement