参考网上的牛顿迭代法C++程序 写了两个计算函数 输入初始迭代值得到结果 计算公式可以根据实际情况更改
#include<stdio.h>
#include<math.h>#include<iostream>
using namespace std;
void Newton1(double x) //已知应变值通过循环应力—应变曲线求应力
{
double n = 1 / 0.137;
double a = 1 / (1.96*pow(10, 5));
double b = 1 / pow(1004.2, n);
double c = 0.021;
double x0, f, f1;
int iteCount = 0;
do
{
x0 = x;
f= b*pow(x, n) + a*x - c;
f1= b*n*pow(x, n - 1) + a;
x = x0 - f / f1;
iteCount++;
printf("第%d次迭代方程的值为:%f\n", iteCount, f);
cout << "当前的近似根为: " << x << endl;
} while (fabs(x - x0) >= 1e-5);
cout << "总迭代次数:" << iteCount << endl;
cout << "最终的近似根为:" << x << endl;
}
void Newton2(double x) //利用应力—应变迟滞回线求应力
{
double n = 1 / 0.137;
double a = 1 / (2*1.96*pow(10, 5));
double b = 1 / pow(2*1004.2, n);
double c = 3.19*0.001;
double x0, f, f1;
int iteCount = 0;
do
{
x0 = x;
f = b*pow(x, n) + a*x - c;
f1 = b*n*pow(x, n - 1) + a;
x = x0 - f / f1;
iteCount++;
printf("第%d次迭代方程的值为:%f\n", iteCount, f);
cout << "当前的近似根为: " << x << endl;
} while (fabs(x - x0) >= 1e-5);
cout << "总迭代次数:" << iteCount << endl;
cout << "最终的近似根为:" << x << endl;
}
int main()
{
double xFirst1,xFirst2;
cout << "Please input xFirsts: ";
cin >> xFirst1 >> xFirst2;
Newton1(xFirst1);
Newton2(xFirst2);
return 0;
}