/************************
* 用迭代法求方程
* f(x)=e^(-x)-x+1=0
* 的根
*************************/
#include<stdio.h>
#include<math.h>
#include<conio.h>
float fa(float);
float dd(float);
int main() {
float x0;
printf("input data x0 = ");
scanf("%f", &x0);
printf("The root of f(x) = 0 is x = \t%f\n", dd(x0));
return 0;
}
float dd(float a) {
float x, x0, e;
int i;
printf("input eps = ");
scanf("%f", &e);
x0 = a;
printf("x0 = %f\n", x0);
for (i = 1;; i++) {
x = fa(x0);
printf("x(%d)=%f\n", i, x);
if (fabs(x - x0) < e)
return x;
else
x0 = x;
}
}
float f(float x) {
float a;
a = exp(-x) - x + 1;
return a;
}
float fa(float x) {
float a;
a = exp(-x) + 1;
return a;
}
转载于:https://www.cnblogs.com/java20130722/archive/2013/06/11/3206792.html