gpt4 book ai didi

c++ - 假位法(需要帮助)

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:34:18 26 4
gpt4 key购买 nike

我正在尝试编写代码以使用假位置法求非线性方程的根。
我完成了我的代码,但我仍然有问题。例如,如果我知道根在 5 和 6 之间。所以我输入上限为 7,下限为 6。我仍然得到了根。我不明白假位置方法是如何收敛的,即使两个初始猜测没有包含根。

这是我的代码:

void main()
{
std::cout << "Enter the First Limit: " << std::endl;
double x1;
std::cin >> x1;

std::cout << "Enter The Second Limit: " << std::endl;
double x2;
std::cin >> x2;

std::cout << "\nThe root = " << iteration(x1,x2); << std::endl;
}

double f(double x)
{
return pow(x,3) - 8*pow(x,2)+12*x-4;
}

// Evaluating the closer limit to the root
// to make sure that the closer limit is the
// one that moves and the other one is fixed

inline bool closerlimit(double u, double l)
{
return fabs(f(u)) > fabs(f(l)));
}

double iteration(double u, double l)
{
double s=0;
for (int i=0; i<=10; i++)
{
s = u - ((f(u)*(l-u)) / (f(l)-f(u)));
if (closerlimit(u,l))
l = s;
else
u = s;
}

return s;
}

最佳答案

你的函数图和根:

enter image description here

关于c++ - 假位法(需要帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5780642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com