zoukankan      html  css  js  c++  java
  • 计算方法之二分法求方程根

    /************************
    * 用二分法求方程
    * f(x)=x^3-2x-5=0
    * 在区间[2,3]内的根
    *************************/
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    
    float f(float x) {
    	float a;
    	a = x * x * x - 2 * x - 5;
    	return a;
    }
    int main() {
    	float a, b, e, x;
    	printf("\nplease input data a = ");
    	scanf("%f", &a);
    	printf("\nplease input data b = ");
    	scanf("%f", &b);
    	printf("\nplease input data eps = ");
    	scanf("%f", &e);
    	if (f(a) * f(b) < 0) {
    		while (f(x) != 0) {
    			x = (a + b) / 2;
    			if (f(a) * f(x) < 0) {
    				b = x;
    				if (abs(b - a) < e) {
    					break;
    				} else
    					continue;
    			} else {
    				a = x;
    				if (abs(b - a) < e) {
    					break;
    				} else
    					continue;
    			}
    		}
    		printf("\n");
    		x = (a + b) / 2;
    		printf("The root of f(x) = 0 is x =\t%f\n", x);
    	} else
    		printf("\n Not root ! Afresh input!\n");
    	return 0;
    }


  • 相关阅读:
    问题 B: 投简历
    问题 C: P4 游戏中的Human角色
    绘制直方图
    绘制条形图
    绘制散点图
    问题 A: E2 驾驭const
    问题 B: 矩形类中运算符重载【C++】
    extjs tips
    extjs
    struts1
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206793.html
Copyright © 2011-2022 走看看