-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem3_3.c
52 lines (47 loc) · 1021 Bytes
/
problem3_3.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
#include <stdio.h> /*for printf*/
#include <math.h>
double newton(double x)
{
return (x*exp(x));
}
double newtonp(double x)
{
return(exp(x)+x*exp(x));
}
main()
{
double delta, iterations, root, guess, guess2, midpoint;
iterations=delta=root=delta=midpoint=0;
delta=(newton(guess)/newtonp(guess));
guess2=0.5;
guess= -0.5;
if(newton(guess)<1e-2)
{
for(guess=-1,root=guess-delta;fabs(newton(root))>1e-10;guess=root)
{
delta=(newton(guess)/newtonp(guess));
root=guess-delta;
iterations=iterations+1;
printf("iterations= %lf guess= %lf\n",iterations,guess);
}
}
else
{
while(fabs(guess-guess2)>1e-10) {
midpoint=((guess + guess2)/2);
if(newton(guess)*newton(guess2)>0) {
printf("Pick different guess\n");
}
if (newton(midpoint)<0) {
guess=midpoint;
}
else {
guess2=midpoint;
}
iterations=iterations+1;
}
}
printf("root= %lf\n",root);
printf("delta= %lf\n",delta);
printf("iterations= %lf\n",iterations);
}