can you tell me what mistake i made?

Status
Not open for further replies.

geekgod

Journeyman
hi, i was writing a c program for my class assignment, and i got stuck. i paste it below.
/*This program calculates the roots of the equation x^3-3x^2+5x-10=0
by two separate methods, Bisection method and Newton-Rhapson method*/
#include <stdio.h>
#include <math.h>
void bisect(void);
void newton(void);
main()
{
int choice;
printf("Choose the method you want to use\n");
printf("1:Bisection method\n");
printf("2:Newton-Rhapson\n");
scanf("%d", &choice);
/*switch(choice) {
case 1:*/
bisect();
/*case 2:
newton();*/
}

void bisect()
{
float x1,x2,x0,f1,f2,f0;
int count;
printf("Enter the range within which the calculation is to be carried out : ");
scanf("%f %f", &x1, &x2);
for(count=1;count<=50;count++)
{
f1=x1*x1-4*x1-10;
f2=x2*x2-4*x2-10;
if(f1*f2>0)
{
x0=(x1+x2)/2.0;
f0=x0*x0-4*x0-10;
printf("Iteration %d : x = %f \n", count, x0);
if(f1*f0<0)
{
x2=x0;
}
else
{
x1=x0;
f1=f0;
}
if(fabs((x2-x1)/x2)<.0005)
{
printf("\nThe required root of the equation is %f", x1);
break;
}
}
}

}
it appears to me that the program structure is ok, but when i run it, it does'nt give any output. i have also tried by taking the last 'printf' statement out of the for loop, but then the out put is not what i expected. it prints the value of x as -10 continously 50 times. :( :( :(.
help plz!! :oops: :oops:
 

vswizard

Broken In
This is wat i used

// Prog for Bisection method

#include<math.h>
void main()
{
float fun(float x);
void bis(float *x,float a , float b, int *itr);
int itr=0,maxit;
float x,a,b,aerr,x1;

clrscr();
printf("\n\n\nEnter Value of a ");
scanf("%f",&a);
printf("\nEnter Value of b ");
scanf("%f",&b);
printf("\nEnter Value of allowed error ");
scanf("%f",&aerr);
printf("\nEnter Maximum Iteration allowed ");
scanf("%d",&maxit);

bis(&x,a,b,&itr);

do
{

if(fun(a)*fun(x)<0)
{b=x;}
else
{a=x;}

bis(&x1,a,b,&itr);

if(fabs(x1-x)<aerr)
{
printf("\n After %d Iterations ,Root is %.4f ",itr,x1);
getch();
exit(0);
}

x=x1;
}

while(itr<maxit);
{
printf("\n Solution Does not converge after %d iteration",maxit);
}
getch();
exit(1);

}

float fun(float x)
{
return ((x*x*x)-(x)-11);
}

void bis(float *x,float a,float b,int *itr)
{
*x=(a+b)/2;
++(*itr);
printf("\n Iteration number %3d x = %.4f ",*itr,*x);
}
 

technomodel

Journeyman
i think ur program is okay. just change x1 to x0 in the final output statement, coz what u need is the value of x0, and x1 will takethat value only if a particular condition is satisfied (f1*f0>0). also the equation you chose appears to have distributed roots, so you will have to try a number of ranges before u get a proper answer.
 

aadipa

Padawan
here r my code

1 Bisection Method
Code:
/*
 * Find root of equation xýSin(x)-xCos(x)+5 = 0 using bisection method
 * upto 6 decimal places
 *
 * GUESS 1 & 2
 */

#include "math.h"

double function1(double);

void main() {
  int i;
  float a=0,b=0,c,fa,fb,fc;
  long n,nc=0;

  start:

  clrscr();
  printf("\n   -----------------------------------------------------------");
  printf("\n   -------------------- BISECTION METHOD  --------------------");
  printf("\n   -------------- y = f(x) = xýSin(x)-xCos(x)+5 --------------");
  printf("\n   -----------------------------------------------------------");

  printf("\nEnter your guesses\
  \n\nFirst guess\t: ");
  scanf("%f",&a);
  printf("\nSecond guess\t: ");
  scanf("%f",&b);
  fa=function1(a);
  fb=function1(b);
  if(fa*fb > 0) {
    printf("\n\nWrong Guesses");
    printf("\na=%f, f(a)=%f",a,fa);
    printf("\nb=%f, f(b)=%f",b,fb);
    getch();
    goto start;
    }

  printf("\n\nNo. of iterations : ");
  scanf("%ld",&n);

  while(1) {
    c=(a+b)/2;
    nc++;
    if(nc > n) break;
    fc=function1(c);
    fa=function1(a);
    fb=function1(b);

    printf("\nn=%02ld a=%+f b=%+f c=%+f fa=%+f fb=%+f fc=%+f",nc,a,b,c,fa,fb,fc);

    if(fa*fc <= 0)
     b=c;
    else
     if(fb*fc <= 0)
      a=c;

    if(fabsl(a-b) < 1e-6) break;
    }
  printf("\n\nFinal Value of root is \t  c =%+.10f,\n\t\t\tf(c)=%+.10f",c,fc);
  printf("\nSolution converges to root after %ld iterations",nc);
  }


double function1(double x) {
  return(x*x*sin(x)-x*cos(x)+5);
  }

2. NR Method
Code:
/*
 * Find root of equation x^3-2x+5 = 0 by Newton Raphson method upto
 * five decimal places.
 *
 * GUESS = -2.1
 */

#include "math.h"

float f(float);
float fd(float);
void main(void);

float f(float a) {
 return(a*a*a-2*a+5);
 }

float fd(float a) {
 return(3*a*a-2);
 }

void main(void) {
 float a,nr,dr,p;
 float const error=1e-5;
 int n,s;

 start:
 clrscr();

  printf("\n   -----------------------------------------------------------");
  printf("\n   ------------------ NEWTON RAPHSON METHOD ------------------");
  printf("\n   ------------------- y = f(x) = x^3-2x+5 -------------------");
  printf("\n   -----------------------------------------------------------");

 printf("\n Enter your guess :: ");
 scanf("%f",&a);
 nr=f(a);
 dr=fd(a);
 n=1;
 p=a;
 printf("\n Enter max iterations :: ");
 scanf("%d",&s);

 while(n<=s) {
  if(dr==0) {
   printf("\nERROR! Division by zero.");
   getch();
   goto start;
   }
  printf("\n n=%2d a=%+f fa=%+f f'a=%f",n,a,nr,dr);
  a-=(nr/dr);
  nr=f(a);dr=fd(a);n++;
  if( fabs(a-p) < error ) break;
  p=a;
  printf(" a=>>%+f",a);
  }

 printf("\n Root = %f\n Iterations = %d",a,n-1);
 getch();
 }
 

aadipa

Padawan
i even wrote a code to show how the solution converges to root graphically using Turbo C. I don't have that code, i was in my college and i am sure that college hdd must had be formatted atleast once in last 4 years otherwise i would have given that code too.
 

Satan_Rulez

Broken In
Guys i want to program a small application.
Like i want to write a letter as an exe file.
When downloaded n opened it should popup a dos window n the
text should appear one character at a time with a very short delay.
Can anyone tell me how to in Basic cos currently i dont have any c compiler.
 
Status
Not open for further replies.
Top Bottom