Created
January 9, 2019 19:07
-
-
Save asd227695/176a5e3ef3e64d5909744cf4d1ee1863 to your computer and use it in GitHub Desktop.
Bisection Method of Numerical Engineering using 3 stopping criterion. This program is for the equation (x-2)^2-log(x)=0. You can change it by changing the return statement in the function name "func".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<math.h> | |
#define e 2.718281828 | |
#define pie 3.141592654 | |
using namespace std; | |
double r; | |
int m; | |
float num; | |
double ea; | |
int flag; | |
char choice; | |
double func(double x) | |
{ | |
// return (pow(e,-x)*(3.2*(sin(x))-0.5*(cos(x)))); | |
//return(5*x*x*x-5*x*x+6*x-2); | |
return(pow((x-2),2)-log(x)); | |
} | |
void send(double c) | |
{ | |
r=c; | |
} | |
void mid(double a,double b,int m) | |
{ | |
long double c; | |
if((abs(b-a))>=pow(10,-(m+1))) | |
{ | |
if(func(a)*func(b)>=0) | |
{ | |
cout<<"You have assumed the wrong initial values\n"; | |
} | |
else | |
{ | |
c=(a+b)/2; | |
send(c); | |
if(func(c)==0.0) | |
{ | |
cout<<"The root is :"<<c<<endl; | |
} | |
else if(func(c)*func(a)<0) | |
{ | |
mid(a,c,m); | |
} | |
else if(func(c)*func(b)<0) | |
{ | |
mid(c,b,m); | |
} | |
} | |
} | |
} | |
void mid(double a,double b,float num) | |
{ | |
long double c; | |
float temp=0; | |
while(temp!=num) | |
{ | |
if(func(a)*func(b)>=0) | |
{ | |
flag=0; | |
} | |
else | |
{ | |
flag=1; | |
c=(a+b)/2; | |
send(c); | |
if(func(c)==0.0) | |
{ | |
cout<<"The root is :"<<c<<endl; | |
} | |
else if(func(c)*func(a)<0) | |
{ | |
b=c; | |
} | |
else if(func(c)*func(b)<0) | |
{ | |
a=c; | |
} | |
} | |
temp++; | |
} | |
if(flag==0){cout<<"You have assumed wrong initial values"<<endl;} | |
} | |
void mid(double a,double b,double ea) | |
{ | |
long double c; | |
long double temp=ea; | |
long double pre=0; | |
do | |
{ | |
temp=abs((((a+b)/2)-pre)/((a+b)/2)); | |
if(func(a)*func(b)>=0) | |
{ | |
flag=0;break; | |
} | |
else | |
{ | |
flag=1; | |
pre=(a+b)/2; | |
c=(a+b)/2; | |
send(c); | |
if(func(c)==0.0) | |
{ | |
cout<<"The root is :"<<c<<endl; | |
} | |
else if(func(c)*func(a)<0) | |
{ | |
b=c; | |
} | |
else if(func(c)*func(b)<0) | |
{ | |
a=c; | |
} | |
} | |
}while(temp>=ea); | |
if(flag==0){cout<<"You have assumed wrong initial values"<<endl;} | |
} | |
int main() | |
{ | |
do | |
{ | |
double w,y; | |
cout<<"Enter the initial values of the function"<<endl; | |
cin>>w; | |
cin>>y; | |
int ch; | |
cout<<"Enter through which stopping criteria you wanna proceed.\n1-By Significant Digit\n2-By number of iteration\n3-By relative error\n"; | |
cin>>ch; | |
switch(ch) | |
{ | |
case 1: | |
{ | |
cout<<"Enter to how many decimal digits you want answer :"; | |
cin>>m; | |
mid(w,y,m); | |
r = ((double)((int)(r *pow(10,m))))/pow(10,m); | |
cout<<"The root is: "<<r<<endl; | |
break; | |
} | |
case 2: | |
{ | |
cout<<"Enter number iteration you wanna go :"; | |
cin>>num; | |
mid(w,y,num); | |
if(flag==0){} | |
else{cout<<"The root is: "<<r<<endl;} | |
break; | |
} | |
case 3: | |
{ | |
cout<<"Enter the value of relative error(Without Percentage) :"; | |
cin>>ea; | |
mid(w,y,ea); | |
if(flag==0){} | |
else{cout<<"The root is: "<<r<<endl;} | |
} | |
} | |
cout<<"Press Y to do the process again"<<endl; | |
cin>>choice; | |
}while(choice=='y'||choice=='Y'); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment