对于作业问题,我们被要求以数值方式找到二次方程 ax^2 + bx + c 的估计根,(其中 a、b、c 由用户给出),我们不能使用 -b 公式我们必须使用基于简单搜索的算法来进行分配。因此,我们必须在较大的 x 值范围内找到 f(x),然后打印 x 的值,这使得 f(x) 最接近于零。
我将 f(x) 的值存储在一个数组中,并搜索最接近 0 的值。我想打印 x 的值,即函数的根,这导致了 f(x) 的值,但是不知道如何,我只能从最接近零的数组中打印 f(x) 的值。
例如,如果我的 a、b 和 c 值分别为 1、-1 和 -6,我希望我的输出显示 x = 3 作为估计根。 现在我显示的输出是 = 0。(f(x) 的值,我最接近零的元素)
这是我的代码: (如果代码格式不正确,我也很抱歉,我对使用 stackoverflow 还很陌生,非常感谢任何帮助)
/*==================================================================
* 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);
}
您有一个将数组索引
i
转换为 x 值的表达式:
x = 0 + i*(0.5)
为了使这更容易,请添加一个函数来执行此转换
// Returns the x value used for the index
float index_to_x(int i) {
return i * (0.5);
}
然后你可以将此函数放入 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 */
}
现在您已经有了该功能,您可以更新
array_roots
来使用它:
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);
}
如您所见,这将打印出用于查找根的 x 坐标。