Basic algorithm of C language 24-Golden section method for approximate root of equation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.