-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecantCodeHorners.cpp
66 lines (56 loc) · 1.04 KB
/
SecantCodeHorners.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
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<stdlib.h>
using namespace std;
static int coeff[100];
static double f(int degree,double x)
{ double result=coeff[0];
for(int i=1;i<=degree;i++)
{
result=result*x+coeff[i];
}
//cout<<result<<endl;
return result;
}
double error(double x1,double x2)
{
return abs((x1-x2)/x1);
}
int main()
{ double x1,x2,x3,fx1,fx2,fx3;
double E=0.001;
int degree;
//static coeff[degree+1];
printf("Enter the degree:");
scanf("%d",°ree);
printf("Enter coeff:");
for(int i=0;i<=degree;i++)
{
scanf("%d",&coeff[i]);
}
float root;
printf("Enter x1,x2: ");
cin>>x1;
cin>>x2;
fx1=f(degree,x1);
fx2=f(degree,x2);
cout<<fx2<<endl;
x3=(fx2*x1-fx1*x2)/(fx2-fx1);
fx3=f(degree,x3);
//double e=error(x3,x2);
double err=abs((x3-x2)/x3);
while(E<err)
{
cout<<x3<<endl;
x1=x2;
x2=x3;
fx1=f(degree,x1);
fx2=f(degree,x2);
x3=(fx2*x1-fx1*x2)/(fx2-fx1);
fx3=f(degree,x3);
//e=error(x3,x2);
err=abs((x3-x2)/x3);
}
cout<<x3<<endl;
}