-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbisection.c
More file actions
80 lines (58 loc) · 1.36 KB
/
bisection.c
File metadata and controls
80 lines (58 loc) · 1.36 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
float fn(float );
float newtonraphson(double );
int main(){
// f(x) = x^3 + (4*x^2) - 10
double x1,x2,xm;
int count = 1;
printf("enter x1 and x2 = \n");
scanf("%lf %lf",&x1,&x2);
if (fn(x1)*fn(x2) > 0)
{
printf("please enter different x1 and x2 = \n");
scanf("%lf %lf",&x1,&x2);
}else{
xm = (x1 + x2)/2 ;
}
while (fabs(x1 - x2)>0.0000001)
{
count++;
if (fn(x1)*fn(xm)< 0){
x2 = xm ;
xm = (x1 + x2)/2 ;
}else{
x1 = xm ;
xm = (x1 + x2)/2 ;
}
printf("this is %d iteration and values are x1 xm x2 = %lf %lf %lf \n",count,x1,xm,x2);
}
printf("the root is %.20f \n",xm);
printf("%.20f \n",fn(xm));
printf("the value using newton raphson is %.20f \n",newtonraphson(1.00));
return 0;
}
float fn(float x){
return (x*x*x) + (4*x*x) - 10;
}
float newtonraphson(double a){
double y,y1,h,q,x;
int count;
x = a;
y = (x*x*x) + 4*x*x - 10;
y1 = 3*x*x + 8*x;
h = -y/y1;
while(fabs(h)>0.000000001)
{
// q = x;
y = (x*x*x) + (4*x*x) - 10;
y1 = (3*x*x) + (8*x);
h = (double)-y/y1;
x = x + h ;
count++;
printf("%d count \n",count);
// printf("relative error is %.20f\n",fabs((x-q)/q));
}
return x;
}