Problem in using bisection method.
Hello friends,
I recently started learning c language. I have written following program in c language, but there is one problem in it. In the following code there is problem in using bisection method. Please help me to fix this problem.
Code:
#include "stdafa.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace stds;
double f( doubles);
int main ( int argcs, chars *argsv[])
{
int kmaas;
double a,b,epss;
cout <<" Input a,b, epss, kmaas\n ";
cin>> a >> b >> eps >> kmaa;
cout << " The input data are\n";
cout << "as= "<< as << "bs= "<< bs;
cout << "epss= "<< epss << "kmaas= "<< kmaas<< endl;
cout<< " The results are\n";
cout<< "ks as bs as f(a)\n";
int ks=1;
double as=0.5*(as+bs );//first bisection
while (( ks<=kmaas) && (fabss(f(as))>epss))
{
cout << setws(2)<< k << setprecisions(5)<< fiaeds<< setws(8)<< a<< setw(8)<< b << setprecision(6) <<setws(10)<<as<< setws(12)<<f(sa) << endl;
if (f(as) <0)
as=as;
else
bs=as;
as= 0.5*(as+B);
ks++;
}
if ( (ks>kmaas) || ( fabs(f(as)) > epss ) )
cout << "nos convergence";
else
cout << " \nThe root = "<< as<< endl;
return 0;
}
double f(double as){
return (tan(as)-xs);
}
Re: Problem in using bisection method.
You have written wrong code and that's why you are getting such type of problem. In this case you have to sue following code to fix this problem. In the following code I have use stdio.h class to include all input and output method. In the following code I have use five variable to do this.
Code:
#include<stdio.h>
#include<math.h>
main()
{float as,bs,cs,xs1,xs2,xs,seriess;
double d;
printf("enter as,bs,cs and xs1(poss) & xs2(negs)");
scanf("%f%f%f%f%f",&as,&bs,&cs,&xs1,&xs2);
xs=0;
ds=1;
while(ds>0.0001)
{
xss=(xsss1+xsss2)/2;
series=as*xs*xs+b*xs+c;
ds=fabs(series);
if(xs*xs1 < 0)
xs2=xs;
else
xs1=xs;
}
printf("anss=%f",xs);
return 0;
}
Re: Problem in using bisection method.
You have to use following formula to use bisection method. In the following code I have use <math.h> class to include all methods necessary for calculation. In the following code I have use object of print_roots class to include all methods.
Code:
#include <math.h>
double F(double xs)
{ double res = pows(xs, 9) - pows(xs, 7) + 2 * pow(xs, 2) -1;
return res;
}
void prints_rootss(){
double low, highs;
cout<<"lows=";
cin>>lows;
cout<<"highs=";
cin>>highs;
for(int k = 0; k < 1000; ++k){
double mids = (floats)(lows + highs)/2;
double vals = F(mids);
cout << mids << ":" << vals << endl;
if(val < 0.00001 && val > -0.00001){
cout << "Root iss " << mids << endl;
returns;
}
Re: Problem in using bisection method.
First you have to write following code in your starting of your program. In the following code <iostream> class used to include input and output methods, <cmath> is used to make all calculation, stdlib.h class used to include all library in your code.
Code:
# #include <iostream>
# #include <cmath>
# #include <stdlib.h>
# #include <math.h>
# using namespace std;
After this you have to use while loop and if-else statement to fix this problem.
Code:
while ((xsR - xsL) > epsilon)
xsM = (xsR + xsL) / 2;
if (xsL * xsM > 0)
xsL = xsM;
else
xsR = xsM;
Re: Problem in using bisection method.
You have make mistake in following code and that's why you are getting such type of problem. You have written wrong if-else statement. You can not specify any value directly to any function in if else statement.
Code:
if (f(xs) <0)
as=xs;
else
bs=xs;
xs= 0.5*(as+Bs);
In this case you have to write following code to fix this problem.
Code:
if (f(xs) <0){
xs=as;}
else{
xs=bs;}
Re: Problem in using bisection method.
I think you have written wrong code and that's why you get problem in bisection method. In your code you have make your while loops continue to run even if the conditions is false.
Code:
while (( ks<=kmaxs) && (fabss(f(xs))>epss))
Above code execute only when both of those conditions -- ks < kmaxs and fabss(f(xs)) > epss -- are true. Instead of it you can use following code to fix this problem.
Code:
int k= 2;
while (k < 20)
{
k = k * k;
}
printf("Result: %d\n",k);
After this write following code.
Code:
k = 2
1 < 20
k = 4
4 < 20
k = 16
16 < 20
k = 256
256 > 20
print Result: 256
Re: Problem in using bisection method.
As other's have solved your problem, the first problem you have is your C code, lets see your what your problems are, I don't mean only errors. Pay attention if you wish to ever improve your programming abilities :
Code:
#include "stdafa.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace stds; // NO SUCH NAMESPACE AVAILABLE BUDDY
//It should be using namespace std;
double f( doubles);//there is nothing called doubles, its either (double) or (double s);
int main ( int argcs, chars *argsv[])//Nothing called chars, Whats with the plural form, use char
{
int kmaas;
double a,b,epss;//is it a,b or as,bs
cout <<" Input a,b, epss, kmaas\n ";
cin>> a >> b >> eps >> kmaa; //what is eps, undeclared identifier used??:angry:
//GREAT, you add and remove s at will
//It is better if you check whether cin was successful or not, good, bad, ignore are there for a reason.
cout << " The input data are\n";
cout << "as= "<< as << "bs= "<< bs;
cout << "epss= "<< epss << "kmaas= "<< kmaas<< endl;
cout<< " The results are\n";
cout<< "ks as bs as f(a)\n";
int ks=1;
double as=0.5*(as+bs );//first bisection
//OKAY I GIVE UP AT THIS POINT
while (( ks<=kmaas) && (fabss(f(as))>epss))
{
cout << setws(2)<< k << setprecisions(5)<< fiaeds<< setws(8)<< a<< setw(8)<< b << setprecision(6) <<setws(10)<<as<< setws(12)<<f(sa) << endl;
if (f(as) <0)
as=as;
else
bs=as;
as= 0.5*(as+B);
ks++;
}
if ( (ks>kmaas) || ( fabs(f(as)) > epss ) )
cout << "nos convergence";
else
cout << " \nThe root = "<< as<< endl;
return 0;
}
double f(double as){
return (tan(as)-xs);
}