-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog_root.c
More file actions
135 lines (101 loc) · 3.52 KB
/
log_root.c
File metadata and controls
135 lines (101 loc) · 3.52 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/****************** Usage: For finding the real roots of any given Quadratic equation. ****************************/
/********************* Specification: The program takes guess value as the input and computes the root for the given equation using Chebyshev method ***********************/
/*************** Given Equation: xlog(x) - 1.2 **************/
/*************** Root is: 2.740646 ***************/
/* Chebysav method is like approimating the given Transcedental Equation into a quadratic equation f(x) = 0, f(x) ~ a0 + a1x + a2x^2
Let xk be an approximate root
f'(x) = a1 + a2x
f''(x) = 2a2 by subsituting the value xk in all the equations we get the values of f(xk), f'(xk), f''(xk)
we get,
f(x) ~ fk + (x-xk)f'_k + (x-xk)^2*f''_k/2 ==> 0
x_(k+1) = xk - fk/f'k - (fk^2 f''_k)/2((f'_k)^3)*/
/*************** PROGRAM STARTS HERE ***************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/********* FUNCTION DECLARATION *********/
float function(float val);
float function_d(float val);
float function_d_d(float val);
void check_bound(float a);
void chebyshev(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
if (a == 0)
{
printf("log(0) is undefined so give a value which is other than zero\n");
exit(2);
}
check_bound(a); //Calling Function
printf("By using chebyshev method: \n");
printf("The equation is: \n");
printf("\t");
printf("f(x) = xlog(x) - 1.2\n");
printf("---------------------------\n");
printf(" f(x) f'(x) f''(x) \n");
printf("---------------------------\n");
chebyshev(a); //Calling Function
exit(0);
}
void chebyshev(float a)
{
float fa, fa_d, fa_d_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
fa_d_d = function_d_d(a); //Calling Functions
printf("%f %f %f\n", fa, fa_d, fa_d_d);
root = a - (fa/fa_d) - (fa*fa*fa_d_d)/(2*fa_d*fa_d*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
}
if (isnan(a)) //Comparing the gk and val1
{
printf("The given root is %f after %.1f iterations\n", a, 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 = x * log10f(x) - 1.2; // 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 = 0.43429 * (1 + logf(x)); // f'(x)
return fx_d; //Returning the value of f'(x) at x1
}
float function_d_d(float val)
{
float fx_d_d, x = val; //Declaration of variables in float
fx_d_d = 0.43429 / x; // f''(x)
return fx_d_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 ;
}