-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewton Raphson
47 lines (39 loc) · 842 Bytes
/
Newton Raphson
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
#include <stdio.h>
#include <math.h>
#define EPSILON 0.001
float func(float x)
{
float y = (x * x * x) - (x * x) + 2;
return y;
}
float derivative(float x)
{
float y = (3 * x * x) - (2 * x);
return y;
}
void raphson(float x)
{
int count = 0;
float h = func(x) / derivative(x);
float xprev = 0.0;
while (x - xprev >= EPSILON || x - xprev <= -EPSILON)
{
count++;
printf("Iteration %d\n", count);
h = func(x) / derivative(x);
printf("f(x%d) = x%d - (f(%f)/f'(%f)) = %f\n", count, count - 1, x, x, func(x - h));
xprev = x;
x = x - h;
}
printf("Root is %f", x);
}
int main()
{
float a, b, x;
printf("f(x) = x^3 - x^2 + 2\n");
printf("Input initial guess: ");
scanf("%f", &x);
printf("%f\n", x);
raphson(x);
return 0;
}