-
Notifications
You must be signed in to change notification settings - Fork 0
/
Newton Raphson.cpp
99 lines (95 loc) · 2.3 KB
/
Newton Raphson.cpp
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
#include<iostream>
#include<cmath>//Newton_Raphson
using namespace std;
double intervalChecker_der(int* coeff, double a, int deg)//to find value in derivative
{
double po, temp = deg, final = 0;
for (int i = 0; i < deg; i++)
{
po = pow(a, temp);
po = coeff[i] * po;
final += po;
temp--;
}
return final;
}
double intervalChecker(int* coeff, double a, int deg)//to find value in actual function
{
double po, temp = deg, final = 0;
for (int i = 0; i < deg + 1; i++)
{
po = pow(a, temp);
po = coeff[i] * po;
final += po;
temp--;
}
return final;
}
void display(int* coeff_arr, int deg)//displaying equation
{
cout << endl << " <--------------------------------------Equation--------------------------------->" << endl << endl;
cout << " ";
int temp = deg;
for (int i = 0; i < deg + 1; i++)
{
cout << "(" << coeff_arr[i] << ")" << "x^" << temp;
if (i < deg)
{
cout << "+";
}
temp--;
}
}
int main()
{
int* coeff_arr, accu;
int deg;
double a, b, fun1, fun2, avg, temp1, deri, iter;
cout << "Enter Highest Degree of x:";
cin >> deg;
int temp = deg;
coeff_arr = new int[deg + 1];
for (int i = 0; i < deg + 1; i++)
{
cout << "Enter Coefficient of x^" << temp << ":";
cin >> coeff_arr[i];
temp--;
}
system("cls");
display(coeff_arr, deg);
cout << endl;
do {
cout << "Enter Interval value a:";
cin >> a;
cout << "Enter Interval value b:";
cin >> b;
fun1 = intervalChecker(coeff_arr, a, deg);
fun2 = intervalChecker(coeff_arr, b, deg);
cout << "Interval value: [" << a << "," << b << "]" << endl;
cout << "f(a):" << fun1 << endl;
cout << "f(b):" << fun2 << endl;
} while (fun1 < 0 && fun2 < 0 || fun1>0 && fun2>0);
double x_not = (a + b) / 2;
temp = deg;
int* temp_arr = new int[deg + 1];//calculating derivative
for (int i = 0; i < deg + 1; i++)
{
temp_arr[i] = coeff_arr[i];
}
for (int i = 0; i < deg + 1; i++)
{
temp_arr[i] = temp_arr[i] * temp;//derivative stored in temp_arr
temp--;
}
cout << "[x0]:" << x_not << endl;//value of x_not
for (int i = 0; i < 15; i++)
{
deri = intervalChecker_der(temp_arr, x_not, deg - 1);
temp1 = intervalChecker(coeff_arr, x_not, deg);
temp1 = temp1 / deri;
temp1 = x_not - temp1;
x_not = temp1;
cout << "[x" << i + 1 << "]:" << temp1 << endl;
}
return 0;
}