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));
- }
- int main(){
- double x, x0 = -0.7, x1 = 0; // variable x and value of x1 (x_n) and x0 (x_n-1)
- double error;
- int k = -10, i = 1;
- printf("i:\tx:\t\tf(x):\t\tError:\n");
- do{
- i++;
- x = x1 - f(x1) * (x1 - x0) / (f(x1) - f(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 = x1;
- x1 = 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