468,462 Members | 1,811 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,462 developers. It's quick & easy.

how to write a c program to find the roots of the equation using bisection method

6
to write a c program to find the roots of the equation using bisection method
that too using array or pointers
Aug 29 '06 #1
8 21918
D_C
293 100+
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.
Aug 29 '06 #2
Niheel
2,425 Expert Mod 2GB
Thanks for the headsup D_C.
Dup has been deleted.
Aug 29 '06 #3
aruna
6
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



Expand|Select|Wrap|Line Numbers
  1. /* to find the roots using bisection method*/
  2. # include <stdio.h>
  3. # include <conio.h>
  4. # include<stdlib.h>
  5. # include <math.h>
  6. # define EPS .00001
  7. double F(double x);
  8. double root(double fVal1,double fVal2);
  9.  
  10. void main()
  11. {
  12.  
  13.  
  14.     double fVal1,fVal2;
  15.     clrscr();
  16.     printf("enter the values ");
  17.     scanf("%f %f",&fVal1,&fVal2);
  18.     printf("the roots of the euation is %1.9f\n",root(fVal1,fVal2));
  19.     getch();
  20. }
  21.     double root(double fVal1,double fVal2)
  22.     {
  23.         int nCtr=0;
  24.         double fTest1,fTest2,fTest3,fVal,fNum;
  25.  
  26.         fTest1=F(fVal1);
  27.         fTest2=F(fVal2);
  28.         if((fTest1*fTest2)>0)
  29.         {
  30.             printf("both are of equal sign");
  31.             exit(0);
  32.         }
  33.         else
  34.  
  35.         {
  36.             do
  37.             {
  38.                 nCtr++;
  39.                 fVal1=(fVal1+fVal2)/2;
  40.                 fTest3=F(fVal);
  41.             if(fTest1*fTest3>0)
  42.  
  43.             {
  44.                 fTest1=fTest3;
  45.                 fVal1=fVal;
  46.             }
  47.             else
  48.             {
  49.                  fTest2=fTest3;
  50.                  fVal2=fVal;
  51.  
  52.             }
  53.             fNum=fabs (fVal2-fVal1);
  54.  
  55.  
  56.             } while(fNum>EPS);
  57.         }
  58.             return fabs(fVal2)>fabs(fVal1)?fVal2:fVal1;
  59.  
  60.  
  61.  
  62.  
  63.  
  64.            }
  65.            double F(double x)
  66.            {
  67.             return (x*x*x)+(3*x)-5;
  68.  
  69.            }
  70.  
Sep 3 '06 #4
Banfa
9,051 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.
Sep 3 '06 #5
aruna
6
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;

}
Sep 4 '06 #6
aruna
6
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.


Expand|Select|Wrap|Line Numbers
  1. /* to find the roots using bisection method*/
  2. # include <stdio.h>
  3. # include <conio.h>
  4. # include<stdlib.h>
  5. # include <math.h>
  6. # define EPS .00001
  7. float F(float x);
  8. float root(float fVal1,float fVal2);
  9.  
  10. void main()
  11. {
  12.  
  13.  
  14.     float fVal1,fVal2,;
  15.     clrscr();
  16.     printf("enter the values ");
  17.     scanf("%f %f",&fVal1,&fVal2);
  18.     printf("the roots of the euation is %1.9f\n",root(fVal1,fVal2));
  19.     getch();
  20. }
  21.     float root(float fVal1,float fVal2)
  22.     {
  23.         int nCtr=0;
  24.         float fTest1,fTest2,fTest3,fVal,fNum;
  25.  
  26.         fTest1=F(fVal1);
  27.         fTest2=F(fVal2);
  28.         if((fTest1*fTest2)>0)
  29.         {
  30.             printf("both are of equal sign");
  31.             exit(0);
  32.         }
  33.         else
  34.  
  35.         {
  36.             do
  37.             {
  38.                 nCtr++;
  39.                 fVal=(fVal1+fVal2)/2;
  40.                 fTest3=F(fVal);
  41.                 if(fTest1*fTest3>0)
  42.  
  43.                 {
  44.                     fTest1=fTest3;
  45.                     fVal1=fVal;
  46.                 }
  47.                 else
  48.                 {
  49.                      fTest2=fTest3;
  50.                      fVal2=fVal;
  51.  
  52.                 }
  53.                 fNum=fabs (fVal2-fVal1);
  54.  
  55.  
  56.             } while(fNum>EPS);
  57.         }
  58.         return fabs(fVal2)>fabs(fVal1)?fVal2:fVal1;
  59.  
  60.  
  61.  
  62.  
  63.  
  64.            }
  65.            float (float x)
  66.            {
  67.             return (x*x)-5;
  68.  
  69.            }
  70.  
Sep 7 '06 #7
Banfa
9,051 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.
Sep 7 '06 #8
[


/*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!!.
Oct 5 '06 #9

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
1 post views Thread by salman patni | last post: by
5 posts views Thread by CoreyWhite | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.