Solve the equation: p e
f(x)是减函数,可以二分。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps=1e-7;
int p,q,r,s,t,u;
double f(double x)
{
return p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;
}
int main()
{
double ll,rr,mid;
while (scanf("%d%d%d%d%d%d",&p,&q,&r,&s,&t,&u)==6)
{
ll=0;
rr=1;
if (f(ll)+eps<0||f(rr)>eps)
{
printf("No solution\n");
continue;
}
while (fabs(rr-ll)>eps)
{
mid=(ll+rr)/2;
if (f(mid)>0) ll=mid;
else rr=mid;
}
printf("%.4f\n",ll);
}
}

本文介绍了一种使用二分法求解特定形式方程的数值解法。该方程包含指数、三角函数等复杂成分,并通过C++实现。文章提供了完整的代码示例,展示了如何确定解的存在区间并逐步逼近精确解。
325

被折叠的 条评论
为什么被折叠?



