You are on page 1of 1

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
/* Newton-Rhapson Method
Adien Akhmad
Oct 3 2013
*/
#include <stdio.h>
#include <math.h>
float fx(float x);
float fxDerivative(float x);
float AbsRelativeErr(float xNew, float xOld);
int main(int argc, char const *argv[])
{
float xGuess = -0.5;
float xNew, absError;
float const errorTolerance = 0.005;
int count = 1;
printf("Newton-Rhapson Method for\n\n\tf(x) = 4x^3 +7x +3\n");
printf("\n%15s\t: %.2f\n","Initial Guess ", xGuess);
printf("%15s\t: %.3f\n","Error Tolerance", errorTolerance);
printf("\n #\t Root Eq\t%c Error\n", char(37));
printf("-------------------------------\n");
do
{
xNew = xGuess - (fx(xGuess)/fxDerivative(xGuess));
absError = AbsRelativeErr(xNew,xGuess);
printf("%2d\t %8.3f\t%6.2f\n", count, xNew, absError );
xGuess = xNew;
count++;
if (count > 50)
{
printf("To prevent errors caused by loop, maximum iteration has been limited to 50.\n");
break;
}
} while(absError > errorTolerance);
return 0;
}
float AbsRelativeErr(float xNew, float xOld)
{
float result;
result = ((xNew-xOld)/xNew) * 100;
/* Error Value must be absolute */
if (result <= -0.00)
{
result *= -1;
}
return result;
}
float fxDerivative(float x)
{
/* derifatif pertama dari 4x^3 + 7x + 3 */
float result = (12*pow(x,2)) + 7;
return result;
}
float fx(float x)
{
/* Fungsi 4x^3 + 7x + 3 */
float result = (4*pow(x,3)) + (7*x) + 3;
return result;
}

You might also like