-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrans2_root.c
More file actions
115 lines (88 loc) · 3.18 KB
/
trans2_root.c
File metadata and controls
115 lines (88 loc) · 3.18 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/****************** Usage: For finding the real roots of any given Transcedental equation. ****************************/
/********************* Specification: The program takes guess value as the input and computes the root for the given equation using Newton-Raphson method ***********************/
/*************** Given Equation: 2x - log(x) - 7 = 0**************/
/*************** Root is: 3.789278 ***************/
/* This method is generally used to improve the result obtained by one of the
previous methods. Let x0 be an approximate root of f(x) = 0 and let x1 = x0 + h be
the correct root so that f(x1) = 0.
Expanding f(x0 + h) by Taylor’s series, we get
f(x0) + hf′(x0) + h2/2! f′′(x0) + ...... = 0
Since h is small, neglecting h2 and higher powers of h, we get
f(x0) + hf′(x0) = 0 or h = – f(x0)/f'(x0)
A better approximation than x0 is therefore given by x1, where
x1 = x0 - f(x0)/f'(x0)
Successive approximations are given by x2, x3, ....... , xn + 1, where
x(n+1) = xn - f(xn)/f'(xn) (n = 0, 1, 2, 3, .......)
Which is Newton-Raphson formula. */
/*************** PROGRAM STARTS HERE ***************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/********* FUNCTION DECLARATION *********/
float function(float val);
void check_bound(float a);
float function_d(float val);
void newton(float a);
/********** MAIN STARTS HERE *********/
int main(int argc, char **argv)
{
float a; //Declaration of variables in float
if (argc != 2) //Verification of arguments
{
fprintf(stderr, "Usage: %s <guess>\n", argv[0]);
exit(1);
}
a = atof(argv[1]); //Converting arguments
check_bound(a); //Calling Function
printf("By using Newton-Raphson method: \n");
printf("The equation is: \n");
printf("\t");
printf("f(x) = 2x - log(x) - 7\n");
printf("------------------------\n");
printf(" f(x) f'(x) \n");
printf("------------------------\n");
newton(a); //Calling Function
exit(0);
}
void newton(float a)
{
float fa, fa_d, root, i = 0; //Declaration of variables in float
while (1) //Infinte Loop
{
fa = function(a); //Calling Functions
fa_d = function_d(a); //Calling Functions
printf("%f %f\n", fa, fa_d);
root = a - (fa/fa_d);
if (floor(a*10000) == floor(root*10000)) //Comparing the roots
{
printf("The given root is %f after %.1f iterations\n", root, i);
break; //Getting out of the loop
}
a = root;
i++; //Incrementing i
}
return ;
}
float function(float val)
{
float fx, x = val; //Declaration of variables in float
fx = 2 * x - log10f(x) - 7; // f(x)
return fx; //Returning the value of f(x) at x1
}
float function_d(float val)
{
float fx_d, x = val; //Declaration of variables in float
fx_d = 2 - 0.43429/x; // f'(x)
return fx_d; //Returning the value of f'(x) at x1
}
void check_bound(float a)
{
float fa; //Declaration of variables in float
fa = function(a); //Calling Functions
if (fa == 0) //Check condition
{
printf("The root is one of the boundaries.\n");
exit(0);
}
return ;
}