to write a c program to find the roots of the equation using bisection method
that too using array or pointers
8 22088
1. You posted a similar thread to this, please refrain from doing that.
2. For which equation are we finding roots? What is the bijection method? A bijection is a 1-1 onto function, so "1-1 onto function method" really doesn't help. You should be more specific, then we might be able to help you.
Thanks for the headsup D_C.
Dup has been deleted.
i just wrote a c program to find the roots of equation using bisection method
but there is some error prevails .i think my logic is correct please help out
this my program -
/* to find the roots using bisection method*/
-
# include <stdio.h>
-
# include <conio.h>
-
# include<stdlib.h>
-
# include <math.h>
-
# define EPS .00001
-
double F(double x);
-
double root(double fVal1,double fVal2);
-
-
void main()
-
{
-
-
-
double fVal1,fVal2;
-
clrscr();
-
printf("enter the values ");
-
scanf("%f %f",&fVal1,&fVal2);
-
printf("the roots of the euation is %1.9f\n",root(fVal1,fVal2));
-
getch();
-
}
-
double root(double fVal1,double fVal2)
-
{
-
int nCtr=0;
-
double fTest1,fTest2,fTest3,fVal,fNum;
-
-
fTest1=F(fVal1);
-
fTest2=F(fVal2);
-
if((fTest1*fTest2)>0)
-
{
-
printf("both are of equal sign");
-
exit(0);
-
}
-
else
-
-
{
-
do
-
{
-
nCtr++;
-
fVal1=(fVal1+fVal2)/2;
-
fTest3=F(fVal);
-
if(fTest1*fTest3>0)
-
-
{
-
fTest1=fTest3;
-
fVal1=fVal;
-
}
-
else
-
{
-
fTest2=fTest3;
-
fVal2=fVal;
-
-
}
-
fNum=fabs (fVal2-fVal1);
-
-
-
} while(fNum>EPS);
-
}
-
return fabs(fVal2)>fabs(fVal1)?fVal2:fVal1;
-
-
-
-
-
-
}
-
double F(double x)
-
{
-
return (x*x*x)+(3*x)-5;
-
-
}
-
Banfa 9,065
Expert Mod 8TB
Firstly, as has already been stated, please do not start multiple threads about the same problem, post to your original thread. I have now merged the 2 relevent threads.
As to your code.
strangely I had to change
double fVal1,fVal2;
in main to
float fVal1,fVal2;
in order to get the correct values into the program.
A more serious error is that in
double root(double fVal1,double fVal2)
{
...
}
you declare and use a value fVal but you never initialise it to anything.
i have changed my program still iam not getting it
# include <stdio.h>
# include <conio.h>
# include<stdlib.h>
# include <math.h>
# define EPS .00001
float F(float x);
float root(float fVal1,float fVal2);
void main()
{
float fVal1,fVal2,;
clrscr();
printf("enter the values ");
scanf("%f %f",&fVal1,&fVal2);
printf("the roots of the euation is %1.9f\n",root(fVal1,fVal2));
getch();
}
float root(float fVal1,float fVal2)
{
int nCtr=0;
float fTest1,fTest2,fTest3,fVal,fNum;
fTest1=F(fVal1);
fTest2=F(fVal2);
if((fTest1*fTest2)>0)
{
printf("both are of equal sign");
exit(0);
}
else
{
do
{
nCtr++;
fVal=(fVal1+fVal2)/2;
fTest3=F(fVal);
if(fTest1*fTest3>0)
{
fTest1=fTest3;
fVal1=fVal;
}
else
{
fTest2=fTest3;
fVal2=fVal;
}
fNum=fabs (fVal2-fVal1);
} while(fNum>EPS);
}
return fabs(fVal2)>fabs(fVal1)?fVal2:fVal1;
}
float (float x)
{
return (x*x)-5;
}
i have just wrote a program to find the roots of the equation using bisection method
but there is some mistake iam not getting the output.comment iam getting as abnormal termination.so please help me
here is my program. -
/* to find the roots using bisection method*/
-
# include <stdio.h>
-
# include <conio.h>
-
# include<stdlib.h>
-
# include <math.h>
-
# define EPS .00001
-
float F(float x);
-
float root(float fVal1,float fVal2);
-
-
void main()
-
{
-
-
-
float fVal1,fVal2,;
-
clrscr();
-
printf("enter the values ");
-
scanf("%f %f",&fVal1,&fVal2);
-
printf("the roots of the euation is %1.9f\n",root(fVal1,fVal2));
-
getch();
-
}
-
float root(float fVal1,float fVal2)
-
{
-
int nCtr=0;
-
float fTest1,fTest2,fTest3,fVal,fNum;
-
-
fTest1=F(fVal1);
-
fTest2=F(fVal2);
-
if((fTest1*fTest2)>0)
-
{
-
printf("both are of equal sign");
-
exit(0);
-
}
-
else
-
-
{
-
do
-
{
-
nCtr++;
-
fVal=(fVal1+fVal2)/2;
-
fTest3=F(fVal);
-
if(fTest1*fTest3>0)
-
-
{
-
fTest1=fTest3;
-
fVal1=fVal;
-
}
-
else
-
{
-
fTest2=fTest3;
-
fVal2=fVal;
-
-
}
-
fNum=fabs (fVal2-fVal1);
-
-
-
} while(fNum>EPS);
-
}
-
return fabs(fVal2)>fabs(fVal1)?fVal2:fVal1;
-
-
-
-
-
-
}
-
float (float x)
-
{
-
return (x*x)-5;
-
-
}
-
Banfa 9,065
Expert Mod 8TB Please do NOT create multiple threads about the same problem
Please DO you the [code] and [/code] to delimit you code.
The posted code does not compile, please post compiling code. I would suggest that rather than typeing your code into the message box you copy and paste it from the source file once you have got the code compiling so we are all working from the same base line.
[
/*The Bisection Method to solve x*x*x+3*x-5***********/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x);
main()
{
float a,b,y,r1,r2,r3;
do{
printf("\nenter approximate interval");
scanf("%f%f",&a,&b);
r1=f(a);
r2=f(b);
}while(r1*r2>0);
printf("a\t\tb x=(a+b)/2 f(x)\n\n");
do{
r1=f(a);
r2=f(b);
y= (a+b)/2.0;
r3=f(y);
printf( "%.4f %.4f %.4f %.4f",a,b,y,r3);
if(r1*r3<0)
b=y;
else
a=y;
printf("\n");
}while(fabs(r3)>0.0001);
printf("\n The approx sol is %.4f",y);
getch();
return 0;
}
float f(float x )
{
float f1;
f1=(x*x*x)+(3*x)-5;
return(f1);
}
well i hope it will help you.
its simple but it works!!.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
9 posts
views
Thread by Stud Muffin |
last post: by
|
5 posts
views
Thread by Justin Caldicott |
last post: by
|
1 post
views
Thread by Dumidu |
last post: by
|
18 posts
views
Thread by anand |
last post: by
| |
5 posts
views
Thread by CoreyWhite |
last post: by
| | | | | | | | | | | | | |