-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed_point.c
91 lines (77 loc) · 1.63 KB
/
fixed_point.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define MAX_COUNT 30
/* solve f(x) = x^3 + 4x^2 - 10 */
float g1(float x)
{
return x - powf(x, 3) - 4*powf(x, 2) + 10;
}
float g2(float x)
{
return sqrtf(10/x - 4*x);
}
float g3(float x)
{
return sqrtf(10 - powf(x, 3))/2;
}
float g4(float x)
{
return sqrtf(10 / (4 + x));
}
float g5(float x)
{
return x - (powf(x, 3) + 4*powf(x, 2) - 10)/(3*powf(x, 2) + 8*x);
}
void print_errors(float *e, size_t num)
{
printf("relative errors:\n\n");
for(int i=0; i < num; i++)
{
printf("%2d-iter: %e\n",i+1 , e[i]);
}
}
float compute_order(float *e, size_t num)
{
return logf(fabsf(e[num-1]/e[num-2])) / logf(fabsf(e[num-2]/e[num-3]));
}
/**
* print_aec - print the asymptotic error constant
* @e: 1D array, stored errors
* @num: size of e
*
* This method is for linearly convergence e.g.
* fixed-point method.
*/
void print_aec(float *e, size_t num)
{
for(int j=0; j < num-1; j++)
{
printf("aec: %f\n", e[j+1]/e[j]);
}
}
int main()
{
float tol = 1e-4;
float p_pre = 1.5;
float p_cur = 1e5;
float exact = 1.365230013;
float diff = fabsf(p_pre - p_cur);
/* relative errors */
float e[MAX_COUNT];
int i = 0;
printf("p0: %f\n", p_pre);
while(i < MAX_COUNT && diff > tol && !isnan(p_cur))
{
p_cur = g5(p_pre);
diff = fabsf(p_pre - p_cur);
printf("%d iteration:\n", i+1);
printf(" p_cur: %f\n\n", p_cur);
p_pre = p_cur;
e[i] = fabsf(p_cur - exact) / exact;
i++;
}
print_errors(e, i);
print_aec(e, i);
return 0;
}