Newton's iterative method to find approximate roots of equations

Question : Use the Newton iteration method to solve the approximate solution of 3*x*x*x-2*x*x-16=0.

#include<stdio.h>
#include<math.h>
#define E 1e-8
double hs(double x) {
	return(3*x*x*x-2*x*x-16);
//Primitive
}
double dhs(double x) {
	return(9*x*x-4*x);
//derivation function
}
int main() {
	double x1 = 1.0, x2;
	x2 = x1 + 1.0;
	while(fabs(x2 - x1) > E) {//No matter how it changes, as long as x1 and x2 can enter the loop, the solution can be completed, that is, x2!=x1.
		x1 = x2;
		x2 = x1 - hs(x1) / dhs(x1);
	}//Here is obtained by keeping the first two items in Taylor expansion. Readers can find relevant literature to understand it in detail.
	printf("Equation: 3*x*x*x-2*x*x-16=0\n");
	printf("Solution: x=%.4lf\n", x2);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326039744&siteId=291194637