Finding the root x^3-5x^2+16x-80=0 of a three-order equation by using the chord-truncation method
#include <stdio.h>
#include <math.h>
float f (float x)//define sub-function f (x) = X^3-5x^2+16x-80, when f (x) →0, then x is the real number root of the request;
{
Float y;
Y= ((x-5.0) *x+16.0) *x-80.0;
return (y); Returns the value of f (x)
}
Float Xpoint (float x1,float x2)//define sub-function Xpoint to find the x0 value of the intersection of the chord and the x-axis;
{
float x0;
x0= (X1*f (x2)-x2*f (x1))/(f (x2)-F (x1)); Calculates the value of the x0 position of the chord and x-axis intersections
return (x0); Returns the value of the x0 position
}
float root (float x1,float x2)//define child function root for approximate real roots
{
float x0;
Do//critical cycle step, do loop operation before condition is reached;
{
X0=xpoint (X1,X2); The above sub-function Xpoint (X1,X2) is called here to get the value x0 the chord and x-axis intersection position.
if (f (x0) *f (x1) >0)//Call the Sub function f (x) Here, get the F (x0) value of the x-axis x0 position, get the F (X1) value of the X1 position, if f (x0) *f (x1) >0, that is, the same symbol;
x1=x0; →x1 the x0 assignment, the interval for the next continuation of the loop operation is x0 (x1)-x2
else//Otherwise, if f (x0) *f (x1) <0, that is, a different number;
x2=x0; →x2 the x0 assignment, the interval for the next continuation of the loop operation is x1-x0 (x2)
}
while (Fabs (f (x0)) >=1e-6);//When the type of cyclic judging conditions, 1e-6 is the value of precision, f (x0) close to 0, that is, close to the x-axis intersection, such as no result of the operation can be too many cycles, can reduce the number of powers to try again.
return (x0); Returns the value of the x0 position
}
Main ()//main function
{
float X1,x2,x0,f1,f2;
Do
{
printf ("Please enter two numeric x1,x2:\n for the estimated real roots interval");
scanf ("%f,%f", &x1,&x2); Enter two numbers x1,x2
}
while (f (x1) *f (x2) ==0),//When the loop operation to F (x1) *f (x2) >=0 (0 is a necessary parameter), i.e. F (x1), F (x2) is the same symbol, and any one close to 0 o'clock, means that the x-axis is close to intersect, there is an equation real roots.
X0=root (X1,X2); Here x0 is the equation real roots;
printf ("One of the equations real roots:%.4f", x0);
}
As an example: x1=-10,x2=10, the result of the operation is:
The root x^3-5x^2+16x-80=0 of a three-order equation is obtained by using the chord-truncation method.