⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bisection.cpp

📁 计算方法中的二分法,计算数值,非线性方程的数值解法
💻 CPP
字号:
// bisection.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    double fabs(double,double);
    double f(double);
    double x1 ,x2 ,x = 0;
	int k = 0;

    do
	{
		cout<<"请输入[a1,b1]区间a1和b1的值:";
		cin >> x1 >> x2;
		cout << "a1="<< x1 << "  b1=" << x2<<endl;
    }
	while(f(x1) > 0 && f(x2) > 0 || x1 > x2);

	printf("f(x)= x*x*x*x*x*x - x - 1\n");

    do
	{
        x = (x1 + x2) / 2;
        if(f(x1) * f(x) < 0) 
            x2 = x;
        else 
			x1 = x;
		k++;
		if(k > 1000)
		{
			//cout<<"\n计算超时,找不到精确结果\n";
			break;
		}
		else if(fabs(f(x),0) >= 1e-5)
		{
			cout<<"结果为:"<<x<<" 分半次数为:"<<k<<endl;
		}
    }
	while(1);
	
	system("pause");

    return 0;
}

double f(double x)
{      
    return x*x*x*x*x*x - x - 1;
}

double fabs(double a,double b)
{    
	return (a>b?(a-b):(b-a));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -