-
Notifications
You must be signed in to change notification settings - Fork 0
/
Regula_falsi.c
37 lines (35 loc) · 985 Bytes
/
Regula_falsi.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define f(x) ((x*x*x)-36)
int main(){
float a=0,b=0,error=0,c,cold;
int i=0;
printf("Enter Interval values: ");
scanf("%f %f",&a,&b); //Interval is [a,b]
if((f(a)*f(b))>0){
printf("Invalid Interval");
exit(1);
}
else if(f(a)==0 || f(b)==0){
printf("Root is one of interval bounds. Root is %f\n",f(a)==0?a:b);
exit(0);
}
printf("Ite\ta\t\tb\t\tc\t\tf(c)\t\terror\n");
do{
cold=c;
c=(((a*f(b))-(b*f(a)))/(f(b)-f(a))); // c is the new root candidate
printf("%2d\t%4.6f\t%4.6f\t%4.6f\t%4.6f\t",i++,a,b,c,f(c));
if(f(c)==0){
break;
}else if(f(a)*f(c)<0){
b=c;
}else a=c;
error=fabs(c-cold);
if(i==1){
printf("----\n");
}else printf("%4.6f\n",error);
}while(error>0.00005);
printf(" Root is %4.6f \n",c);
return 0;
}