20_10

Write a C program to find a root of the equation a*x*x + b*x +c using Bi Section method.

 Source code:

/*Numerical Method: Bi Section method to find a root of the equation a*x*x + b*x +c.  The program read should the values of a,b and c and values for starting left and right points. */


#include <stdio.h>

#include <math.h>

double myFunction(double x, double a, double b,double c)

{

  return a*x*x + b*x +c;

}

int main(void){

double a,b,c;

double leftpt, rightpt, midpt, epsilon = .0000001;

double midvalue, rtvalue, root;

printf("Enter the value of 'a','b' and 'c': ");

scanf("%lf %lf %lf", &a, &b, &c);

printf("Enter the value of left and right point: ");

scanf("%lf %lf", &leftpt, &rightpt);

do {

        midpt = (leftpt + rightpt)/2;

        rtvalue = myFunction(rightpt,a,b,c);

        midvalue = myFunction(midpt,a,b,c);

        if (rtvalue * midvalue >= 0)

            rightpt = midpt;

        else 

leftpt  = midpt;

} while ((rightpt - leftpt) > epsilon);


root = (rightpt+leftpt)/2;

printf("Root for equation%5.2lf*x**2+%5.2lf*x+%5.2lf is",a,b,c);

printf("%5.2lf\n",root);

return 0;

}

Output:

C program to find a root of the equation using Bi Section method


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...