Solving equation 2 * x ^ 3-5 * x ^ 2 + 3 * x-6 = 1 0 roots of real numbers

topic

Here Insert Picture Description

analysis

First it can be determined between which two integers, then the exact decimal places.

Code

#include<cstdio>
#include<iostream>

#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
	int a,i;
	double b,k,j,x;
	i=1;
	while(2*i*i*i-5*i*i+3*i-6<0)i++;//先找出它在哪两个整数之间 
	x=i;
	j=x-1;
	while(2*j*j*j-5*j*j+3*j-6<0)
	j+=0.00001;
	x=j;
	k=x-0.00001;
	for(;;k+=0.000001)//使其精确到0.00001 
	{
		b=2*k*k*k-5*k*k+3*k-6;
		
		if(b>=-0.00001&&b<=0.00001)
		{
			cout<<k;
			return 0;//找到答案,可以直接返回 
		}
	}
	
	return 0;
	
 } 

result

Here Insert Picture Description

correct

Xu brother analysis, this code should be a problem (laugh cry), the original title is the distance the x-axis and the root is less than precision, my approach is the y-axis is less than the distance with accuracy. So, I still use half approximation of it.

Code

#include<cstdio>
#include<iostream>

#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
	int a,i;
	double b,k,j;
	double x1,x2;
	i=1;
	while(2*i*i*i-5*i*i+3*i-6<0)i++;//先找出它在哪两个整数之间 ,其实这个过程可以省略
	x1=i-1;
	x2=i;
	for(;;)
	{
		k=(x1+x2)/2;
		b=2*pow(k,3)-5*k*k+3*k-6;
		if(b<0)x1=k;
		else if(b>0)x2=k;
		if(fabs(x1-x2)<=0.00001)
		{
			cout<<x1;
			return 0;
		}
	}
	return 0;
	
 } 

result

Here Insert Picture Description
Of course, there are other solution, we can see they wrote
solving equations (iterative method / Newton iteration / Gaussian elimination) Detailed and template
algorithm analysis and design - to solve the root of the equation (group) iterative method (Detailed)

Released eight original articles · won praise 1 · views 248

Guess you like

Origin blog.csdn.net/xililixilu/article/details/104167594