-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton_raphson_root.c
58 lines (57 loc) · 1.37 KB
/
newton_raphson_root.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>
#define EPSILON 0.001
#define delta 0.0000001
double *allocate(int n)
{
double *p = (double*)malloc(n*sizeof(double));int i=0;
for(i=0; i<n; i++)
p[i] = 0;
return p;
}
double function(int n, double x, double *c, double *e)
{
int i; double r=0.0;
for(i=0; i<n; i++)
r = r + c[i]*pow(x,e[i]);
return r;
}
double derivative(int n, double x0, double *c, double *e)
{
double y1, y2, x1, x2;
x1 = x0 + delta;
x2 = x0 - delta;
y1 = function(n, x1, c, e);
y2 = function(n, x2, c, e);
return (double)(y2 - y1)/(x2 - x1);
}
double newtonRaphson(int n, double x, double *c, double *e)
{
double h = function(n, x, c, e) / derivative(n, x, c, e);
printf("Newton-Raphson...\n");
while (1)
{
h = function(n, x, c ,e)/derivative(n, x, c, e);
printf("... %lf - (%lf)\n", x, h);
x = x - h;
if(fabs(h) < EPSILON) break;
}
return x;
}
int main()
{
int n;
printf("Enter in format\nFirst line >> No of terms\nFollowing n lines >> The coeff. and expon. of each term\nNext line >> Two value range\n");
scanf("%d", &n);
double *c = allocate(n), *e = allocate(n);
int i = 0;
for(i=0; i<n; i++)
scanf("%lf %lf", &c[i], &e[i]);
double x0, x1;
scanf("%lf %lf", &x0, &x1);
double m = (x0 + x1)/2.0;
printf("Assumed root at %lf\n", m);
printf("Root at %lf", newtonRaphson(n, m, c, e));
free(c); free(e);
return 0;
}