For a homework question we have been asked to numerically find the estimated roots of a quadratic equation ax^2 + bx + c, (where a, b, c is given by the user), we cant use the -b formula and we have to use a simple search based algorithm for the assignment. So, we have to find f(x) over a large range of values of x and then print the value of x which gives f(x) closest to zero.
I stored the values of f(x) in an array and searched for the value which is closest to 0. I want to print the value of x, the root of the function, which resulted in this value of f(x) but dont know how, i can only print the value of f(x) from the array which is closest to zero.
For example if my values for a, b &c were 1, -1 & -6 respectively, i want my output to display x = 3 as the estimated root. Right now my displayed output is = 0. (the value of f(x), the element in my closest to zero)
This is my code: (also sorry if the code is not formatted properly, im still new to using stackoverflow, any help is greatly appreciated)
/*==================================================================
* Systems header files
*==================================================================*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/*==================================================================
* Constant definitions
*==================================================================*/
#define SIZE 50
/*==================================================================
* Function definitions
*==================================================================*/
float array_roots(const float [], int); /*my function where i will search for my root value*/
int main(void)
{
float table[SIZE]; /* array to store the function values f(x) */
float a, b, c, x;
int i;
printf("*********************************************************\n");
printf("Welcome to the quadratic root estimator.\n");
printf("This estimates the value of one root of\n");
printf("f(x)=ax^2+bx+c.\n");
printf("*********************************************************\n");
printf("Enter the coefficients in the form \"a b c\"\n: ");
scanf("%f %f %f",&a, &b, &c);
/*populating array and calling function */
for(i=0; i<SIZE; i++)
{
x = 0 + i*(0.5); /* the range of values of x im using are between 1 and 50*/
table[i] = a*x*x + b*x + c; /* to store the value of f(x) at the correct point in the array */
}
/* Prints out value from the array which is closest to zero
But i want it to print out the root of the function, x which gave the value of f(x) closest to zero*/
printf("There is a root at: x = %.3f\n", array_roots(table, SIZE));
return(0);
}
/*//////////////////////////////////////////////*/
/*function outside of main to find element in array closest to zero */
/*//////////////////////////////////////////////*/
float array_roots(const float table[], int length)
{
int i; /* index for loop over array*/
float root; /* 'running' root. This will eventually be the root element of the array */
root = table[0]; /* At the beginning, assume that the first element is the root */
/* Next, loop through the array. For each element encountered,
if this element is closer to zero, then
set the running root equal to this value */
for(i=1; i<length; i++)
if(table[i] == 0 || abs(0-table[i]) < abs(0-root))
root = table[i];
/* At this point, variable 'root' holds the correct root element */
return(root);
}
You have an expression that turns an array index i
into the x value:
x = 0 + i*(0.5)
To make this easier, add a function to do this conversion
// Returns the x value used for the index
float index_to_x(int i) {
return i * (0.5);
}
You can then put this function into main:
/*populating array and calling function */
for(i=0; i<SIZE; i++)
{
x = index_to_x(i); /* the range of values of x im using are between 1 and 50*/
table[i] = a*x*x + b*x + c; /* to store the value of f(x) at the correct point in the array */
}
Now that you have that function, you can update array_roots
to use it:
float array_roots(const float table[], int length)
{
int i; /* index for loop over array*/
float root; /* 'running' root. This will eventually be the root element of the array */
int min_i = 0; /* index of the minimum root */
root = table[0]; /* At the beginning, assume that the first element is the root */
/* Next, loop through the array. For each element encountered,
if this element is closer to zero, then
set the running root equal to this value */
for(i=1; i<length; i++) {
if(table[i] == 0 || abs(0-table[i]) < abs(0-root)) {
root = table[i];
min_i = i;
}
}
printf("The x coordinate for the minimum root is %f\n", index_to_x(min_i));
/* At this point, variable 'root' holds the correct root element */
return(root);
}
As you can see, this will print out the x coordiante used to find the root.