Brute force Polynomials

The quadratic function ax^2 + bx + c achieves the value 0 for certain values of x (called the roots of the quadratic) – these depend on the parameters a, b, and c. the function will have either one root (repeated twice) or two separate real (i.e., not complex) roots. Your job is to numerically find one root of the quadratic using a “brute-force” method. Specifically, you should evaluate ) (xf over a large range of values of x, and select the value of x which sets ) (xf closest to zero. For example if 1 a , 1 b and 6 c , then a search over the range 0 to 10 will show that values of x near 3 will yield ) (xf to be close to zero. You can then output 3 x as being an estimated root of the quadratic. Your program should also check that the condition ac b 4 2  holds, and if not, it should print “Complex Roots – cannot estimate with this program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include <stdio.h>
#include <math.h>

int main(){

printf("************************************************\nWelcome to the quadratic root estimator\nThis estimates the value of one root of\nf(x)=ax^2+bx+c\n************************************************\n");

printf("Enter the coefficients in the form 'a b c':");

float a, b, c;

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

if((b*b)<(4*a*c))
    printf("Complex Roots-cannot estimate with this program");




else{

float x, y;


printf("%f %f %f", a, b, c);
/* Evaluate x values and find f(x) closest to zero */

for(x=-20;x<20;x++){
    y=a*x*x + b*x +c;

    printf("%f", y);





}






}
}
Topic archived. No new replies allowed.