循环不适用于割线方法
loop is not working with secant method
我为找到方程的根而编写的程序已接近完成,但我遇到了一个小问题。我用来将速度和角度的变化值分配给方程的嵌套循环不起作用。无论是在循环中的某个地方,还是在调用 fx 割线的某个地方,我都错过了一个错误。它一直为我提供相同的根、迭代次数和 f(x) 数字。如果有人能帮助我找到我的错误,我将不胜感激:)
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
using namespace std;
// Declaration of functions used
void secant(double, double, double, double, double, double, double, double, double&, int& );
double fx( double, double, double, double, double, double, double);
const double tol=0.0001;
// Tolerance for convergence
const int max_iter=50; // Maximum iterations allowed
// main program
int main()
{
int iteration; // Number of iterations
double kr, uc, q, b, radians;
double x0, x1; // Starting values for x
double root; // Root found by secant method
const double PI = 4.0*atan(1.0);
ifstream datain ("shuttle.txt");
ofstream dataout ("results.txt");
datain >> kr >> uc >> q >> b;
int velocity = 16000;
double angle =10;
x0= 1000;
x1 = 200;
for (int velocity = 16000; velocity <= 17500; velocity += 500)
{
for (int angle = 10; angle <= 70; angle += 15)
{
radians= angle * PI/180 ;
cout << velocity << endl;
cout << radians << endl;
cout << angle << endl;
secant (radians, velocity, kr, uc, q, b, x0, x1, root, iteration);
}
}
system("pause");
}
// Definition of function "secant"
// Receives a, b, c, d and x0 values from main program
// Returns root and the iterations required
void secant(double radians,double velocity, double kr, double uc, double q, double b, double x0, double x1, double& root, int& iteration)
{
double xnminus1, xnplus1, xn; // Local variables
iteration=0; // Initialize iterations
xnminus1=x0;
xn=x1;
do
{
++iteration;
xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/(fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1));
cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
xnminus1 = xn;
xn=xnplus1;
} while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol )&& (iteration < max_iter));
root=xnplus1;
cout<<"nThe root is = "<<root<<endl;
cout<<"The number of iterations was = "<<iteration<<endl;
cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl;
}
// Defines "fx"
double fx(double radians,double velocity, double kr, double uc, double q, double b, double ts)
{
return kr * pow(ts,4.0) + uc * ts - q - pow(velocity / b, 2.0) * sin(radians);
}
您有多个定义的变量,请尝试删除 main 中的最外层速度和角度:
int velocity = 16000; // remove this one
double angle =10; // and this one
x0= 1000;
x1 = 200;
for (int velocity = 16000; velocity <= 17500; velocity += 500)
{
for (int angle = 10; angle <= 70; angle += 15)
{
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 适用于 WebView2 旧版本的示例应用程序
- 一个模板方法,用于同时接受常量和非常量参数
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- __attribute__(优化(0))) 是否适用于"recursively"?
- 一种优雅或至少可行的方法,用于使用和接受具有重载方法和运算符的不同大小的文字数组常量
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 将 2D 矢量转换为 C 类型的最佳方法(适用于 SGX 飞地)
- 适用于 VIM 的 YouCompleteMe 插件 - 如何支持其他库方法的自动完成?(例如OpenMPI)
- 为什么仅适用于C 的课堂上定义衍生类的构造方法
- 方法DSA_do_verify和SHA1(适用于Windows的OpenSSL库)
- C++指针到方法模板推导在面向 x86 时不编译,但适用于 x64