-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton_raphson.c
58 lines (44 loc) · 1017 Bytes
/
newton_raphson.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <math.h>
#include <conio.h>
#define MAX_ERROR 0.01
double f(double x)
{
double value = pow(x,3) - 5*pow(x,2) + 7*x - 3;
return value;
}
double fdash(double x)
{
double value = 3*pow(x,2) - 10*x + 7;
return value;
}
int main()
{
double x,x_new,root,f_val,fdash_val,diff;
printf("Initial x: ");
scanf("%lf",&x);
printf("\n\n");
while(1)
{
f_val = f(x);
fdash_val = fdash(x);
x_new = x - (f_val/fdash_val);
diff = x_new - x;
printf("f(x) = %lf \n",f_val);
printf("f'(x) = %lf \n",fdash_val);
printf("New x: %lf \n\n\n",x_new);
//2getch();
if(f_val == 0.0)
{
root = x_new;
break;
}
if(abs(diff)<2*MAX_ERROR)
{
root = (x_new + x)/2;
break;
}
x = x_new;
}
printf("\n--> Estimated Root at %lf \n\n",root);
}