-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFALSI.cpp
154 lines (135 loc) · 3.51 KB
/
FALSI.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
*
* @author : Pawan Pal
* @Date : 28 Aug 2015
*
*/
#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>
using namespace std;
#define epsilon 9.16978e-005
long iteration = 0;
// Last two iterations.
double pn = 0, pn1 = 0, pn2 = 0, error = 0;
/**
* Calculates the polynomial value
*
* @param poly Vector of coefficients
* @param x Value to calculate, independent variable
* @param degree Order of equation
* @param exp Expression
* @return exp Polynomial Value, dependent variable
*/
double func(vector<double> poly, double x,int degree,double exp)
{
for(int i=0; i<degree; i++)
{
exp += ((poly.at(i))*(pow(x,(degree-i-1))));
}
return exp;
}
/**
* To calculate the value of x intercept
*
* @param a starting interval
* @param b ending interval
* @param va function value at a
* @param vb function value at b
* @return intercept value
*/
double xintercept(double a, double b, double va, double vb)
{return ((a*vb - b*va)/ (vb-va));}
/**
* To calculate error
*/
void calculate_error()
{
// Check if
if (++iteration> 3)
{
error = (pn - pn1) / (pn1 - pn2);
error = (abs(error)/abs(error-1))*abs(pn-pn1);
}
}
/**
* Function to calculates roots using falsi method
*
* @param poly Vector of coefficients
* @param a Starting of interval
* @param b ending of interval
* @param degree order of equation
* @param exp expression
*/
double falsi(vector<double> poly, double a, double b,int degree, double exp)
{
calculate_error();
double root;
int start=0;
pn2 = pn1;
//cout<<pn2<<endl;
pn1 = pn;
//cout<<pn1<<endl;
double intercept = xintercept( a,b,func(poly, a, degree, exp),func(poly, b, degree, exp));
pn=intercept;
cout<<std::setprecision(10)<<"Iteration number : "<<iteration<<", Root : "<<intercept<<", Error : "<<error<<"\t a : "<<a<<", b : "<<b<<endl;
// Checking the stoping condition
if(abs(func(poly, intercept, degree, exp)) < epsilon)
{
cout<<std::setprecision(10)<<"Iteration number : "<<iteration<<", Root : "<<intercept<<", Error : "<<error<<"\t a : "<<a<<", b : "<<b<<endl;
//cout<<"The root is : "<<intercept<<" in "<<iteration<<"iterations"<<"The error is : "<<error<<endl;
return root;
}
else //shrinking the interval
{
if(func(poly, intercept, degree, exp) * func(poly, a, degree, exp) < 0)
{
b= intercept;
falsi(poly, a, b,degree,exp);
}
else
{
a=intercept;
falsi(poly, a, b,degree,exp);
}
}
}
/**
* Main function
*/
int main()
{
char y;
do
{
int degree;
double exp=0.0,x,n,a,b,temp;
vector<double> poly;
cout<<"Enter the degree of polynomial\n";
cin>>degree;
degree++;
cout<<"Enter coefficient\n";
while(poly.size()<degree) // input the coefficients
{
cin>>n;
poly.push_back(n);
}
for(int i=0; i<degree;i++) // display the equation
{
if(i!= (degree-1))
cout<<poly.at(i)<<"x^"<<(degree-i-1)<<" + ";
else
cout<<poly.at(i)<<endl;
}
cout<<"Enter intervals\n";
cin>>a>>b;
cout<<"Value at a is : "<<func(poly, a, degree, exp)<<endl;
cout<<"Value at b is : "<<func(poly, b, degree, exp)<<endl;
if(func(poly, a, degree, exp)>0 && func(poly, b, degree, exp)<0) // to make a must be negative and b positive
{a=temp;a=b;b=temp;}
falsi(poly, a,b,degree,exp);
cout<<"Want to solve more equations\n Press 'y' or 'Y' for yes and any other key for no\n";
cin>>y;
}while(y=='y' || y=='Y');
}