xchebev.cpp
来自「这是C++数值算法(第二版)的源代码,其中包含了目前一些比较常用的数值计算的算法」· C++ 代码 · 共 41 行
CPP
41 行
#include <iostream>
#include <iomanip>
#include <cmath>
#include "nr.h"
using namespace std;
// Driver for routine chebev
DP func(const DP x)
{
return x*x*(x*x-2.0)*sin(x);
}
int main(void)
{
const int NVAL=40;
const DP PIO2=1.570796326794896619;
int i,mval;
DP a=(-PIO2),b=PIO2,x;
Vec_DP c(NVAL);
NR::chebft(a,b,c,func);
// Test Chebyshev evaluation routine
cout << fixed << setprecision(6);
for (;;) {
cout << endl << "How many terms in Chebyshev evaluation?" << endl;
cout << "Enter n between 2 and " << NVAL << ". (n<2 to end)." << endl;
cin >> mval;
cin.get();
if ((mval < 2) || (mval > NVAL)) break;
cout << endl << setw(9) << "x" << setw(15) << "actual";
cout << setw(17) << "chebyshev fit" << endl;
for (i = -8;i<=8;i++) {
x=i*PIO2/10.0;
cout << setw(12) << x << setw(13) << func(x);
cout << setw(13) << NR::chebev(a,b,c,mval,x) << endl;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?