-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewton_raphson.c
More file actions
42 lines (41 loc) · 1.11 KB
/
newton_raphson.c
File metadata and controls
42 lines (41 loc) · 1.11 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
float function(float);
float function_derivative(float);
void newton_raphson_method(float,float);
void main()
{
float allowed_error=0.0,seed_value;
printf("Enter the seed value\n");
scanf("%f",&seed_value);
printf("Enter the allowed error\n");
scanf("%f",&allowed_error);
newton_raphson_method(seed_value,allowed_error);
}
void newton_raphson_method(float seed_value,float allowed_error)
{
float value=seed_value;
float h=-function(value)/function_derivative(value);
for(;fabs(function(value))>=allowed_error;)
{
if(fabs(function_derivative(value))>=0.00000001)
h=-function(value)/function_derivative(value);
else
{
printf("Newton_raphson method fails\n");
exit(0);
}
value=value+h;
}
printf("Root value is %f\n",value);
printf("functional value: f(%f)=%f",value,function(value));
}
float function(float value)
{
return (value*value*value-2);
}
float function_derivative(float value)
{
return (3*value*value);
}