1个回答
展开全部
参考
http://zhidao.baidu.com/question/39397721.html
针对x^2-3x+2-e^x=0
#include <stdio.h>
#include <math.h>
typedef double (*Fun)(double);
double secant (double x0, double x1, Fun f) //割线法
{
double f0, f1, x2;
f0 = f(x0); f1 = f(x1);
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
while(fabs(x2 - x1) > 1e-12)
{
x0 = x1, f0 = f1;
x1 = x2, f1 = f(x1);
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
}
return x2;
}
double fun(double x) //f(x)=x^2-3x+2-e^x
{
return x * (x - 3.0) + 2.0 - exp(x);
}
int main(void)
{
double ret = secant(1.0, 2.0, fun);
printf("f(%.10f) = %.10f\n", ret, fun(ret));
return 0;
}
结果:f(0.2575302854) = 0.0000000000
http://zhidao.baidu.com/question/39397721.html
针对x^2-3x+2-e^x=0
#include <stdio.h>
#include <math.h>
typedef double (*Fun)(double);
double secant (double x0, double x1, Fun f) //割线法
{
double f0, f1, x2;
f0 = f(x0); f1 = f(x1);
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
while(fabs(x2 - x1) > 1e-12)
{
x0 = x1, f0 = f1;
x1 = x2, f1 = f(x1);
x2 = x1 - f1 * (x1 - x0) / (f1 - f0);
}
return x2;
}
double fun(double x) //f(x)=x^2-3x+2-e^x
{
return x * (x - 3.0) + 2.0 - exp(x);
}
int main(void)
{
double ret = secant(1.0, 2.0, fun);
printf("f(%.10f) = %.10f\n", ret, fun(ret));
return 0;
}
结果:f(0.2575302854) = 0.0000000000
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询