-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecant.cpp
More file actions
95 lines (92 loc) · 2.16 KB
/
Secant.cpp
File metadata and controls
95 lines (92 loc) · 2.16 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
#include<iostream>
#include<cmath>//Secant Method
using namespace std;
struct function
{
double fun1, fun2;
};
double calculate_fx(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--;
}
}
function intervalChecker(double a, double b, int* coeff_arr, int deg)
{
function f;
double fun1, fun2;
do {
a = a + 0.1;
b = b - 0.1;
fun1 = calculate_fx(coeff_arr, a, deg);
fun2 = calculate_fx(coeff_arr, b, deg);
} while (fun1 < 0 && fun2 < 0 || fun1>0 && fun2>0);
f.fun1 = a;
f.fun2 = b;
return f;
}
int main()
{
int* coeff_arr;
int deg;
double a, b, fun1, fun2,iter;
double x, temp1, temp2;
function f;
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 = calculate_fx(coeff_arr, a, deg);
fun2 = calculate_fx(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);
f = intervalChecker(a, b, coeff_arr, deg);//picking x0 and x1
for (int i = 0; i < 15; i++)
{
temp1 = calculate_fx(coeff_arr, f.fun2, deg);
temp2 = calculate_fx(coeff_arr, f.fun1, deg);
x = (f.fun1 * temp1) - (f.fun2 * temp2);
temp1 = temp1 - temp2;
x = x / temp1;
cout << "x[" << i+2 << "]=" << x << endl;
f.fun1 = f.fun2;
f.fun2 = x;
}
}