-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecant method.cpp
More file actions
45 lines (34 loc) · 843 Bytes
/
Copy pathSecant method.cpp
File metadata and controls
45 lines (34 loc) · 843 Bytes
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
//program to demonstrate the secant method considering the possibility of f(a)=f(b)
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
inline float f(float x)
{
return x*x*x-4*x-9;
}
int main()
{
float a,b,c, dum1, dum2;
int counter=0;
do
{
cout<<"enter initial guesses a and b:";
cin>>a>>b;
}
while(f(a)==f(b));
cout<<"S.N"<<setw(17)<<"a"<<setw(24)<<"b"<<setw(33)<<"c=(a*f(b)-b*f(a))/(f(b)-f(a))"<<setw(40)<<"f(c)"<<endl;
while(1)
{
dum1=a;
dum2=b;
counter++;
c=(a*f(b)-b*f(a))/(f(b)-f(a));
a=b;
b=c;
cout<<counter<<setw(17)<<dum1<<setw(24)<<dum2<<setw(33)<<c<<setw(40)<<f(c)<<endl;
if(fabs(f(c))<0.0001)
break;
}
cout<<"soln and no of iteration="<<c<<" "<<counter;
}