Golden Division!
/*
================================================================
Title: The root of 3*x*x*x-2*x*x-16=0 is solved by the Golden Section method.
================================================================
*/
#include <stdio.h>
#include <math.h>
#define E 1e-8
Double HS (Double x)
{
return 3*x*x*x-2*x*x-16;
}
Double Gen (double a,double b)
{
Double x,fx,fa,fb,g;
G= (sqrt (5)-1.0)/2;
FA=HS (a);
FB=HS (b);
while (Fabs (b-a) >e)
{
x=g* (b-a) +a;
FX=HS (x);
if (fa*fx>0)
{
A=x;
FA=FX;
}
else if (fb*fx>0)
{
B=x;
FB=FX;
}
}
return x;
}
Main ()
{
Double A,B,T,FA,FB;
while (fa*fb>0)
{
printf ("Input estimate interval: \na=");
scanf ("%lf", &a);
printf ("Input estimate interval: \nb=");
scanf ("%lf", &b);
FA=HS (a);
FB=HS (b);
}
T=gen (A, b);
printf ("Equation: 3*x*x*x-2*x*x-16=0\n");
printf ("Solution: x=%.4lf\n", t);
}
/*
================================================================
Evaluation:
The Golden section method is efficient, but it is troublesome for beginners to write programs, compared with the poor lifting method program.
Simple, but inefficient, but its scope of application is very wide, as long as a given interval, can be poor lift! For simple equations
, the poor lifting method is easy to achieve! In the absence of means, the method of exhaustive can be taken!
================================================================
*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Basic algorithm of C language 24-Golden section method for approximate root of equation